cocos2d中分步实现飞机大战----场景跳转
首先,作为一个完整的游戏项目,要有满足游戏要求的界面,并实现之间的跳转,不能只有一个游戏主界面。 下面以主场景界面为例: .h文件 #include "cocos2d.h" USING_NS_CC; class menuScene:public Layer{ public: static Scene * createScene(); //创建场景 CREATE_FUNC(menuScene); bool init(); void menuCallBack(Ref * obj); //按钮回调函数 }; .CCP文件 #include "menuScene.h" #include "gameScene.h" //---引用要跳转的的场景 #include "helpScene.h" #include "aboutScene.h" Scene * menuScene::createScene(){ auto scene=Scene::create(); scene->addChild(menuScene::create()); return scene; } bool menuScene::init(){ if (!Layer::init()) { return false; } //-------添加背景图 auto sp=Sprite::create("img_bg_logo.jpg"); this->addChild(sp); sp->setPosition(Director::getInstance()->getWinSize().width/2, Director::getInstance()->getWinSize().height/2 ); auto splogo=Sprite::create("LOGO.png"); //-------添加logo this->addChild(splogo); splogo->setPosition(Director::getInstance()->getWinSize().width/2,-200); splogo->runAction(MoveTo::create(1,Vec2(Director::getInstance()->getWinSize(). width/2,Director::getInstance()->getWinSize().height/2)));
splogo->runAction(MoveTo::create(60,Vec2(splogo->getPositionX(),splogo->getPositionY()))); //-------添加按钮 MenuItemFont * itemstart=MenuItemFont::create("开始游戏", CC_CALLBACK_1(menuScene::menuCallBack,this)); // 添加按钮,和回调函数 itemstart->setPositionY(-100); //设置位置 itemstart->setColor(Color3B(0,255,0));//设置颜色 itemstart->setTag(11); //设置编号
MenuItemFont * itemhelp=MenuItemFont::create("游戏帮助",this)); itemhelp->setPositionY(-150); itemhelp->setColor(Color3B(0,0)); itemhelp->setTag(12);
MenuItemFont * itemabout=MenuItemFont::create("关于",this)); itemabout->setPositionY(-200); itemabout->setColor(Color3B(0,0)); itemabout->setTag(13);
Menu * menu=Menu::create(itemstart,itemhelp,itemabout,NULL); this->addChild(menu); //-----按钮点击回调函数 switch (((MenuItemFont*)obj)->getTag()) { //强制转换,获取按钮编号 case 11: { auto f=gameScene::createScene(); //创建场景 Director::getInstance()->replaceScene(f);//使用导演函数实现跳转 } break; case 12: { auto f=helpScene::createScene(); Director::getInstance()->replaceScene(f); } break; case 13: { auto f=aboutScene::createScene(); Director::getInstance()->replaceScene(f); } break; default: break; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |