【cocos2d-x 3.7 飞机大战】 决战南海I (四) 敌机管理

【cocos2d-x 3.7 飞机大战】 决战南海I (四) 敌机管理,第1张

概述        敌方飞机应该不定时的出现,有自己的生命周期、运动轨迹。这个类用来管理敌机的产生、移动、爆炸、销毁等。 敌机管理类主要函数如下 //绑定控制器(更新分数) void bindController(Controller* controller); //根据分数决定添加敌机速度 void addSpeed(float dt); // 添加敌机1 void addEnemy

敌方飞机应该不定时的出现,有自己的生命周期、运动轨迹。这个类用来管理敌机的产生、移动、爆炸、销毁等。


敌机管理类主要函数如下


	//绑定控制器(更新分数)	voID bindController(Controller* controller);	//根据分数决定添加敌机速度	voID addSpeed(float dt);	// 添加敌机1	voID addEnemy1(float dt); 	// 添加敌机2	voID addEnemy2(float dt); 	// 添加敌机3	voID addEnemy3(float dt); 	// 添加敌机4	voID addEnemy4(float dt);	// 敌机爆炸	voID blowupEnemy(Enemy* pEnemySprite);		// 移除敌机pNode	voID removeEnemy(Node *pNode);		


其成员变量如下

Vector<Enemy*> vecEnemy;// 敌机容器,用于遍历碰撞问题Controller* m_controlLayer;	//控制器float m_fSpeed;	//添加敌机速度float m_fEnemy1;float m_fEnemy2;float m_fEnemy3;float m_fEnemy4;


敌机产生后,要先存储在容器中,通过容器来进行与子d、我方飞机的碰撞检测,之后统一销毁。


还有就是,因为分数是要存储在数据库中的,所以我们用一个控制器来管理分数。对于我方飞机的触摸移动,也可以用控制器来控制,由于本游戏中的我方飞机 *** 作简单,代码就嵌在了我方飞机类中。(⊙o⊙)…扯远了,先介绍敌机管理类的实现,再讲控制器。


