场景类头文件:
#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"youneedtoinitializeyourinstance boolUseSprit::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(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。