场景类头文件:
#ifndef__USE_SPRIT_SCENE_H__#define__USE_SPRIT_SCENE_H__#include"cocos2d.h"typedefenum{ Box_a=102,Box_b,Box_c}SpriteTags;classUseSprit:publiccocos2d::Layer{public: staticcocos2d::Scene*createScene(); //there'sno'ID'incpp,sowerecommendreturningtheclassinstancepointer //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'ID'incocos2d-iphone virtualboolinit(); // virtualvoIDonEnter(); // virtualvoIDonExit(); booltouchBegan(cocos2d::touch*touch,cocos2d::Event*event); voIDtouchmoved(cocos2d::touch*touch,cocos2d::Event*event); voIDtouchended(cocos2d::touch*touch,cocos2d::Event*event); //implementthe"staticcreate()"methodmanually CREATE_FUNC(UseSprit);};#endif
说明:1.单点触摸事件分为三段:开始拖动(touchBegan)、拖移(touchmoved)、释放拖动(touchended);
2.它们拥有共同参数:cocos2d::touch* touch,cocos2d::Event *event;
3.onEnter 初始化的时候调用,即 刚要显示当前场景的时候(场景进入的时候调用);
4.onExit 场景退出的时候调用;
场景类源文件:
#include"UseSpritScene.h"USING_NS_CC;Scene*UseSprit::createScene(){ //'scene'isanautoreleaSEObject autoscene=Scene::create(); //'layer'isanautoreleaSEObject autolayer=UseSprit::create(); //addlayerasachildtoscene scene->addChild(layer); //returnthescene returnscene;}//on"init"youneedtoinitializeyourinstanceboolUseSprit::init(){ ////////////////////////////// //1.superinitfirst if(!Layer::init()) { returnfalse; } SizevisibleSize=Director::getInstance()->getVisibleSize(); Vec2origin=Director::getInstance()->getVisibleOrigin(); // autobg=Sprite::create("bg.png",Rect(0,visibleSize.wIDth,visibleSize.height)); Texture2D::TexParamstp={GL_liNEAR,GL_liNEAR,GL_REPEAT,GL_REPEAT}; bg->getTexture()->setTexParameters(tp); bg->setposition(origin+Vec2(visibleSize.wIDth/2,visibleSize.height/2)); addChild(bg,0); Sprite*BoxA=Sprite::create("ygq.png"); BoxA->setposition(origin+Vec2(visibleSize.wIDth/2,visibleSize.height/2)+Vec2(-120,120)); addChild(BoxA,10,Box_a); Sprite*BoxB=Sprite::create("yg.png"); BoxB->setposition(origin+Vec2(visibleSize.wIDth/2,visibleSize.height/2)); addChild(BoxB,20,Box_b); Sprite*BoxC=Sprite::create("ygq.png"); BoxC->setposition(origin+Vec2(visibleSize.wIDth/2,visibleSize.height/2)+Vec2(120,160)); addChild(BoxC,30,Box_c); returntrue;}voIDUseSprit::onEnter(){ Layer::onEnter(); log("UseSpritonEnter"); autoListener=EventListenertouchOneByOne::create(); Listener->setSwallowtouches(true); Listener->ontouchBegan=CC_CALLBACK_2(UseSprit::touchBegan,this); Listener->ontouchmoved=CC_CALLBACK_2(UseSprit::touchmoved,this); Listener->ontouchended=CC_CALLBACK_2(UseSprit::touchended,this); Eventdispatcher*eventdispatcher=Director::getInstance()->getEventdispatcher(); eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,getChildByTag(Box_a)); eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener->clone(),getChildByTag(Box_b)); eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener->clone(),getChildByTag(Box_c));}boolUseSprit::touchBegan(touch*touch,Event*event){ autotarget=static_cast<Sprite*>(event->getCurrentTarget()); Vec2locationInNode=target->convertToNodeSpace(touch->getLocation()); Sizes=target->getContentSize(); Rectrect=Rect(0,s.wIDth,s.height); if(rect.containsPoint(locationInNode)) { target->runAction(ScaleBy::create(0.06f,1.06f)); returntrue; } returnfalse;}voIDUseSprit::touchmoved(touch*touch,Event*event){ autotarget=static_cast<Sprite*>(event->getCurrentTarget()); target->setposition(target->getposition()+touch->getDelta());}voIDUseSprit::touchended(touch*touch,Event*event){ autotarget=static_cast<Sprite*>(event->getCurrentTarget()); Vec2locationInNode=target->convertToNodeSpace(touch->getLocation()); Sizes=target->getContentSize(); Rectrect=Rect(0,1.0f)); }}voIDUseSprit::onExit(){ Layer::onExit(); Director::getInstance()->getEventdispatcher()->removeAllEventListeners();}总结
以上是内存溢出为你收集整理的cocos2d-x 单点触摸事件使用全部内容,希望文章能够帮你解决cocos2d-x 单点触摸事件使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)