码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx-触摸事件

时间:2016-01-01 16:50:48      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

 1 //创建一个精灵
 2     Sprite *spriteTest = Sprite::create("1.png");
 3     spriteTest->setPosition(Point(100,100));
 4     this->addChild(spriteTest);
 5    //创建一个单点触摸事件监听
 6     auto listener = EventListenerTouchOneByOne::create();
 7     //向下传递触摸
 8     listener->setSwallowTouches(true);
 9     //触摸回调
10     listener->onTouchBegan = [](Touch *touch,Event *event)
11     {
12         auto target = event->getCurrentTarget();
13         Point locationInNode = target->convertToNodeSpace(touch->getLocation());
14         Size size = target->getContentSize();
15         Rect rect = Rect(0, 0, size.width, size.height);
16         if (rect.containsPoint(locationInNode)) {
17             CCLOG("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
18             target->setOpacity(180);
19             return true;
20         }
21         return false;
22     };
23     
24     listener->onTouchMoved = [](Touch *touch,Event *event)
25     {
26         auto target = event->getCurrentTarget();
27         target->setPosition(target->getPosition()+touch->getDelta());
28     };
29     
30     listener->onTouchEnded = [](Touch *touch,Event *event)
31     {
32         auto target = event->getCurrentTarget();
33         target->setOpacity(255);
34     };
35     
36     _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, spriteTest);

 

cocos2dx-触摸事件

标签:

原文地址:http://www.cnblogs.com/wanyongjian/p/5093390.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!