cocos2d-x3.2单点触摸

来源:互联网 发布:崩坏学园2淘宝代充 编辑:程序博客网 时间:2024/04/30 15:40


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//GameScene.h
#include "cocos2d.h"
USING_NS_CC;
 
class GameScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
     
    virtual bool init();
 
    virtual bool onTouchBegan(Touch *touch, Event *unused_event);
    virtual void onTouchMoved(Touch *touch, Event *unused_event);
    virtual void onTouchEnded(Touch *touch, Event *unused_event);
     
    void menuCallback(cocos2d::Ref* pSender);
     
    CREATE_FUNC(GameScene);
};




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//GameScene.cpp
//功能:运行点击屏幕,控制台显示触点坐标,并将图标移动到触点位置
 
#include "GameScene.h"
 
USING_NS_CC;
 
cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}
 
//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
     
    //获取屏幕大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //添加一个图片精灵
    auto sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    this->addChild(sprite);
     
    auto sprite1 = Sprite::create("Flag_1.png");
    sprite1->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    sprite1->setTag(12);
    this->addChild(sprite1);
     
    //1.注册监听事件对象
    auto listener = EventListenerTouchOneByOne::create();       //单点触摸
    //auto listener = EventListenerTouchAllAtOnce::create();      //多点触摸
    //setTouchEnabled(true);        //3.0版本已经被弃用
     
    //2.定义监听对象的回调方法
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this);
     
    //3.在事件分发器中注册
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
     
    return true;
}
 
//触摸事件函数(内部已创建,这里重新编写)
bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)
{
//    touch->getLocation();       //获得的是OpenGL坐标
//    auto p1 = touch->getLocationInView();     //获得的是UI坐标
//    auto p2 = Dirctor::getInstance()->convertToGL(p1);    //转换成OpenGL坐标
    CCLOG("TouchBegan x:%lf, y:%lf", touch->getLocation().x, touch->getLocation().y);
     
    auto sprite = this->getChildByTag(12);      //通过标识获取精灵
    //sprite->setPosition(Vec2(touch->getLocation().x, touch->getLocation().y));
    sprite->runAction(MoveTo::create(0.5f, Point(touch->getLocation().x, touch->getLocation().y)));
 
    return true;
}
 
void GameScene::onTouchMoved(Touch *touch, Event *unused_event)
{
    //图标随鼠标一起移动
//    auto sprite = this->getChildByTag(12);      //通过标识获取精灵
//    sprite->setPosition(Vec2(touch->getLocation().x, touch->getLocation().y));
}
 
void GameScene::onTouchEnded(Touch *touch, Event *unused_event)
{
     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//GameScene.h
#include "cocos2d.h"
USING_NS_CC;
 
class GameScene : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();
     
    virtual bool init();
 
    virtual bool onTouchBegan(Touch *touch, Event *unused_event);
    virtual void onTouchMoved(Touch *touch, Event *unused_event);
    virtual void onTouchEnded(Touch *touch, Event *unused_event);
     
    void menuCallback(cocos2d::Ref* pSender);
     
    CREATE_FUNC(GameScene);
};




?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//GameScene.cpp
//功能:运行点击屏幕,控制台显示触点坐标,并将图标移动到触点位置
 
#include "GameScene.h"
 
USING_NS_CC;
 
cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}
 
//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
     
    //获取屏幕大小
    Size visibleSize = Director::getInstance()->getVisibleSize();
    //添加一个图片精灵
    auto sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    this->addChild(sprite);
     
    auto sprite1 = Sprite::create("Flag_1.png");
    sprite1->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
    sprite1->setTag(12);
    this->addChild(sprite1);
     
    //1.注册监听事件对象
    auto listener = EventListenerTouchOneByOne::create();       //单点触摸
    //auto listener = EventListenerTouchAllAtOnce::create();      //多点触摸
    //setTouchEnabled(true);        //3.0版本已经被弃用
     
    //2.定义监听对象的回调方法
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this);
     
    //3.在事件分发器中注册
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
     
    return true;
}
 
//触摸事件函数(内部已创建,这里重新编写)
bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)
{
//    touch->getLocation();       //获得的是OpenGL坐标
//    auto p1 = touch->getLocationInView();     //获得的是UI坐标
//    auto p2 = Dirctor::getInstance()->convertToGL(p1);    //转换成OpenGL坐标
    CCLOG("TouchBegan x:%lf, y:%lf", touch->getLocation().x, touch->getLocation().y);
     
    auto sprite = this->getChildByTag(12);      //通过标识获取精灵
    //sprite->setPosition(Vec2(touch->getLocation().x, touch->getLocation().y));
    sprite->runAction(MoveTo::create(0.5f, Point(touch->getLocation().x, touch->getLocation().y)));
 
    return true;
}
 
void GameScene::onTouchMoved(Touch *touch, Event *unused_event)
{
    //图标随鼠标一起移动
//    auto sprite = this->getChildByTag(12);      //通过标识获取精灵
//    sprite->setPosition(Vec2(touch->getLocation().x, touch->getLocation().y));
}
 
void GameScene::onTouchEnded(Touch *touch, Event *unused_event)
{
     

0 0
原创粉丝点击