bool EnemyManager::init(){	if (!Layer::init())	{		return false;	}	cocos2d::Vector<SpriteFrame*> vecTemp;	vecTemp.clear();	// 敌机1爆炸	for (int i = 0; i < 4; i++)	{		auto blowUpname = __String::createWithFormat("enemy1_down%d.png",i + 1);		auto tempBlowUp = SpriteFrameCache::getInstance()->getSpriteFrameByname(			blowUpname->getCString());		vecTemp.pushBack(tempBlowUp);	}	Animation* pAnimation1 = Animation::createWithSpriteFrames(vecTemp,0.1f);		// 添加到AnimationCache,并且命名为Enemy1Blowup	AnimationCache::getInstance()->addAnimation(pAnimation1,"Enemy1Blowup");	// 敌机2爆炸	vecTemp.clear();	for (int i = 0; i < 4; i++)	{		auto blowUpname = __String::createWithFormat("enemy2_down%d.png",i + 1);		auto tempBlowUp = SpriteFrameCache::getInstance()->getSpriteFrameByname(			blowUpname->getCString());		vecTemp.pushBack(tempBlowUp);	}	Animation *pAnimation2 = Animation::createWithSpriteFrames(vecTemp,0.1f);	AnimationCache::getInstance()->addAnimation(pAnimation2,"Enemy2Blowup");	// 敌机3爆炸	vecTemp.clear();	for (int i = 0; i < 4; i++)	{		auto blowUpname = __String::createWithFormat("enemy3_down%d.png",i + 1);		auto tempBlowUp = SpriteFrameCache::getInstance()->getSpriteFrameByname(			blowUpname->getCString());		vecTemp.pushBack(tempBlowUp);	}	Animation *pAnimation3 = Animation::createWithSpriteFrames(vecTemp,0.1f);	AnimationCache::getInstance()->addAnimation(pAnimation3,"Enemy3Blowup");	// 敌机4爆炸	vecTemp.clear();	for (int i = 0; i < 4; i++)	{		auto blowUpname = __String::createWithFormat("enemy4_down%d.png",i + 1);		auto tempBlowUp = SpriteFrameCache::getInstance()->getSpriteFrameByname(			blowUpname->getCString());		vecTemp.pushBack(tempBlowUp);	}	Animation *pAnimation4 = Animation::createWithSpriteFrames(vecTemp,0.1f);	AnimationCache::getInstance()->addAnimation(pAnimation4,"Enemy4Blowup");	//根据当前分数来设定添加各种敌机的速度	this->schedule(schedule_selector(EnemyManager::addSpeed),0.1f);	return true;}//绑定控制器(更新分数)voID EnemyManager::bindController(Controller* controller){	this->m_controlLayer = controller;	m_controlLayer->retain();}//根据分数决定添加敌机速度voID EnemyManager::addSpeed(float dt){	m_fSpeed = m_controlLayer->getSaveData()->getscore() / 1000 + 1;	this->schedule(schedule_selector(EnemyManager::addEnemy1),m_fEnemy1 / m_fSpeed); // 每1秒出现一架敌机1	this->schedule(schedule_selector(EnemyManager::addEnemy2),m_fEnemy2 / m_fSpeed);	this->schedule(schedule_selector(EnemyManager::addEnemy3),m_fEnemy3 / m_fSpeed);	this->schedule(schedule_selector(EnemyManager::addEnemy4),m_fEnemy4 / m_fSpeed);}voID EnemyManager::addEnemy1(float dt){	Size size = Director::getInstance()->getVisibleSize();	Enemy *pEnemySprite = Enemy::create();	pEnemySprite->setEnemyByType(Enemy1);	pEnemySprite->setTag(Enemy1);	this->addChild(pEnemySprite);	vecEnemy.pushBack(pEnemySprite);	// 设置运动轨迹 以及到终点时调用的函数	ccBezIErConfig m_bezIEr;	m_bezIEr.controlPoint_1 = ccp(size.wIDth/20,size.height*0.7);	m_bezIEr.controlPoint_2 = ccp(size.wIDth/2,size.height/2);	m_bezIEr.endposition = ccp(size.wIDth*0.9,size.height*0.9);	auto actionMove = BezIErTo::create(2.0f,m_bezIEr);	auto actionDone = CallFuncN::create(CC_CALLBACK_1(EnemyManager::removeEnemy,this));	Sequence* sequence = Sequence::create(actionMove,actionDone,NulL);	pEnemySprite->runAction(sequence);	//根据分数改变敌机数量	}voID EnemyManager::addEnemy2(float dt){	Size size = Director::getInstance()->getVisibleSize();	Enemy *pEnemySprite = Enemy::create();	pEnemySprite->setEnemyByType(Enemy2);	pEnemySprite->setTag(Enemy2);	this->addChild(pEnemySprite);	vecEnemy.pushBack(pEnemySprite);	// 设置运动轨迹 以及到终点时调用的函数	ccBezIErConfig m_bezIEr;	m_bezIEr.controlPoint_1 = ccp(40,500);	m_bezIEr.controlPoint_2 = ccp(250,400);	m_bezIEr.endposition = ccp(400,700);	auto actionMove = BezIErTo::create(4.0f,NulL); 	pEnemySprite->runAction(sequence);}voID EnemyManager::addEnemy3(float dt){	Size size = Director::getInstance()->getVisibleSize();	Enemy *pEnemySprite = Enemy::create();	pEnemySprite->setEnemyByType(Enemy3);	pEnemySprite->setTag(Enemy3);	this->addChild(pEnemySprite);	vecEnemy.pushBack(pEnemySprite);	// 设置运动轨迹 以及到终点时调用的函数	ccBezIErConfig m_bezIEr;	m_bezIEr.controlPoint_1 = ccp(60,550);	m_bezIEr.controlPoint_2 = ccp(100,700);	auto actionMove = BezIErTo::create(6.0f,NulL); 	pEnemySprite->runAction(sequence);}// 添加敌机4voID EnemyManager::addEnemy4(float dt){	Enemy *pEnemySprite = Enemy::create();	pEnemySprite->setEnemyByType(Enemy4);	pEnemySprite->setTag(Enemy4);	this->addChild(pEnemySprite);	vecEnemy.pushBack(pEnemySprite);	// 设置运动轨迹 以及到终点时调用的函数	ccBezIErConfig m_bezIEr;	m_bezIEr.controlPoint_1 = ccp(80,650);	m_bezIEr.controlPoint_2 = ccp(350,450);	m_bezIEr.endposition = ccp(400,700);	auto actionMove = BezIErTo::create(8.0f,this));		// 按顺序执行 敌机飞到边缘,敌机移动结束	Sequence* sequence = Sequence::create(actionMove,NulL);	pEnemySprite->runAction(sequence);}voID EnemyManager::removeEnemy(Node *pNode){	Enemy* enemy = (Enemy*)pNode;	if (enemy != NulL)	{		this->removeChild(enemy,true);		vecEnemy.eraSEObject(enemy);	}}voID EnemyManager::blowupEnemy(Enemy* pEnemySprite){	auto saveData = m_controlLayer->getSaveData();	Animation *pAnimation = NulL;	if (Enemy1 == pEnemySprite->getTag())	{		// 之前缓存的爆炸动作		pAnimation = AnimationCache::getInstance()->getAnimation("Enemy1Blowup");		if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())		{		}		if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())		{			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/enemy1_down.wav");		}		saveData->setscore(saveData->getscore() + ENEMY1_score);		}	else if (Enemy2 == pEnemySprite->getTag())	{		pAnimation = AnimationCache::getInstance()->getAnimation("Enemy2Blowup");		if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())		{			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/enemy2_down.wav");		}		saveData->setscore(saveData->getscore() + ENEMY2_score);	}	else if (Enemy3 == pEnemySprite->getTag())	{		pAnimation = AnimationCache::getInstance()->getAnimation("Enemy3Blowup");		if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())		{			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/enemy3_down.wav");		}		saveData->setscore(saveData->getscore() + ENEMY3_score);	}	else if (Enemy4 == pEnemySprite->getTag())	{		pAnimation = AnimationCache::getInstance()->getAnimation("Enemy4Blowup");		if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())		{			CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/enemy4_down.wav");		}		saveData->setscore(saveData->getscore() + ENEMY4_score);	}	else	{		return;	}	Animate *pAnimate = Animate::create(pAnimation);	// 爆炸完,要移除敌机	auto pActionDone = CallFuncN::create(CC_CALLBACK_0(EnemyManager::removeEnemy,this,pEnemySprite)); 	Sequence* pSequence = Sequence::create(pAnimate,pActionDone,NulL);	pEnemySprite->getSprite()->runAction(pSequence);}
每种类型的敌机的爆炸代码比较短,直接在init()函数中进行初始化。为了使游戏难度不断增加,会根据分数来改变添加敌机的速度。敌机的运动轨迹大同小异,不过贝塞尔曲线看起来确实挺不错的。 总结

以上是内存溢出为你收集整理的【cocos2d-x 3.7 飞机大战】 决战南海I (四) 敌机管理全部内容,希望文章能够帮你解决【cocos2d-x 3.7 飞机大战】 决战南海I (四) 敌机管理所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存