cocos2d-x学习日志(6) --触屏事件
2013-08-24 17:32
381 查看
转载请标明:转载自【小枫栏目】,博文链接:http://blog.csdn.net/my183100521/article/details/10276991
第一步:setIsTouchEnabled(true);这句话在类初始化的时候加入
第二步:重写相应的函数:
ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);//触屏开始事件
ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);//拖动事件
ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);//触屏结束事件
需要在哪个消息上作处理,就重写哪个函数就可以,具体的重写方法registerWithTouchDispatcher,如
注:若setTouchEnabled(),没调用registerWithTouchDispatcher,直接引用注册函数吧~~
效果图:
今天的博文就写到这里,若有不好之处,望各大网友留言赐教,大家交流下~~
参考博文:http://blog.csdn.net/bill_man/article/details/7214667
一、简介
一般在菜单中,不是继承自CCLayer,这时候要检测触屏,就需要两步就可以完成第一步:setIsTouchEnabled(true);这句话在类初始化的时候加入
第二步:重写相应的函数:
ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);//触屏开始事件
ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);//拖动事件
ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);//触屏结束事件
需要在哪个消息上作处理,就重写哪个函数就可以,具体的重写方法registerWithTouchDispatcher,如
void HelloWorld::registerWithTouchDispatcher(void) { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0,true); //单点触摸 //pDirector->getTouchDispatcher()->addStandardDelegate(this, 0);//多点触摸 }
二、实例
1.主角随手移动,参考《Cocos2d-x 权威指南》里的例子。
HelloWorldScene.h//add mumber CCSprite* hero; float deltax; float deltay; bool isControl; //touch事件 std::string title(); void registerWithTouchDispatcher(void); bool ccTouchBegan(CCTouch* touch,CCEvent* event); void ccTouchMoved(CCTouch* touch,CCEvent* event); void ccTouchEnded(CCTouch* touch,CCEvent* event); void ccTouchCancelled(CCTouch* touch,CCEvent* event);HelloWorldScene.cpp
std::string HelloWorld::title()在init()添加代码:
{
return "Targeted touches";
}
void HelloWorld::registerWithTouchDispatcher(void) { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0,true); //单点触摸 //pDirector->getTouchDispatcher()->addStandardDelegate(this, 0);//多点触摸 }
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
CCLog("ccTouchBegan");
CCPoint heropos = hero->getPosition();
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
if (location.x > heropos.x - 42.5 && location.x < heropos.x +42.5 && location.y > heropos.y - 60.5 && location.y <heropos.y +60.5) {
isControl = true;
deltax = location.x - heropos.x;
deltay = location.y - heropos.y;
}
return true;
}
void HelloWorld::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
CCLog("ccTouchMoved");
if (isControl) {
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
float x = location.x - deltax;
float y = location.y - deltay;
hero->setPosition(ccp(x,y));
}
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
CCLog("ccTouchEnded");
isControl = false;
}
void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *touch, cocos2d::CCEvent *event)
{
CCLog("ccTouchCancelled");
isControl = false;
}
hero = CCSprite::create("Role.png"); this->addChild(hero); //registerWithTouchDispatcher(); this->setTouchEnabled(true);
注:若setTouchEnabled(),没调用registerWithTouchDispatcher,直接引用注册函数吧~~
效果图:
今天的博文就写到这里,若有不好之处,望各大网友留言赐教,大家交流下~~
参考博文:http://blog.csdn.net/bill_man/article/details/7214667
相关文章推荐
- cocos2d-x-LuaProxy学习日志(8) -- Lua实现事件派发器
- cocos2d-x基础学习--添加触摸事件
- logz.io一个企业级的ELK日志分析器 内部集成了机器学习识别威胁——核心:利用用户对于特定日志事件的反馈处理动作来学习判断日志威胁 + 类似语音识别的专家系统从各方收集日志威胁信息
- cocos2d学习-Touch事件
- Android学习日志:事件拦截机制
- Cocos2d-x 3.1.1 学习日志15--getContentSize和getContentSizeInPixels区别
- Cocos2d-x学习(三):触屏事件(单点触屏)
- Cocos2d-x 3.1.1 学习日志12--一Cocos2dx3.1.1移植到Android平台的方法(最实用最有效的!!)
- cocos2d-x-LuaProxy学习日志(7) -- 场景切换
- Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
- Cocos2D-X2.2.3学习笔记8(处理精灵单击、双击和三连击事件)
- Cocos2d-x 3.1.1 学习日志6--30分钟了解C++11新特性
- Cocos2D-X2.2.3学习笔记8(处理精灵单击、双击和三连击事件)
- cocos2d-x学习之菜单与事件,消息触发机制
- cocos2d-x-LuaProxy学习日志(2) -- 搭建cocos2dx-LuaProxy (2.1.4) + cocosbuilder
- Cocos2d-x 3.1.1 学习日志16--A星算法(A*搜索算法)学问
- cocos2d-x学习笔记-触屏事件详解
- cocos2d-x学习日志(5) --定时器
- Cocos2d-x 3.1.1 学习日志13--物理引擎登峰造极之路
- cocos2d-x 2.X demo学习笔记 9 ----Touches 触摸事件 以及碰撞检测