代码改变世界

cocos2dx 3.x(精灵的碰撞检测,点击移动与拖动精灵)

2016-10-23 03:15  罗任德  阅读(...)  评论(...编辑  收藏
 1 //
 2 //  MainScene.hpp
 3 //  helloworld
 4 //
 5 //  Created by apple on 16/9/19.
 6 //
 7 //
 8 
 9 #ifndef MainScene_hpp
10 #define MainScene_hpp
11 
12 #include <stdio.h>
13 #include "cocos2d.h"
14 USING_NS_CC;
15 using namespace cocos2d;
16 //定义一个场景类
17 
18 class MainScene : public cocos2d::Layer{
19     
20 private:
21     // 成员变量(私有的)
22     Size size;//注意不能用auto关键字
23     Sprite *sprite1, *sprite2;//定义两个成员变量的精灵(注意不能用auto关键字
24     Label *label;//全局变量Label->显示碰撞与未碰撞(注意不能用auto关键字)
25 public:
26     virtual bool init(); // 虚函数,返回值为布尔类型,没有函数
27 
28     static cocos2d::Scene* createScene();//static是一个类方法返回场景
29     //声明点击手势
30     virtual bool onTouchBegan(Touch *touch, Event *unused_event);
31     //声明拖动手势
32     virtual void onTouchMoved(Touch *touch, Event *unused_event);
33     
34     CREATE_FUNC(MainScene);
35 };
36 
37 
38 #endif /* MainScene_hpp */

 

 

 1 //
 2 //  MainScene.cpp
 3 //  helloworld
 4 //
 5 //  Created by apple on 16/9/19.
 6 //
 7 //
 8 
 9 #include "MainScene.hpp"
10 cocos2d::Scene * MainScene::createScene()
11 {
12      auto scene = Scene::create();
13 //    CCScene * scene = CCScene::create();// 创建场景
14     //创建层
15     MainScene *layer = MainScene::create();
16     scene->addChild(layer);
17     return scene;
18 }
19 bool MainScene::init(){
20     if (!Layer::init()) {
21         return false;
22     }
23     //获取屏幕大小
24     size = Director::getInstance()->getVisibleSize();
25     //auto size = Director::getInstance()->getWinSize();
26     //添加一个图片精灵
27     sprite1 = Sprite::create("CloseNormal.png");
28     sprite1->setPosition(Vec2(size.width/2, size.height/2));
29     this->addChild(sprite1);
30     
31     sprite2 = Sprite::create("CloseNormal.png");
32     sprite2->setPosition(Vec2(size.width*0.4, size.height/2));
33     this->addChild(sprite2);
34     
35     label = Label::createWithSystemFont("No collision", "Marker Felt", 50);
36     label->setPosition(Vec2(size.width/2, size.height*0.7));
37     this->addChild(label);//直接把Label显示在屏幕上
38     
39     //创建监听事件对象
40     auto listener = EventListenerTouchOneByOne::create();
41     
42     //定义监听事件的回调函数
43     listener->onTouchBegan = CC_CALLBACK_2(MainScene::onTouchBegan, this);
44     listener->onTouchMoved = CC_CALLBACK_2(MainScene::onTouchMoved, this);
45     
46     //在事件分发器中注册
47     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
48     
49     return true;
50 }
51 
52 //点击移动精事件回调方法
53 bool MainScene::onTouchBegan(Touch *touch, Event *unused_event)
54 {
55     //获取触屏位置(坐标)
56     Point pos = touch->getLocation();
57     
58     sprite2->setPosition(pos);//把点击屏幕的触摸点(坐标)给赋值给精灵2
59     
60     log("TouchBegan");
61     
62     return true;
63 }
64 //拖动手势执行事件方法
65 void MainScene::onTouchMoved(Touch *touch, Event *unused_event)
66 {
67     //获取当前拖动手势的坐标与位置
68     Point pos = touch->getLocation();
69     
70     sprite2->setPosition(pos);//让精灵跟着拖动点移动
71     
72     //判断两个精灵是否相交
73     if (sprite2->getBoundingBox().intersectsRect(sprite1->getBoundingBox())){    //3.0改变处
74         label->setString("Collision!");//改变屏幕上Label的文字
75     }else{
76         label->setString("No collision");//改变屏幕上Label的文字
77     
78         log("TouchMoved");
79     }
80 }