热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

[cocos2d-x]动作+场景切换

实现一个demo,具备以下功能:1.实现带一个参数或者两个参数的方法回调。2.实现按钮围绕屏幕转动。3.实现场景的切换(中间要有过渡场景,以

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

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

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

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

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

效果图:

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


推荐阅读
  • 1.点击查看隐藏当前按钮和这个内容,显示另一个内容能。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 本文介绍了在序列化时如何对SnakeYaml应用格式化,包括通过设置类和DumpSettings来实现定制输出的方法。作者提供了一个示例,展示了期望的yaml生成格式,并解释了如何使用SnakeYaml的特定设置器来实现这个目标。对于正在使用SnakeYaml进行序列化的开发者来说,本文提供了一些有用的参考和指导。摘要长度为169字。 ... [详细]
  • 网址:https:vue.docschina.orgv2guideforms.html表单input绑定基础用法可以通过使用v-model指令,在 ... [详细]
  • 先看看ElementUI里关于el-table的template数据结构:<template><el-table:datatableData><e ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • Gitlab接入公司内部单点登录的安装和配置教程
    本文介绍了如何将公司内部的Gitlab系统接入单点登录服务,并提供了安装和配置的详细教程。通过使用oauth2协议,将原有的各子系统的独立登录统一迁移至单点登录。文章包括Gitlab的安装环境、版本号、编辑配置文件的步骤,并解决了在迁移过程中可能遇到的问题。 ... [详细]
  • 本文介绍了如何使用n3-charts绘制以日期为x轴的数据,并提供了相应的代码示例。通过设置x轴的类型为日期,可以实现对日期数据的正确显示和处理。同时,还介绍了如何设置y轴的类型和其他相关参数。通过本文的学习,读者可以掌握使用n3-charts绘制日期数据的方法。 ... [详细]
  • OpenMap教程4 – 图层概述
    本文介绍了OpenMap教程4中关于地图图层的内容,包括将ShapeLayer添加到MapBean中的方法,OpenMap支持的图层类型以及使用BufferedLayer创建图像的MapBean。此外,还介绍了Layer背景标志的作用和OMGraphicHandlerLayer的基础层类。 ... [详细]
  • 如何在elementui table 内容里面放多选框?
    本文介绍了如何在elementui的table组件中放置多选框的方法,并提供了相应的代码示例和UI图效果。通过阅读本文,你将了解如何将UI图中的多选框放到表格内容中,并实现相应的功能。 ... [详细]
  • [echarts] 同指标对比柱状图相关的知识介绍及应用示例
    本文由编程笔记小编为大家整理,主要介绍了echarts同指标对比柱状图相关的知识,包括对比课程通过率最高的8个课程和最低的8个课程以及全校的平均通过率。文章提供了一个应用示例,展示了如何使用echarts制作同指标对比柱状图,并对代码进行了详细解释和说明。该示例可以帮助读者更好地理解和应用echarts。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
author-avatar
最好的冥羽
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有