【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景

【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景,第1张

概述        主场景要包含其他类的头文件 #include "cocos2d.h"#include "MyPlane.h"#include "Bullet.h"#include "EnemyManager.h"#include "Controller.h"#include "BackgroundMove.h"#include "FlowWord.h" 在这个游戏中,我将各种碰撞检测也

主场景要包含其他类的头文件

#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);   // 子d和敌机碰撞voID enemyCollisionPlane();    // 我机和敌机、敌机子d碰撞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);	// 开启子d	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;	//子d和敌机对碰	for (auto& eButtle : bulletLayer->vecBullet)	{		Sprite* pBullet = (Sprite*)eButtle; // 获取子d精灵		bMoveButt = bulletCollisionEnemy(pBullet);		if (bMoveButt)		{			// 子d删除了,无需再遍历			return;		}	}	// 敌机、敌机子d与我方飞机碰撞	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();			}			//删除子d			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; // 获取子d精灵		// 是否发生碰撞		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 (十) 游戏主场景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069136.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存