cocos2d-x 3.7 飞机大战 决战南海I 游戏主场景

Posted cxchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos2d-x 3.7 飞机大战 决战南海I 游戏主场景相关的知识,希望对你有一定的参考价值。

        主场景要包括其它类的头文件

#include "cocos2d.h"
#include "MyPlane.h"
#include "Bullet.h"
#include "EnemyManager.h"
#include "Controller.h"
#include "BackgroundMove.h"
#include "FlowWord.h"

在这个游戏中。我将各种碰撞检測也放到主场景中进行

void gameUpdate(float dt);     // 碰撞检測
bool bulletCollisionEnemy(Sprite* pBullet);   // 子弹和敌机碰撞
void enemyCollisionPlane();    // 我机和敌机、敌机子弹碰撞
virtual void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);


当然。主场景要有各个类的实例做变量

MyPlane *planeLayer;
Bullet *bulletLayer;
EnemyManager *enemyLayer;
Controller *controlLayer;


主场景的实现

TollgateOne::TollgateOne() : planeLayer(NULL), bulletLayer(NULL), enemyLayer(NULL), controlLayer(NULL)
{

}
TollgateOne::~TollgateOne()
{
	_eventDispatcher->removeEventListenersForTarget(this);
}

cocos2d::Scene* TollgateOne::createScene()
{
	auto scene = Scene::create();

	auto layer = TollgateOne::create();

	scene->addChild(layer);

	return scene;
}

bool TollgateOne::init()
{
	if (!Layer::init())
	{
		return false;
	}

	// 启动触摸机制
	this->setTouchEnabled(true);

	// 背景无限滚动
	auto m_back = BackgroundMove::create();
	this->addChild(m_back,0);

	//游戏開始 飘字效果
	auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");
	auto m_flow = FlowWord::create();
	m_flow->showFlowWord(
		((__String *)(dictionary->objectForKey("play")))->getCString(),
		Point(Director::getInstance()->getVisibleSize().width / 2+60, Director::getInstance()->getVisibleSize().height/2), 
		m_flow->otherFlowWord()
		);
	this->addChild(m_flow);

	//游戏更新
	this->schedule(schedule_selector(TollgateOne::gameUpdate));

	// 增加控制层
	controlLayer = Controller::create();
	this->addChild(controlLayer);

	// 增加飞机
	planeLayer = MyPlane::create();
	this->addChild(planeLayer,1);

	// 增加敌机和分数显示
	enemyLayer = EnemyManager::create();
	enemyLayer->bindController(controlLayer);
	this->addChild(enemyLayer,1);

	// 开启子弹
	bulletLayer = Bullet::create();
	bulletLayer->bindEnemyManager(enemyLayer);
	this->addChild(bulletLayer,1);

	//对返回键的响应
	auto m_listener = EventListenerKeyboard::create();
	m_listener->onKeyReleased = CC_CALLBACK_2(TollgateOne::onKeyReleased, this);
	_eventDispatcher->addEventListenerWithSceneGraphpriority(m_listener, this);

	return true;
}

void TollgateOne::gameUpdate(float dt)
{
	bool bMoveButt = false;

	//子弹和敌机对碰
	for (auto& eButtle : bulletLayer->vecBullet)
	{
		Sprite* pBullet = (Sprite*)eButtle; // 获取子弹精灵
		bMoveButt = bulletCollisionEnemy(pBullet);
		if (bMoveButt)
		{
			// 子弹删除了。无需再遍历
			return;
		}
	}

	// 敌机、敌机子弹与我方飞机碰撞
	enemyCollisionPlane();
}

bool TollgateOne::bulletCollisionEnemy(Sprite* pBullet)
{
	for (auto& eEnemy : enemyLayer->vecEnemy)
	{
		Enemy* pEnemySprite = (Enemy*)eEnemy;

		// 是否发生碰撞
		if (pBullet->boundingBox().intersectsRect(pEnemySprite->getBoundingBox()))
		{

			// 飞机仅仅剩下一格生命值
			if (1 == pEnemySprite->getLife())
			{
				pEnemySprite->loseLife(); // 知道为什么这里也要loselife吗?你能够试着凝视掉看看
				enemyLayer->blowupEnemy(pEnemySprite);
			}
			else
			{
				pEnemySprite->loseLife();
			}

			//删除子弹
			bulletLayer->removeBullet(pBullet);
			return true;
		}
	}

	return false;
}

void TollgateOne::enemyCollisionPlane()
{
	Sprite* pPlane = (Sprite*)planeLayer->getChildByTag(AIRPLANE);
	for (auto& eEnemy : enemyLayer->vecEnemy)
	{
		Enemy* pEnemySprite = (Enemy*)eEnemy;

		// 是否发生碰撞
		if (pPlane->boundingBox().intersectsRect(pEnemySprite->getBoundingBox()) && pEnemySprite->getLife() > 0)
		{
			if (1 == planeLayer->getAlive())
			{
				planeLayer->loseAlive();
				controlLayer->getSaveData()->save();
				this->unscheduleAllSelectors();
				this->bulletLayer->StopBulletShoot();
				this->planeLayer->blowUp();
				Director::getInstance()->replaceScene(
					TransitionMoveInT::create(0.8f, GameOver::createScene())); // 替换场景
			}
			else
				planeLayer->loseAlive();
		}
	}

	for (auto& eEnemyBullet : bulletLayer->vecEnemyBullet)
	{
		Sprite* pEnemyBullet = (Sprite*)eEnemyBullet; // 获取子弹精灵

		// 是否发生碰撞
		if (pPlane->boundingBox().intersectsRect(pEnemyBullet->getBoundingBox()))
		{
			if (1 == planeLayer->getAlive())
			{
				planeLayer->loseAlive();
				controlLayer->getSaveData()->save();
				this->unscheduleAllSelectors();
				this->bulletLayer->StopBulletShoot();
				this->planeLayer->blowUp();
				Director::getInstance()->replaceScene(
					TransitionMoveInT::create(0.8f, GameOver::createScene())); // 替换场景
			}
			else
				planeLayer->loseAlive();

			bulletLayer->removeEnemyBullet(pEnemyBullet);

			return;
		}
	}
}

void TollgateOne::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE)
	{
		Director::getInstance()->replaceScene(HelloWorld::createScene());
	}
}


主场景就是将其他类融合在一起,碰撞检測也是在这里进行。


注意一下这里的事件监听,和開始界面的不太一样。但都要在析构函数中移除。

以上是关于cocos2d-x 3.7 飞机大战 决战南海I 游戏主场景的主要内容,如果未能解决你的问题,请参考以下文章

cocos2d-x 3.7 飞机大战 决战南海I 敌机实现

cocos2d-x 3.7 飞机大战 决战南海I 游戏结束场景

cocos2d-x 3.7 飞机大战 决战南海I 游戏主场景

cocos2d-x 3.7 飞机大战 决战南海I 控制器的实现

Cocos2d-x飞机大战教程笔记

基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(上)