博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[cocos2d-x]动作+场景切换
阅读量:6763 次
发布时间:2019-06-26

本文共 10395 字,大约阅读时间需要 34 分钟。

实现一个demo,具备以下功能:

1.实现带一个参数或者两个参数的方法回调。

2.实现按钮围绕屏幕转动。

3.实现场景的切换(中间要有过渡场景,以便实现前一个场景资源的释放)。

4.实现label的循环旋转+不停的来回移动。

效果图:

实现代码:

HelloWorldScene.h:

#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__  #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayer { public:     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)     virtual bool init();      // there's no 'id' in cpp, so we recommend to return the class instance pointer     static cocos2d::CCScene* scene();          // a selector callback     void menuCloseCallback(CCObject* pSender);      // preprocessor macro for "static create()" constructor ( node() deprecated )     CREATE_FUNC(HelloWorld);          //函数回调,带一个参数的回调     void funcN_CallBack(void *sender);     //带两个参数的回调     void funcND_CallBack(void *sender,void *data); };  #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp:

#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "SecondScene.h" using namespace cocos2d; using namespace CocosDenshion;  CCScene* HelloWorld::scene() {     // 'scene' is an autorelease object     CCScene *scene = CCScene::create();          // 'layer' is an autorelease object     HelloWorld *layer = HelloWorld::create();      // add layer as a child to scene     scene->addChild(layer);      // return the scene     return scene; }  // on "init" you need to initialize your instance bool HelloWorld::init() {     //     // 1. super init first     if ( !CCLayer::init() )     {         return false;     }      /     // 2. add a menu item with "X" image, which is clicked to quit the program     //    you may modify it.      // add a "close" icon to exit the progress. it's an autorelease object     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                         "CloseNormal.png",                                         "CloseSelected.png",                                         this,                                         menu_selector(HelloWorld::menuCloseCallback) );     pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );      // create menu, it's an autorelease object     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);     pMenu->setPosition( CCPointZero );     this->addChild(pMenu, 1);                          /     // 3. add your codes below...      // add a label shows "Hello World"     // create and initialize a label     CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);      // ask director the window size     CCSize size = CCDirector::sharedDirector()->getWinSize();          CCCallFuncN *callFuncN = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::funcN_CallBack));     pLabel->runAction(callFuncN);          // position the label on the center of the screen     pLabel->setPosition( ccp(size.width / 2, size.height - 20) );      // add the label as a child to this layer     this->addChild(pLabel, 1);          CCLabelTTF* pLabel2 = CCLabelTTF::create("hi", "Thonburi", 34);          //带两个参数的回调     CCString *str = CCString::create("带两个参数的回调");     str->retain();     CCCallFuncND *callFuncND = CCCallFuncND::create(this,callfuncND_selector(HelloWorld::funcND_CallBack),str); //最后一个参数是void*可以是任意类型     pLabel2->runAction(callFuncND);          pLabel2->setPosition(ccp(size.width / 2 + 20,40));     this->addChild(pLabel2,1);          // add "HelloWorld" splash screen"     CCSprite* pSprite = CCSprite::create("HelloWorld.png");      // position the sprite on the center of the screen     pSprite->setPosition( ccp(size.width/2, size.height/2) );      // add the sprite as a child to this layer     this->addChild(pSprite, 0);          return true; }  int i=0; void HelloWorld::menuCloseCallback(CCObject* pSender) {          CCSize size = CCDirector::sharedDirector()->getWinSize();     CCMenuItem *item = (CCMenuItem *)pSender; /******舒缓动作********************************************************/     //    CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20));     //    item->runAction(action);      /******先慢再快********************************************************/     //    CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20));     //    CCActionInterval *ease = CCEaseInOut::create(move, 4);     //    item->runAction(ease);      /*******循环移动**********************************************************/     //float duration = CCRANDOM_0_1()*5+1;     float duration = 0.1f;     CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20));     CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20));     CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20));     CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20));     CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL);     //CCRepeatForever *repeat = CCRepeatForever::create(sequence);     item->runAction(sequence);     i++;          //跳转     if (i==2) {         //replease切换场景 //        SecondScene *sense = SecondScene::create(); //        CCScene *secScene = CCScene::create(); //        secScene->addChild(sense,0); //        CCDirector::sharedDirector()->replaceScene(secScene);                  //push切换场景         CCTransitionFade *secondScene = CCTransitionFade::create(1.0f,SecondScene::scene(),ccGREEN);         CCDirector::sharedDirector()->pushScene(secondScene);     } }  //回调函数 void HelloWorld::funcN_CallBack(void *sender) {     CCLabelTTF *label = (CCLabelTTF *)sender;     label->setString("带一个参数的回调");     CCLog("CallFuncN的回调"); }  //带两个参数的回调 void HelloWorld::funcND_CallBack(void *sender,void *data) {     CCString *str = (CCString *)data;     CCLabelTTF *label = (CCLabelTTF *)sender;     label->setString(str->getCString()); }

