博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景
阅读量:6700 次
发布时间:2019-06-25

本文共 4496 字,大约阅读时间需要 14 分钟。

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

#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());	}}

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

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

转载地址:http://dfwlo.baihongyu.com/

你可能感兴趣的文章
[Leetcode] Course Schedule
查看>>
[收藏]String formatting in C# :玩转string.format
查看>>
扯扯所学的编程语言
查看>>
数据库设计准则(第一、第二、第三范式说明)
查看>>
[转]SharePoint 2013配置开发环境,需安装VS2012插件
查看>>
TcxDBTreeList导出EXCEL
查看>>
12/22从东京到布里斯班
查看>>
逻辑思维简易入门
查看>>
POJ 3358 Period of an Infinite Binary Expansion
查看>>
PHP——上传头像(2)
查看>>
01-Java基础知识:数据类型与变量、标识符、运算符、表达式
查看>>
连接SQLServer时,因启用连接池导致孤立事务的原因分析和解决办法
查看>>
【转】iOS开发笔记--识别单击还是双击
查看>>
手工创建非singleton 的TopComponent
查看>>
分享一个自己写的table表格排序js插件(高效简洁)
查看>>
三十二、Android上传文件至服务器
查看>>
Silverlight企业应用框架设计【六】自定义系统菜单(使用自己的DataForm)
查看>>
jQuery插件-轻量图片轮换-UISlide
查看>>
在线购物系统权限模块
查看>>
浅谈SQL Server 2008中的Hints(提示)
查看>>