cocos2dx 利用schedule实现长按触发事件功能

cocos2dx 利用schedule实现长按触发事件功能,第1张

概述在cocos2dx中给我们提供了很多点击屏幕触发事件监听,比如CCMenu类的一系列 ,ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent),ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent), ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent),ccTouchCancel

在cocos2dx中给我们提供了很多点击屏幕触发的事件监听,比如Ccmenu类的一系列 ,cctouchBegan(CCtouch *ptouch,CCEvent *pEvent)cctouchmoved(CCtouch *ptouch,CCEvent *pEvent) cctouchended(CCtouch *ptouch,CCEvent *pEvent)cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent)等监听方法,但有时项目中会要求当长按某一精灵达到一定时间时才响应触发事件,而不是简单的按下抬起,这时就可以利用schedule来实现,下面是实现代码。


.h文件 [cpp] view plain copy #ifndef__HELLOWORLD_SCENE_H__ #define__HELLOWORLD_SCENE_H__ #include"cocos2d.h" #include"cocos-ext.h" usingnamespacestd; usingnamespacecocos2d; usingnamespaceextension; enum{ backgroundTag=0, }; classHelloWorld:publiccclayer { public: virtualboolinit(); staticCCScene*scene(); CCSprite*red_sp; CCSprite*green_sp; CCSprite*blue_sp; CCSprite*longtouch_sp; voIDupdate(); virtualboolcctouchBegan(CCtouch*ptouch,CCEvent*pEvent); virtualvoIDcctouchmoved(CCtouch*ptouch,CCEvent*pEvent); virtualvoIDcctouchended(CCtouch*ptouch,CCEvent*pEvent); virtualvoIDcctouchCancelled(CCtouch*ptouch,CCEvent*pEvent); virtualvoIDonEnter(); virtualvoIDonExit(); CREATE_FUNC(HelloWorld); }; #endif//__HELLOWORLD_SCENE_H__
.cpp文件 [cpp] view plain copy #include"HelloWorldScene.h" #include"SimpleAudioEngine.h" usingnamespacecocos2d; usingnamespaceCocosDenshion; CCScene*HelloWorld::scene() { CCScene*scene=CCScene::create(); HelloWorld*layer=HelloWorld::create(); scene->addChild(layer); returnscene; } boolHelloWorld::init() { if(!cclayer::init()) { returnfalse; } CCSizesize=CCDirector::sharedDirector()->getWinSize(); //添加一背景当点击此背景时d出键盘 CCSprite*background=CCSprite::create("HelloWorld.png"); background->setScale(2); background->setposition(ccp(size.wIDth*0.5,size.height*0.5)); this->addChild(background,1,backgroundTag); //红色精灵 red_sp=CCSprite::create("Icon.png"); red_sp->setcolor(ccRED); red_sp->setposition(ccp(size.wIDth*0.3,size.height*0.5)); this->addChild(red_sp,1); //绿色精灵 green_sp=CCSprite::create("Icon.png"); green_sp->setcolor(ccGREEN); green_sp->setposition(ccp(size.wIDth*0.5,size.height*0.5)); this->addChild(green_sp,2); //蓝色精灵 blue_sp=CCSprite::create("Icon.png"); blue_sp->setcolor(ccBLUE); blue_sp->setposition(ccp(size.wIDth*0.7,size.height*0.5)); this->addChild(blue_sp,3); returntrue; } voIDHelloWorld::update() { this->unschedule(schedule_selector(HelloWorld::update)); //被长按触发的精灵做旋转动作 longtouch_sp->runAction(CCRotateBy::create(1,360)); } boolHelloWorld::cctouchBegan(CCtouch*ptouch,CCEvent*pEvent) { CCPointtouch_point=ptouch->getLocation(); if(red_sp->boundingBox().containsPoint(touch_point)){ this->schedule(schedule_selector(HelloWorld::update),1); longtouch_sp=red_sp; }elseif(green_sp->boundingBox().containsPoint(touch_point)){ this->schedule(schedule_selector(HelloWorld::update),1); longtouch_sp=green_sp; }elseif(blue_sp->boundingBox().containsPoint(touch_point)){ this->schedule(schedule_selector(HelloWorld::update),1); longtouch_sp=blue_sp; } returntrue; } voIDHelloWorld::cctouchmoved(CCtouch*ptouch,CCEvent*pEvent) { CCPointtouch_point=ptouch->getLocation(); if(longtouch_sp){ //当手指滑动超出所点精灵范围时取消触发 if(!longtouch_sp->boundingBox().containsPoint(touch_point)){ this->unschedule(schedule_selector(HelloWorld::update)); } } } voIDHelloWorld::cctouchended(CCtouch*ptouch,CCEvent*pEvent) { this->unschedule(schedule_selector(HelloWorld::update)); } voIDHelloWorld::cctouchCancelled(CCtouch*ptouch,CCEvent*pEvent){} voIDHelloWorld::onEnter() { //事件注册 CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,false); cclayer::onEnter(); } voIDHelloWorld::onExit() { CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this); cclayer::onExit(); }

效果如下图,当长按某一精灵达到1秒时,就会触发旋转动作。

总结

以上是内存溢出为你收集整理的cocos2dx 利用schedule实现长按触发事件功能全部内容,希望文章能够帮你解决cocos2dx 利用schedule实现长按触发事件功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1070885.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存