到目前为止,这个游戏没有任何提示,也没有制定任何游戏规则,这样就没法玩了。下面来制定游戏输赢的规则:放走任何一个敌人就算输,打死n个敌人算赢(n自己可以调),并且提示玩家最终的游戏结果。
添加两个文件:GameOverScene.cpp 和GameOverScene.h。
GameOverScene.h

#ifndef GAME_OVER_SCENE_H
#define GAME_OVER_SCENE_H

#include "cocos2d.h"

class GameOverLayer:public cocos2d::CCLayerColor
{
public:
	GameOverLayer():_label(NULL)
	{};

	virtual ~GameOverLayer();

	bool init();

	void gameOverDone();

	LAYER_NODE_FUNC(GameOverLayer);

	//定义一个只读属性Label,在类定义中可以使用this->getLabel来访问
	CC_SYNTHESIZE_READONLY(cocos2d::CCLabelTTF*,_label,Label);

};

class GameOverScene:public cocos2d::CCScene
{
public:
	GameOverScene():_layer(NULL)
	{};
	virtual ~GameOverScene();

	bool init();

	SCENE_NODE_FUNC(GameOverScene);

	CC_SYNTHESIZE_READONLY(GameOverLayer*,_layer,Layer);

};

#endif

CC_SYNTHESIZE_READONLY(varType, varName, funName)声明一个成员变量以及getfunName函数,没有set函数。getfunName已经实现,其实现就是返回这个值。

GameOverScene.cpp

#include "GameScene.h"
#include "GameOverScene.h"

using namespace cocos2d;

bool GameOverScene::init()
{
	if(CCScene::init())
	{
		this->_layer = GameOverLayer::node();
		this->_layer->retain();
		//this->_layer->setIsRelativeAnchorPoint(true);
		this->addChild(_layer);
		return true;	
	}
	else
	{
		return false;	
	}
}

GameOverScene::~GameOverScene()
{
	if(this->_layer)
	{
		this->_layer->release();
		this->_layer = NULL;
	}
}

bool GameOverLayer::init()
{
	if(CCLayerColor::initWithColor(ccc4(255,255,255,255)))
	{
		CCSize winSize = CCDirector::sharedDirector()->getWinSize();
		this->_label = CCLabelTTF::labelWithString("","Artial",32);
		this->_label->retain();
		this->_label->setColor(ccc3(0,0,0));
		this->_label->setPosition(ccp(winSize.width/2,winSize.height/2));
		this->addChild(this->_label);

		this->runAction(CCSequence::actions(CCDelayTime::actionWithDuration(3),
			                            CCCallFunc::actionWithTarget(this,
										callfunc_selector(GameOverLayer::gameOverDone)),NULL));
		return true;
	}
	else
	{
		return false;	
	}
}

void GameOverLayer::gameOverDone()
{
	CCDirector::sharedDirector()->replaceScene(SimpleGame::scene());
}

GameOverLayer::~GameOverLayer()
{
	if(this->_label)
	{
		this->_label->release();	
		this->_label = NULL;
	}
}

这个类实现很简单,就是在游戏结束场景层中添加一个文字标签提示。

现在需要在GameScene.h中给SimpleGame添加一个成员变量enemyDestoryed,表示有多少敌人被消灭了。在GameScene.cpp的init中添加:

enemyDestoryed = 0;

在GameScene.cpp的update方法中添加检测消灭敌人数量和切换场景的代码,修改后的代码如下:

void SimpleGame::update(ccTime dt)
{
	CCMutableArray<CCSprite *>::CCMutableArrayIterator it,jt;
    CCMutableArray<CCSprite *> *projectileToDelete = new CCMutableArray<CCSprite *>;
	//遍历所有子弹,然后根据子弹遍历所有敌人,检查子弹跟敌人是否发生碰撞
	for(jt = projectiles->begin(); jt != projectiles->end(); jt ++)
	{
		CCSprite *projectile = *jt;
		CCRect projectileRect = CCRectMake(projectile->getPosition().x - projectile->getContentSize().width/2,
									   projectile->getPosition().y - projectile->getContentSize().height/2,
									   projectile->getContentSize().width,
									   projectile->getContentSize().height);
		CCMutableArray<CCSprite *> *enemyToDelete = new CCMutableArray<CCSprite *>;
		for(it = enemies->begin(); it != enemies->end(); it ++)
		{
			CCSprite *enemy = *it;
			CCRect enemyRect = CCRectMake(enemy->getPosition().x - enemy->getContentSize().width/2,
									   enemy->getPosition().y - enemy->getContentSize().height/2,
									   enemy->getContentSize().width,
									   enemy->getContentSize().height);
			//如果碰撞,则把被击中的敌人放进要删除的列表中
			if(CCRect::CCRectIntersectsRect(projectileRect,enemyRect))
			{
				enemyToDelete->addObject(enemy);	
			}
		}

		for(it = enemyToDelete->begin(); it != enemyToDelete->end(); it ++)
		{
			CCSprite *enemy = *it;
			enemies->removeObject(enemy);
			this->removeChild(enemy,true);
			this->enemyDestoryed ++;

			if(this->enemyDestoryed > 3)
			{
				GameOverScene *gameOverScene = GameOverScene::node();	
				gameOverScene->getLayer()->getLabel()->setString("You Win");
				CCDirector::sharedDirector()->replaceScene(gameOverScene);
			}
		}

		//无论该子弹击中多少敌人,都只添加一次
		if(enemyToDelete->count() > 0)
		{
			projectileToDelete->addObject(projectile);
		}

		enemyToDelete->release();
	}

	for(jt = projectileToDelete->begin(); jt != projectileToDelete->end(); jt ++)
	{
		CCSprite *projectile = *jt;
		projectiles->removeObject(projectile);
		this->removeChild(projectile,true);
	}
	projectileToDelete->release();

}

修改spriteMoveFinished函数,添加输了的提示代码。

void SimpleGame::spriteMoveFinished(CCNode *sender)
{
	CCSprite *sprite = (CCSprite *)sender;
	this->removeChild(sprite,true);

	if(sprite->getTag() == 1)
	{
		enemies->removeObject(sprite);		
		GameOverScene *gameOverScene = GameOverScene::node();	
		gameOverScene->getLayer()->getLabel()->setString("You Lose!!!");
		CCDirector::sharedDirector()->replaceScene(gameOverScene);
	}
	else if(sprite->getTag() == 2)
	{
		projectiles->removeObject(sprite);	
	}
}

记得包含头文件GameOverScene.h。

ok,编译运行项目,该demo在win32平台的开发已经完成。效果如下图:

 

cocos2d-x手游性能优化总结

近段时间在使用cocos2d-x开发2D手游,技术方案使用的是cocos2d-x+lua,因为游戏使用的是cocos2d-x 2.1.5版本,有些优化方案在最新版的cocos2d-x版本已经实现...

阅读全文

cocos2dx-html5 实现网页版flappy bird游戏

我也是第一次使用cocos2d_html5,对js和html5也不熟,看引擎自带的例子和引擎源码,边学边做,如果使用过cocos2d-x的话,完成这个游戏还是十分简单的。游戏体...

阅读全文

【cocos2d-x开发实战 特训99-终结篇】移植到android平台和添加admob广告

上一篇已经完成特性99在win32平台下的开发,现在把它移植到android上,首先修改Android.mk文件,内容如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_...

阅读全文

欢迎留言