在app中我们会有页面的切换,同样在游戏中也会有页面切换,只是在游戏中我们称之为场景而已
Android开发中我们是通过startActivity()进行Activity跳转,或者transaction.replace()进行Fragment页面跳转的,
在cocos2dx中,与fragment的切换比较类似,使用的是
Director::getInstance()->replaceScene();当然replaceScene()需要一个scene作为参数
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 style="background-color: rgb(255,0);">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 style="color:#FF0000;">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));