Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

在上一篇中,我们实现了游戏的开始界面,接下来要实现游戏的主界面,主界面包含地图、我方飞机、敌机等

先来实现我方飞机


我方飞机具有哪些属性呢? 飞机要具有生命值、要有动画效果(尾部喷气),飞机不能够飞出边界,所以要进行边界检测,当飞机生命值为0时,飞机会爆炸,然后被移除。


.h文件

//飞机动画
	Animate* planeFly();

	//边界检测
	void bordercheck(float dt);

	//飞机爆炸
	void blowUp();

	//移除飞机
	void removePlane();

	//获取生命值
	int getAlive();

	//设定生命值
	void loseAlive();

	// 更新生命值
	void updateAlive(int alivE);

这个变量在create()函数中初始化,方便其他层调用我方飞机的相关数据

static MyPlane* instancePlane;	//飞机实例



我方飞机的生命值直接在这里显示、更新,不受控制器的控制
private:
	int m_alive;
	Label* aliveItem1;
	Label* aliveItem2;



.cpp文件

/*
************************************************************************
*
*	MyPlane.cpp
*	杜星飞 2015年8月13日
*   描述: 包含飞机的属性、功能等
*
************************************************************************
*/

#include "MyPlane.h"
#include "SimpleAudioENGIne.h"

MyPlane::MyPlane() :m_alive(5)
{

}
MyPlane::~MyPlane()
{

}

MyPlane* MyPlane::instancePlane = NULL;

MyPlane* MyPlane::create()
{
	MyPlane* m_plane = NULL;
	do 
	{
		m_plane = new MyPlane();
		CC_BREAK_IF(!m_planE);

		if (m_plane && m_plane->init())
		{
			m_plane->autorelease();
			instancePlane = m_plane;
		}
		else
			CC_SAFE_deletE(m_planE);
	} while (0);

	return m_plane;
}


//飞机动画
Animate* MyPlane::planeFly()
{
	Vector<SpriteFrame *> vector;
	for (int i = 0; i < 2; i++)
	{
		auto framename = __String::createWithFormat("chinaFly%d.png",i + 1);
		auto temSpriteFrame = SpriteFrameCache::geTinstance()->getSpriteFrameByName(framename->getCString());
		vector.pushBACk(temSpriteFramE);
	}
	//设置不断播放飞机的动画
	auto animation = Animation::createWithSpriteFrames(vector,0.2f,-1);
	auto animate = Animate::create(animation);

	return animate;
}

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

	Size winSize = Director::geTinstance()->getWinSize();

	//添加飞机
	auto m_planesprite = Sprite::createWithSpriteFramename("chinaFly1.png");
	m_planesprite->setPosition(Point(winSize.width / 2,m_planesprite->getContentSize().height / 2));
	m_planesprite->setTag(AIRPLANE);
	this->addChild(m_planespritE);
	m_planesprite->runAction(this->planeFly());

	// 飞机触摸
	auto listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);	//吞噬触摸事件

	//对触摸事件的监听过程直接写在这里
	listener->onTouchBegan = [](Touch* touch,Event *event)
	{
		auto target = static_cast<Sprite*>(event->getCurrentTarget());

		Point LOCATIOnInNode = target->convertToNodeSpace(touch->getLOCATIOn());
		Size s = target->getContentSize();
		Rect rect = Rect(0,s.width,s.height);

		if (rect.containsPoint(LOCATIOnInNodE))
			return true;
		else
			return false;
	};

	listener->onTouchMoved = [](Touch* touch,Event *event)
	{
		auto target = static_cast<Sprite*>(event->getCurrentTarget());
		target->setPosition(target->getPosition() + touch->getDelta());
	};

	listener->onTouchEnded = [](Touch* touch,Event* event)
	{
	};

	//将触摸监听添加到eventDispacher中去  
	_eventDispatcher->addEventListenerWithSceneGraphpriority(listener,m_planespritE);

	//初始化生命值

	//设置标签 并 获取中文文本
	auto Dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");

	aliveItem1 = Label::createWithTTF(
		(((__String*)(Dictionary->objectForKey("alive"))))->getCString(),"fonts/DFPShaoNvW5-GB.ttf",25);
	aliveItem1->setPosition(Point(winSize.width/8,winSize.height-aliveItem1->getContentSize().height));
	aliveItem1->setColor(Color3B(255,0));
	this->addChild(aliveItem1);

	aliveItem2 = Label::createWithTTF(
		"5",25);
	aliveItem2->setPosition(Point(aliveItem1->getPositionX()*2,winSize.height - aliveItem1->getContentSize().height));
	aliveItem2->setColor(Color3B(255,0));
	this->addChild(aliveItem2);

	// 开启边界检测
	this->schedule(schedule_SELEctor(MyPlane::bordercheck));

	return true;
}