SecondScene.h:

#ifndef ___013_9_4___________SecondScene__ #define ___013_9_4___________SecondScene__  #include 
#include "cocos2d.h" using namespace cocos2d; class SecondScene : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(SecondScene); }; #endif /* defined(___013_9_4___________SecondScene__) */

SecondScene.cpp:

#include "SecondScene.h" #include "SimpleAudioEngine.h"  using namespace cocos2d; using namespace CocosDenshion;  CCScene* SecondScene::scene() {     // 'scene' is an autorelease object     CCScene *scene = CCScene::create();          // 'layer' is an autorelease object     SecondScene *layer = SecondScene::create();          // add layer as a child to scene     scene->addChild(layer);          // return the scene     return scene; }  // on "init" you need to initialize your instance bool SecondScene::init() {     //     // 1. super init first     if ( !CCLayer::init() )     {         return false;     }          CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                                           "CloseNormal.png",                                                           "CloseSelected.png",                                                           this,                                                           menu_selector(SecondScene::menuCloseCallback) );     pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );          // create menu, it's an autorelease object     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);     pMenu->setPosition( CCPointZero );     this->addChild(pMenu, 1);          CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);          // ask director the window size     CCSize size = CCDirector::sharedDirector()->getWinSize();          // position the label on the center of the screen          pLabel->setPosition( ccp(size.width / 2, size.height / 2) );     pLabel->setAnchorPoint(ccp(0.5, 0.5));     pLabel->setTag(10);     // add the label as a child to this layer     this->addChild(pLabel, 1); } void SecondScene::menuCloseCallback(CCObject* pSender) { //        CCDirector::sharedDirector()->end(); //     //    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) //        exit(0); //    #endif          CCSize size = CCDirector::sharedDirector()->getWinSize();     CCMenuItem *item = (CCMenuItem *)pSender; /******舒缓动作********************************************************/     //    CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20));     //    item->runAction(action);      /******先慢再快********************************************************/     //    CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20));     //    CCActionInterval *ease = CCEaseInOut::create(move, 4);     //    item->runAction(ease);           /*******循环移动**********************************************************/     //float duration = CCRANDOM_0_1()*5+1;     float duration = 0.1f;     CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20));     CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20));     CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20));     CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20));     CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL);     CCRepeatForever *repeat = CCRepeatForever::create(sequence);     item->runAction(repeat);               CCLabelTTF *label = (CCLabelTTF *)this->getChildByTag(10);     CCRotateBy *rotateBy = CCRotateBy::create(8, 360);     CCMoveTo *move11 = CCMoveTo::create(2, ccp(0, size.height/2));     CCMoveTo *move13 = CCMoveTo::create(2, ccp(size.width, size.height/2));     CCMoveTo *move12 = CCMoveTo::create(2, ccp(size.width/2, size.height/2));     CCSequence *sequence1 = CCSequence::create(move11,move12,move13,move12,NULL);     CCSpawn *span = CCSpawn::create(sequence1,rotateBy,NULL);     CCRepeatForever *repeat1 = CCRepeatForever::create(span);     label->runAction(repeat1);   }

资源文件:http://download.csdn.net/detail/s10141303/6214883

本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366390,如需转载请自行联系原作者
你可能感兴趣的文章
Babel 配置工程师应知应会
查看>>
iKcamp&掘金Podcast直播回顾(12月2号和9号的两场)
查看>>
nodejs + koa2 实现爬虫
查看>>
聊天机器人:应用程序纪元新黎明
查看>>
How to Override Equals in Java and Scala
查看>>
由Git引发的对SSH的研究
查看>>
Java IO
查看>>
面试宝典之沟通能力
查看>>
RSA加密解密(无数据大小限制,php、go、java互通实现)
查看>>
ES6系列文章 模板字符串
查看>>
logrotate使用
查看>>
Trie树使用实例
查看>>
ELK 集群 + Redis 集群 + Nginx ,分布式的实时日志(数据)搜集和分析的监控系统搭建,简单上手使用...
查看>>
【Java并发编程的艺术】第二章读书笔记之synchronized关键字
查看>>
WePY为了兼容支付宝小程序,改了好几十行代码
查看>>
翻译:为Forge Viewer的 模型贴材质
查看>>
vue pomodoro (番茄钟) 组件 - 基于vue2.x
查看>>
TWaver可视化编辑器的前世今生(三)Doodle
查看>>
字节码及ASM使用
查看>>
重定向和伪静态在网站中的应用
查看>>