首页 > 代码库 > cocos2dx 3.x(捕鱼达人炮台角度换算)

cocos2dx 3.x(捕鱼达人炮台角度换算)

 1 //
 2 //  GameScence.hpp
 3 //  NotesDamo
 4 //
 5 //  Created by apple on 16/10/23.
 6 //
 7 //
 8 
 9 #ifndef GameScence_hpp
10 #define GameScence_hpp
11 
12 #include <stdio.h>
13 #include "cocos2d.h"
14 
15 class GameScence : public cocos2d::Layer
16 {
17 private:
18     //创建一个私有的精灵成员变量
19     cocos2d::Sprite * m_spriteGun;
20 public:
21     
22     
23     static cocos2d::Scene* createScene();//声明创建当前的层
24     
25     virtual bool init();//声明初始化层实例函数。
26     
27 
28 
29     bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);//手势点击事件
30     
31     
32         CREATE_FUNC(GameScence);//CREATE_FUNC是cocos2d-x中定义的一个宏(作用是:创建一个静态函数"static create()",该函数可以用来创建层);
33     
34 };
35 #endif /* GameScence_hpp */

 

 

 1 //
 2 //  GameScence.cpp
 3 //  NotesDamo
 4 //
 5 //  Created by apple on 16/10/23.
 6 //
 7 //
 8 
 9 #include "GameScence.hpp"
10 
11 USING_NS_CC;
12 
13 Scene* GameScence::createScene()
14 {
15     auto scene = Scene::create();
16     
17     auto layer = GameScence::create();
18     
19     scene->addChild(layer);
20     
21     return scene;
22 }
23 
24 GameScence::GameScence()
25 {
26     
27 }
28 bool GameScence::init()
29 {
30     //////////////////////////////
31     // 1. super init first
32     // 初始化父类
33     if ( !Layer::init() )
34     {
35         return false;
36     }
37         
38     
39 m_spriteGun = Sprite::create(StringUtils::format("gun2_0.png"));
40         m_spriteGun->setPosition(240, 30);
41         this ->addChild(m_spriteGun);
42 
43     //声明
44     auto listener = EventListenerTouchOneByOne::create();
45     listener->setSwallowTouches(true);
46 
47     //注册事件
48     listener->onTouchBegan = CC_CALLBACK_2(GameScence::onTouchBegan, this);
49 
50     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
51     
52     
53     return true;
54 }
55 
56 
57 bool GameScence::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event)
58 {
59     
60     touch->getLocation();//    获取当前点击的坐标
61     
62     m_spriteGun->cocos2d::Node::setRotation(atan2((touch->getLocation().x-m_spriteGun->getPositionX()),(touch->getLocation().y-m_spriteGun->getPositionY()))*180/3.1415926);//改变弧度 后面加不加90要根据精灵的初始角度是怎样的
63     
64     
65     return true;
66 }

 

cocos2dx 3.x(捕鱼达人炮台角度换算)