//边界检测
void MyPlane::bordercheck(float dt)
{
	//进行边界判断,不可超出屏幕  
	Point LOCATIOn = this->getChildByTag(AIRPLANE)->getPosition();
	Size winSize = Director::geTinstance()->getWinSize();  

	// 返回的就是这个矩形的大小
	Size planesize = this->getChildByTag(AIRPLANE)->getContentSize();  
	
	if (LOCATIOn.x<planesize.width / 2)
		LOCATIOn.x = planesize.width / 2;

	if (LOCATIOn.x>winSize.width - planesize.width / 2)
		LOCATIOn.x = winSize.width - planesize.width / 2;

	if (LOCATIOn.y<planesize.height / 2)
		LOCATIOn.y = planesize.height / 2;

	if (LOCATIOn.y>winSize.height - planesize.height / 2)
		LOCATIOn.y = winSize.height - planesize.height / 2;

	this->getChildByTag(AIRPLANE)->setPosition(LOCATIOn);
}

//飞机爆炸
void MyPlane::blowUp()
{
	this->unscheduleAllSELEctors(); // 停止飞机的所有行动

	//加载飞机爆炸动画 音效
	if (CocosDenshion::SimpleAudioENGIne::geTinstance()->isBACkgroundMusicPlaying())
	{
		CocosDenshion::SimpleAudioENGIne::geTinstance()->playEffect("sound/chinaDown.mp3");
	}
	
	Vector<SpriteFrame*> planeBlowUp;
	for (int i = 0; i < 4; i++)
	{
		auto planename = __String::createWithFormat("china1_down%d.png",i + 1);
		auto tempBlowUp = SpriteFrameCache::geTinstance()->getSpriteFrameByName(
			planename->getCString());
		planeBlowUp.pushBACk(tempBlowUp);
	}

	Animation* animation = Animation::createWithSpriteFrames(planeBlowUp,0.2f);
	Animate* animate = Animate::create(animation);
	CallFunc* m_removePlane = CallFunc::create(this,callfunc_SELEctor(MyPlane::removePlanE));
	Sequence* sequence = Sequence::create(animate,m_removePlane,null); 

	// 停止一切的飞机动作
	this->getChildByTag(AIRPLANE)->stopAllActions(); 

	this->getChildByTag(AIRPLANE)->runAction(sequencE);
}

//移除飞机
void MyPlane::removePlane()
{
	// 移除飞机精灵 true子节点上的所有运行行为和回调将清理
	this->removeChildByTag(AIRPLANE,truE); 
}

//获取生命值
int MyPlane::getAlive()
{
	return m_alive;
}

//设定生命值
void MyPlane::loseAlive()
{
	--m_alive;
	updateAlive(m_alivE);
}

// 更新生命值
void MyPlane::updateAlive(int alivE)
{
	if (alive >= 0)
	{
		CCString* strAlive = CCString::createWithFormat("%d",alivE);
		aliveItem2->setString(strAlive->getCString());
		aliveItem2->setColor(Color3B(rand_0_1() * 255,rand_0_1() * 255,rand_0_1() * 255));
	}
}

更新生命值的函数只用在我方飞机生命值减少是调用。


还有就是对于中文字符的处理

//设置标签 并 获取中文文本
	auto Dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");

可以在项目中添加一个XML文件

<?xml version="1.0" encoding="UTF-8"?>
<Dict>
<key>play</key>
<String>开始游戏</String>
<key>score</key>
<String>得分:</String>
<key>alive</key>
<String>生命:</String>

通过相应的key来显示显示相应的中文。

还有就是,有些字体不支持中文的显示,比如系统自带的arial.ttf就不行,而DFPShaoNvW5-GB.ttf可以。

大佬总结

以上是大佬教程为你收集整理的【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现全部内容,希望文章能够帮你解决【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。