制作飞镖忍者(1) Cocos2d-x 3.0alpha0(4)
来源:未知 责任编辑:责任编辑 发表时间:2013-12-06 08:43 点击:次
this->scheduleUpdate();11.编译运行,这时当子弹和怪物碰撞时,它们就会消失;
12.接下来,创建一个新的场景,来指示"You Win"或者"You Lose"。创建新类
GameOverLayer.h文件代码为:
#include "cocos2d.h" USING_NS_CC; class GameOverLayer:public LayerColor{ public: GameOverLayer(); ~GameOverLayer(); bool initWithWon(bool won); static Scene* sceneWithWon(bool won); void gameOverDone(); };GameOverLayer.cpp文件代码为:
// // GameOverLayer.cpp // HelloCpp // // Created by 杜甲 on 13-12-1. // // #include "GameOverLayer.h" #include "HelloWorldScene.h" GameOverLayer::GameOverLayer() { } GameOverLayer::~GameOverLayer() { } GameOverLayer* GameOverLayer::createWithWon(bool won) { GameOverLayer* pRet = new GameOverLayer(); if (pRet && pRet->initWithWon(won)) { } else{ CC_SAFE_RELEASE_NULL(pRet); } return pRet; } bool GameOverLayer::initWithWon(bool won) { bool bRet = false; do { CC_BREAK_IF(!LayerColor::initWithColor(Color4B(255, 255, 255, 255))); char* message; if (won) { message = "You Won"; } else { message = "You Lose"; } Size winSize = Director::getInstance()->getWinSize(); LabelTTF* label = LabelTTF::create(message, "Arial", 32); label->setColor(Color3B(0, 0, 0)); label->setPosition(Point(winSize.width / 2, winSize.height / 2)); this->addChild(label); this->runAction(Sequence::create(DelayTime::create(3), CallFunc::create(std::bind(&GameOverLayer::gameOverDone, this)), NULL)); bRet = true; } while (0); return bRet; } Scene* GameOverLayer::sceneWithWon(bool won) { Scene* scene = NULL; do { scene = Scene::create(); CC_BREAK_IF(!scene); GameOverLayer* layer = GameOverLayer::createWithWon(won); CC_BREAK_IF(!layer); scene->addChild(layer); } while (0); return scene; } void GameOverLayer::gameOverDone() { Director::getInstance()->replaceScene(HelloWorld::createScene()); }游戏结束时,切换到以上所建的场景,场景上的层显示一个文本,在3秒之后返回到HelloWorld场景中。
13.最后,为游戏添加一些游戏逻辑。记录玩家消灭怪物的数量,进而决定该玩家输赢。在HelloWorldScene.h文件中,添加如下:
int _monstersDestroyed;添加头文件引用:
#include "GameOverLayer.h"在update定时函数中,monstersToDelete循环removeChild(monster, true)的后面添加被消灭怪物的计数,并判断胜利条件,代码如下:
_monstersDestroyed++; if (_monstersDestroyed > 2) { Scene* gameOverScene = GameOverLayer::sceneWithWon(true); Director::getInstance()->replaceScene(gameOverScene); }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>