在app中我们会有页面的切换,同样在游戏中也会有页面切换,只是在游戏中我们称之为场景而已
AndroID开发中我们是通过startActivity()进行Activity跳转,或者transaction.replace()进行Fragment页面跳转的,
在cocos2dx中,与fragment的切换比较类似,使用的是
Director::getInstance()->replaceScene();当然replaceScene()需要一个scene作为参数
接下来就一起来实现第一个cocos2dx页面跳转
1.首先创建一个新的场景
创建一个新的C++类 ImageScene
ImageScene.h文件
#ifndef __d2StartScene__ImageScene__#define __d2StartScene__ImageScene__#include <stdio.h>#include <cocos2d.h>USING_NS_CC;class ImageScene:public Layer { public: virtual bool init(); //定义创建场景的静态方法 static Scene* createScene(); // 这是系统提供的,写了这个宏以后再cpp文件中就可以使用create()方法了 CREATE_FUNC(ImageScene);};#endif /* defined(__d2StartScene__ImageScene__) */
ImageScene.cpp文件,在.cpp文件中实现.h中的方法
#include "ImageScene.h"Scene* ImageScene::createScene(){ Scene *scene = Scene::create(); ImageScene *layer = ImageScene::create(); scene->addChild(layer); return scene; }bool ImageScene::init(){ Sprite *s = Sprite::create("HelloWorld.png"); Size size = Director::getInstance()->getVisibleSize(); s->setposition(size.wIDth/2,size.height/2); addChild(s); return true;}
2.在第一个场景中创建事件监听,并进行跳转 *** 作
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); LabelTTF *label = LabelTTF::create("next page","CourIEr",36); addChild(label); label->setposition(visibleSize.wIDth/2,visibleSize.height/2); //创建监听 EventListenertouchOneByOne *Listener = EventListenertouchOneByOne::create(); Listener->ontouchBegan=[label,this](touch *t,Event *e){ if (label->getBoundingBox().containsPoint(t->getLocation())) { Director::getInstance()->replaceScene(ImageScene::createScene()); } return false; }; Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,label); return true;}
好了,第一个cocos2dx的跳转就完成了
3.动画(红色字体部分)
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); LabelTTF *label = LabelTTF::create("next page",Event *e){ if (label->getBoundingBox().containsPoint(t->getLocation())) { Director::getInstance()->replaceScene(<span >TransitionFadeBL::create(1,ImageScene::createScene())</span>); } return false; }; Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,label); return true;}
4.Action(红色字体部分)
bool HelloWorld::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); LabelTTF *label = LabelTTF::create("next page",Event *e){ if (label->getBoundingBox().containsPoint(t->getLocation())) { <span >label->runAction(Moveto::create(1,Point(100,100)));</span> } return false; }; Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(Listener,label); return true;}
5.一些其他动作
/**重复动作*/ //重复旋转三次 //label->runAction(Repeat::create(RotateBy::create(1,180),3)); //不断重复旋转 //label->runAction(RepeatForever::create(RotateBy::create(1,180))); /*复合动作 使用Spawn可以包含多个动作**/ //label->runAction(Spawn::create(MoveBy::create(1,Point(50,50)),RotateBy::create(1,NulL)); /**序列动作sequence*/ //label->runAction(Sequence::create(MoveBy::create(1,NulL)); /**动作侦听*/ label->runAction(Sequence::create( MoveBy::create(1,//在这边调用这个函数 CallFunc::create([](){ MessageBox("动作结束侦听","动作结束了"); }),NulL));总结
以上是内存溢出为你收集整理的cocos2dx-场景切换及动画(三)全部内容,希望文章能够帮你解决cocos2dx-场景切换及动画(三)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)