C++ 与cocos2d-x-4.0完成太空飞机大战 (二)

动画演示

游戏画面演示

飞机精灵编码:AircraftSprite.cpp

#include "AircraftSprite.h"
#include "ActionBase.h"

namespace SpaceWar {

	AircraftSprite::AircraftSprite()
	{
	}

	AircraftSprite::~AircraftSprite()
	{
	}
}


飞机精灵编码:AircraftSprite.h

#pragma once
#include "cocos2d.h"
#include "SpriteBase.h"

using namespace std;
using namespace cocos2d;

namespace SpaceWar
{
	class ActionBase;

	class AircraftSprite : public SpriteBase
	{
	public:
		AircraftSprite();
		virtual ~AircraftSprite();

		ActionBase* actBase = nullptr;

		/*玩家标识,0:玩家1,1:玩家2*/
		int playerTag = 0;

		/*飞机类型*/
		int airType = 0;

		/*飞行速度*/
		int airSpeed = 0;

		/*武器威力*/
		int armPower = 1;

		/*武器类型*/
		int armType = 0;

		/*武器上次发射时间*/
		long armTime = 0;

		/*导弹威力*/
		int missilePower = 1;

		/*导弹类型*/
		int missileType = -1;

		/*导弹上次发射时间*/
		long missileTime = 0;

		/*炸弹数量*/
		int bombCount = 3;

		/*炸弹类型*/
		int bombType = 0;

		/*炸弹上次发射时间*/
		long bombTime = 0;

		/*碰撞次数*/
		int collisionCount = 0;

		/*最大碰撞次数*/
		int maxCollisionCount = 0;

		/*碰撞类型掩码*/
		int category = 0b111;

		/*碰撞掩码*/
		int collision = 0b000;

		/*碰撞事件掩码*/
		int contact = 0b110;

		/*是否允许重力*/
		bool gravityEnable = false;

		/*是否允许碰撞*/
		bool collisionEnable = false;

		/*是否允许控制*/
		bool controlEnable = false;
	};
}


飞机动画编码:Aircraft.cpp

#include "Aircraft.h"
#include "ActionBase.h"

namespace SpaceWar {

	Aircraft::Aircraft()
	{
	}

	Aircraft::~Aircraft()
	{
	}

	bool Aircraft::init(int pTag, int aType)
	{
		Size vsize = Director::getInstance()->getVisibleSize();
		this->playerTag = pTag;
		string airImg = "fj.png";
		if (aType == 1) {
			airImg = "fj1.png";
		}
		auto pinfo = AutoPolygon::generatePolygon(airImg);
		sprite = new AircraftSprite();
		sprite->actBase = this->actBase;
		sprite->airType = aType;
		sprite->playerTag = pTag;
		int tag = 100;
		AircraftSprite::create(sprite, pinfo);
		if (sprite != nullptr)
		{
			auto size = sprite->getContentSize();
			auto body = Util::getPhysicsBody(pinfo, size, tag, sprite->category, sprite->collision, sprite->contact, sprite->gravityEnable);
			sprite->setPhysicsBody(body);
			sprite->setTag(tag);
			float w2 = (vsize.width / 2) / 2;
			if (pTag == 1)
			{
				w2 = (vsize.width / 2) + w2;
			}
			sprite->collisionEnable = false;
			sprite->setPosition(Vec2(w2, -sprite->getContentSize().height));
			auto moveTo = MoveTo::create(1.0f, Vec2(w2, vsize.height / 2 / 2));
			auto moveTo1 = MoveTo::create(1.0f, Vec2(w2, sprite->getContentSize().height * 3));
			auto sequence = Sequence::create(moveTo, moveTo1, NULL);
			sprite->runAction(sequence);
			auto blink = Blink::create(4.0f, 20);
			auto actCollEnable = CallFunc::create(std::bind(&Aircraft::setCollisionEnable, this, true));
			auto sequence1 = Sequence::create(blink, actCollEnable, NULL);
			sprite->runAction(sequence1);
			SpriteFrameCache::getInstance()->addSpriteFramesWithFile("zd01.plist");
			auto frames = Util::getAnimation("%d.png", 3);
			auto w_sprite = Sprite::createWithSpriteFrame(frames.front());
			w_sprite->setScale(0.5f);
			w_sprite->setScale(0.5f, 0.2f);
			w_sprite->setPosition(Vec2(sprite->getContentSize().width / 2, -20.0f));
			auto animation = Animation::createWithSpriteFrames(frames, 0.1f / 3);
			auto animate = Animate::create(animation);
			auto repeat = RepeatForever::create(animate);
			w_sprite->runAction(repeat);
			sprite->addChild(w_sprite);
			return true;
		}
		return false;
	}

	void Aircraft::setCollisionEnable(bool isEnable)
	{
		sprite->collisionEnable = isEnable;
	}

	/*飞机控制*/
	void Aircraft::controlAction(int player)
	{
		int *cact = actBase->colAct.controlAct;
		if (sprite == nullptr)
		{
			return;
		}
		Size ssize = sprite->getContentSize();
		Vec2 position = sprite->getPosition();
		float speed = 1.5;
		if (sprite->airSpeed == 1)
		{
			speed = 2.0;
		}
		else if (sprite->airSpeed == 2)
		{
			speed = 2.5;
		}
		else if (sprite->airSpeed == 3)
		{
			speed = 3.0;
		}
		else if (sprite->airSpeed > 3)
		{
			speed = 3.5;
		}
		if (player == 1)
		{
			if (cact[100] == 1)
			{
				Util::spriteSceneMove(0, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[101] == 1)
			{
				Util::spriteSceneMove(1, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[102] == 1)
			{
				Util::spriteSceneMove(2, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[103] == 1)
			{
				Util::spriteSceneMove(3, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[104] == 1)
			{
				launchBullet();
				launchMissile();
			}
			if (cact[105] == 1)
			{
				launchBomb();
			}
		}
		else
		{
			if (cact[0] == 1)
			{
				Util::spriteSceneMove(0, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[1] == 1)
			{
				Util::spriteSceneMove(1, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[2] == 1)
			{
				Util::spriteSceneMove(2, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[3] == 1)
			{
				Util::spriteSceneMove(3, sprite, actBase->vSize, ssize, position, speed);
			}
			if (cact[4] == 1)
			{
				launchBullet();
				launchMissile();
			}
			if (cact[5] == 1)
			{
				launchBomb();
			}
		}
	}

	/*发射子弹*/
	void Aircraft::launchBullet()
	{
		if (sprite->armType == 1)
		{
			launchBullet2();
		}
		else
		{
			launchBullet1();
		}
	}

	/*发射子弹1*/
	void Aircraft::launchBullet1()
	{
		int bspeed = 250;
		if (sprite->armPower > 5)
		{
			bspeed = 100;
		}
		else if (sprite->armPower > 3)
		{
			bspeed = 150;
		}
		else if (sprite->armPower > 2)
		{
			bspeed = 200;
		}
		if (!Util::isIntervalTime(bspeed, &sprite->armTime))
		{
			return;
		}
		AudioEngine::play2d("zd1.ogg", false, 0.2f);
		auto pinfo = AutoPolygon::generatePolygon("fgzd.png");
		int tag = 5;
		float bulletSpeed = 2.0f;
		if (sprite->armPower == 2)
		{
			bulletSpeed = 1.8f;
		}
		else if (sprite->armPower == 3)
		{
			bulletSpeed = 1.6f;
		}
		else if (sprite->armPower == 4)
		{
			bulletSpeed = 1.4f;
		}
		else if (sprite->armPower == 5)
		{
			bulletSpeed = 1.2f;
		}
		else if (sprite->armPower > 5)
		{
			bulletSpeed = 1.0f;
		}
		createBullet1(&pinfo, tag, bulletSpeed, 0);
		if (sprite->armPower > 2)
		{
			createBullet1(&pinfo, tag, bulletSpeed, 10);
			createBullet1(&pinfo, tag, bulletSpeed, -10);
		}
		if (sprite->armPower > 4)
		{
			createBullet1(&pinfo, tag, bulletSpeed, 20);
			createBullet1(&pinfo, tag, bulletSpeed, -20);
		}
		if (sprite->armPower > 5)
		{
			createBullet1(&pinfo, tag, bulletSpeed, 30);
			createBullet1(&pinfo, tag, bulletSpeed, -30);
		}
	}

	void Aircraft::createBullet1(PolygonInfo* pinfo, int tag, float speed, float startX)
	{
		Vec2 svec = sprite->getPosition();
		auto b_sprite = new BulletSprite();
		b_sprite->actBase = this->actBase;
		BulletSprite::create(b_sprite, *pinfo);
		auto size = b_sprite->getContentSize();
		auto b_body = Util::getPhysicsBody(*pinfo, size, tag, b_sprite->category, b_sprite->collision, b_sprite->contact, b_sprite->gravityEnable);
		b_sprite->setPhysicsBody(b_body);
		b_sprite->setTag(tag);
		b_sprite->playerTag = sprite->playerTag;
		b_sprite->setPosition(Vec2(svec.x + startX, svec.y + size.height * 2));
		auto moveBy = MoveBy::create(speed, Vec2(0, actBase->vSize.height + size.height));
		auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, b_sprite));
		auto sequence = Sequence::create(moveBy, actRuin, NULL);
		b_sprite->runAction(sequence);
		actBase->addChild(b_sprite, 0);
	}

	/*发射子弹2*/
	void Aircraft::launchBullet2()
	{
		int bspeed = 400;
		if (sprite->armPower > 5)
		{
			bspeed = 250;
		}
		else if (sprite->armPower > 3)
		{
			bspeed = 300;
		}
		else if (sprite->armPower > 2)
		{
			bspeed = 350;
		}
		if (!Util::isIntervalTime(bspeed, &sprite->armTime))
		{
			return;
		}
		AudioEngine::play2d("zd1.ogg", false, 0.2f);
		auto pinfo = AutoPolygon::generatePolygon("fgzd.png");
		int tag = 5;
		float bulletSpeed = 2.0f;
		if (sprite->armPower == 2)
		{
			bulletSpeed = 1.8f;
		}
		else if (sprite->armPower == 3)
		{
			bulletSpeed = 1.6f;
		}
		else if (sprite->armPower == 4)
		{
			bulletSpeed = 1.4f;
		}
		else if (sprite->armPower == 5)
		{
			bulletSpeed = 1.2f;
		}
		else if (sprite->armPower > 5)
		{
			bulletSpeed = 1.0f;
		}
		createBullet2(&pinfo, tag, bulletSpeed, 0, 0);
		if (sprite->armPower > -1)
		{
			createBullet2(&pinfo, tag, bulletSpeed, 10, 150);
			createBullet2(&pinfo, tag, bulletSpeed, -10, -150);
		}
		if (sprite->armPower > 3)
		{
			createBullet2(&pinfo, tag, bulletSpeed, 20, 250);
			createBullet2(&pinfo, tag, bulletSpeed, -20, -250);
		}
		if (sprite->armPower > 5)
		{
			createBullet2(&pinfo, tag, bulletSpeed, 30, 350);
			createBullet2(&pinfo, tag, bulletSpeed, -30, -350);
		}
	}

	void Aircraft::createBullet2(PolygonInfo* pinfo, int tag, float speed, float startX, float endX)
	{
		Vec2 svec = sprite->getPosition();
		auto b_sprite = new BulletSprite();
		b_sprite->actBase = this->actBase;
		BulletSprite::create(b_sprite, *pinfo);
		auto size = b_sprite->getContentSize();
		auto b_body = Util::getPhysicsBody(*pinfo, size, tag, b_sprite->category, b_sprite->collision, b_sprite->contact, b_sprite->gravityEnable);
		b_sprite->setPhysicsBody(b_body);
		b_sprite->setTag(tag);
		b_sprite->playerTag = sprite->playerTag;
		b_sprite->setPosition(Vec2(svec.x + startX, svec.y + size.height * 2));
		auto moveBy = MoveBy::create(speed, Vec2(endX, actBase->vSize.height + size.height));
		auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, b_sprite));
		auto sequence = Sequence::create(moveBy, actRuin, NULL);
		b_sprite->runAction(sequence);
		actBase->addChild(b_sprite, 0);
	}

	/*发射导弹*/
	void Aircraft::launchMissile()
	{
		if (sprite->missileType < 0)
		{
			return;
		}
		if (sprite->missileType == 1)
		{
			launchMissile2();
		}
		else
		{
			launchMissile1();
		}
	}

	/*发射导弹1*/
	void Aircraft::launchMissile1()
	{
		int bspeed = 850;
		if (sprite->missilePower > 5)
		{
			bspeed = 700;
		}
		else if (sprite->missilePower > 3)
		{
			bspeed = 750;
		}
		else if (sprite->missilePower > 2)
		{
			bspeed = 800;
		}
		if (!Util::isIntervalTime(bspeed, &sprite->missileTime))
		{
			return;
		}
		float bulletSpeed = 6.0f;
		float bulletScale = 0.5f;
		int bulletPower = 10;
		if (sprite->missilePower == 2)
		{
			bulletSpeed = 6.5f;
			bulletScale = 0.55f;
			bulletPower = 12;
		}
		else if (sprite->missilePower == 3)
		{
			bulletSpeed = 7.0f;
			bulletScale = 0.60f;
			bulletPower = 14;
		}
		else if (sprite->missilePower == 4)
		{
			bulletSpeed = 7.5f;
			bulletScale = 0.65f;
			bulletPower = 16;
		}
		else if (sprite->missilePower == 5)
		{
			bulletSpeed = 8.0f;
			bulletScale = 0.70f;
			bulletPower = 18;
		}
		else if (sprite->missilePower > 5)
		{
			bulletSpeed = 8.5f;
			bulletScale = 0.75f;
			bulletPower = 20;
		}
		Size vsize = Director::getInstance()->getVisibleSize();
		Vec2 svec = sprite->getPosition();
		auto pinfo = AutoPolygon::generatePolygon("air_Missile.png");
		Size size;
		Size size1;
		int tag = 5;
		if (sprite->missilePower > -1)
		{
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 10, 0);
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -10, 0);
		}
		if (sprite->missilePower > 3)
		{
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 20, 40);
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -20, 40);
		}
		if (sprite->missilePower > 5)
		{
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 30, 80);
			createMissile1(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -30, 80);
		}
	}

	void Aircraft::createMissile1(PolygonInfo* pinfo, int tag, float scale, int power, float speed, float startX, float startY)
	{
		Vec2 svec = sprite->getPosition();
		auto b_sprite = new BulletSprite();
		b_sprite->actBase = this->actBase;
		BulletSprite::create(b_sprite, *pinfo);
		Size size = b_sprite->getBoundingBox().size;
		auto b_body = Util::getPhysicsBody(*pinfo, size, tag, b_sprite->category, b_sprite->collision, b_sprite->contact, b_sprite->gravityEnable);
		b_sprite->setPhysicsBody(b_body);
		b_sprite->setScale(scale);
		b_sprite->bulletSpeed = speed;
		size = b_sprite->getBoundingBox().size;
		b_sprite->setTag(tag);
		b_sprite->bulletPower = power;
		b_sprite->playerTag = sprite->playerTag;
		b_sprite->setPosition(Vec2(svec.x + startX, svec.y + startY));
		Vec2 v2 = Vec2(0, actBase->vSize.height + size.height);
		float mTime = Util::getMoveTime(b_sprite->bulletSpeed, v2.y);
		auto moveBy = MoveBy::create(mTime, v2);
		auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, b_sprite));
		auto sequence = Sequence::create(moveBy, actRuin, NULL);
		b_sprite->runAction(sequence);
		actBase->addChild(b_sprite, 0);
	}

	/*发射导弹2*/
	void Aircraft::launchMissile2()
	{
		int bspeed = 850;
		if (sprite->missilePower > 5)
		{
			bspeed = 700;
		}
		else if (sprite->missilePower > 3)
		{
			bspeed = 750;
		}
		else if (sprite->missilePower > 2)
		{
			bspeed = 800;
		}
		if (!Util::isIntervalTime(bspeed, &sprite->missileTime))
		{
			return;
		}
		float bulletSpeed = 5.0f;
		float bulletScale = 0.3f;
		int bulletPower = 4;
		if (sprite->missilePower == 2)
		{
			bulletSpeed = 5.5f;
			bulletScale = 0.35f;
			bulletPower = 5;
		}
		else if (sprite->missilePower == 3)
		{
			bulletSpeed = 6.0f;
			bulletScale = 0.40f;
			bulletPower = 6;
		}
		else if (sprite->missilePower == 4)
		{
			bulletSpeed = 6.5f;
			bulletScale = 0.45f;
			bulletPower = 7;
		}
		else if (sprite->missilePower == 5)
		{
			bulletSpeed = 7.0f;
			bulletScale = 0.50f;
			bulletPower = 8;
		}
		else if (sprite->missilePower > 5)
		{
			bulletSpeed = 7.5f;
			bulletScale = 0.55f;
			bulletPower = 10;
		}
		auto pinfo = AutoPolygon::generatePolygon("air_Missile1.png");
		int tag = 5;
		if (sprite->missilePower > -1)
		{
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 10, 50, 10);
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -10, -50, -10);
		}
		if (sprite->missilePower > 3)
		{
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 20, 200, 20);
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -20, -200, -20);
		}
		if (sprite->missilePower > 5)
		{
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, 30, 300, 30);
			createMissile2(&pinfo, tag, bulletScale, bulletPower, bulletSpeed, -30, -300, -30);
		}
	}

	void Aircraft::createMissile2(PolygonInfo* pinfo, int tag, float scale, int power, float speed, float startX, float endX, float rotation)
	{
		Vec2 svec = sprite->getPosition();
		auto b_sprite = new BulletSprite();
		b_sprite->actBase = this->actBase;
		BulletSprite::create(b_sprite, *pinfo);
		Size size = b_sprite->getBoundingBox().size;
		auto b_body = Util::getPhysicsBody(*pinfo, size, tag, b_sprite->category, b_sprite->collision, b_sprite->contact, b_sprite->gravityEnable);
		b_sprite->setPhysicsBody(b_body);
		b_sprite->setScale(scale);
		size = b_sprite->getBoundingBox().size;
		b_sprite->setTag(tag);
		b_sprite->bulletPower = power;
		b_sprite->playerTag = sprite->playerTag;
		b_sprite->setPosition(Vec2(svec.x + startX, svec.y));
		b_sprite->bulletSpeed = speed;
		Node* eNode = Util::getRandomNode(&actBase->enemyNodes, 50, 400, b_sprite->getPosition());
		if (eNode != nullptr)
		{
			b_sprite->trackKey = eNode->getName();
			b_sprite->trackPoint = eNode->getPosition();
			b_sprite->moveSpeed = Util::getTrackPosition(b_sprite->bulletSpeed, b_sprite->getPosition(), eNode->getPosition());
			b_sprite->isTrack = true;
		}
		if (!b_sprite->isTrack)
		{
			Vec2 endPoint = Vec2(endX, actBase->vSize.height + size.height);
			float mTime = Util::getMoveTime(b_sprite->bulletSpeed, endPoint.y);
			auto moveBy = MoveBy::create(mTime, endPoint);
			b_sprite->runAction(moveBy);
			b_sprite->setRotation(rotation);
		}
		actBase->addChild(b_sprite, 0);
	}

	/*发射炸弹*/
	void Aircraft::launchBomb()
	{
		if (sprite->bombCount < 1)
		{
			return;
		}
		if (!Util::isIntervalTime(1000, &sprite->bombTime))
		{
			return;
		}
		--sprite->bombCount;
		if (sprite->bombType == 1)
		{
			launchBomb2();
		}
		else
		{
			launchBomb1();
		}
	}

	/*发射炸弹类型1*/
	void Aircraft::launchBomb1()
	{
		int playerTag = sprite->playerTag;
		Vec2 svec = sprite->getPosition();
		Size size = sprite->getBoundingBox().size;
		float sw = 20;
		float ew = actBase->vSize.width - 20;
		float sh = svec.y - actBase->vSize.height / 2;
		float eh = svec.y + actBase->vSize.height / 2;
		SpriteFrameCache::getInstance()->addSpriteFramesWithFile("fjbz.plist");
		auto frames = Util::getAnimation("fjbz(%d).png", 16);
		int tag = 6;
		for (int i = 0; i < 50; ++i)
		{
			auto delayAnimate = CallFunc::create(std::bind(&Aircraft::createBombAnimate, this, frames, tag, playerTag, sw, ew, sh, eh));
			auto dtime = DelayTime::create(random(0.0f, 1.0f));
			auto sequence = Sequence::create(dtime, delayAnimate, NULL);
			actBase->runAction(sequence);
		}
	}

	void Aircraft::createBombAnimate(Vector<SpriteFrame*> frames, int tag, int playerTag, float sw, float ew, float sh, float eh)
	{
		auto bsprite = new BombSprite();
		BombSprite::createWithSpriteFrame(bsprite, frames.front());
		bsprite->setScale(random(1.0f, 5.0f));
		bsprite->setPosition(Vec2(random(sw, ew), random(sh, eh)));
		auto body = Util::getPhysicsBody(bsprite->getBoundingBox().size, tag, bsprite->category, bsprite->collision, bsprite->contact, bsprite->gravityEnable);
		bsprite->setPhysicsBody(body);
		bsprite->setTag(tag);
		bsprite->playerTag = playerTag;
		auto animation = Animation::createWithSpriteFrames(frames, 1.0f / 16);
		auto animate = Animate::create(animation);
		auto repeat = Repeat::create(animate, 1);
		auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, bsprite));
		auto sequence = Sequence::create(repeat, actRuin, NULL);
		bsprite->runAction(sequence);
		float mtime = random(0.2f, 1.0f);
		Vec2 move = Vec2(0.0f, random(-100.0f, -50.0f));
		auto moveBy = MoveBy::create(mtime, move);
		bsprite->runAction(moveBy);
		actBase->addChild(bsprite, 3);
		AudioEngine::play2d("BOMB1.ogg", false, 0.5f);
	}

	/*发射炸弹类型2*/
	void Aircraft::launchBomb2()
	{
		Vec2 svec = sprite->getPosition();
		Size size = sprite->getBoundingBox().size;
		float sw = 20;
		float ew = actBase->vSize.width - 20;
		SpriteFrameCache::getInstance()->addSpriteFramesWithFile("zd02.plist");
		auto frames = Util::getAnimation("zd02(%d).png", 4);
		int tag = 6;
		for (int i = 0; i < 20; ++i)
		{
			auto bsprite = new BombSprite();
			BombSprite::createWithSpriteFrame(bsprite, frames.front());
			bsprite->setScale(random(2.0f, 3.0f));
			auto bsize = bsprite->getBoundingBox().size;
			float fx = random(sw, ew);
			float fy = random(-bsize.height * 4, -bsize.height * 3);
			bsprite->setPosition(Vec2(fx, fy));
			auto body = Util::getPhysicsBody(bsprite->getBoundingBox().size, tag, bsprite->category, bsprite->collision, bsprite->contact, bsprite->gravityEnable);
			bsprite->setPhysicsBody(body);
			bsprite->setTag(tag);
			bsprite->playerTag = sprite->playerTag;
			auto animation = Animation::createWithSpriteFrames(frames, 1.0f / 30);
			auto animate = Animate::create(animation);
			auto repeat = RepeatForever::create(animate);
			bsprite->runAction(repeat);
			auto moveTo = MoveTo::create(random(1.0f, 2.0f), Vec2(fx, actBase->vSize.height + size.height));
			auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, bsprite));
			auto sequence = Sequence::create(moveTo, actRuin, NULL);
			bsprite->runAction(sequence);
			actBase->addChild(bsprite, 3);
			AudioEngine::play2d("fg.ogg", false, 0.5f);
		}
	}

	void Aircraft::createRuinAnimate(Node* node)
	{
		AircraftSprite* aSprite = dynamic_cast<AircraftSprite*>(node);
		auto size = aSprite->getBoundingBox().size;
		auto position = node->getPosition();
		SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ysbz.plist");
		auto frames = Util::getAnimation("ysbz-%d.png", 16);
		auto sprite = Sprite::createWithSpriteFrame(frames.front());
		auto ssize = sprite->getContentSize();
		auto wf = size.width / ssize.width;
		auto hf = size.height / ssize.height;
		sprite->setScale(wf, hf);
		sprite->setPosition(position.x, position.y);
		auto animation = Animation::createWithSpriteFrames(frames, 1.0f / 16);
		auto animate = Animate::create(animation);
		auto repeat = Repeat::create(animate, 1);
		auto actRuin = CallFunc::create(std::bind(&Aircraft::removeRuin, this, sprite));
		auto sequence = Sequence::create(repeat, actRuin, NULL);
		playBOMB(aSprite);
		sprite->runAction(sequence);
		actBase->addChild(sprite);
	}

	void Aircraft::playBOMB(AircraftSprite* aSprite)
	{
		auto size = aSprite->getBoundingBox().size;
		float sz = size.height;
		if (size.width > size.height)
		{
			sz = size.width;
		}
		if (aSprite->airType == 0)
		{
			if (sz > 300)
			{
				AudioEngine::play2d("BOMB6.ogg", false, 1.0f);
			}
			else if (sz > 250)
			{
				AudioEngine::play2d("BOMB5.ogg", false, 1.0f);
			}
			else if (sz > 200)
			{
				AudioEngine::play2d("BOMB4.ogg", false, 1.0f);
			}
			else if (sz > 100)
			{
				AudioEngine::play2d("BOMB3.ogg", false, 1.0f);
			}
			else if (sz > 50)
			{
				AudioEngine::play2d("BOMB2.ogg", false, 1.0f);
			}
			else
			{
				AudioEngine::play2d("BOMB1.ogg", false, 1.0f);
			}
		}
	}

	void Aircraft::removeRuin(Sprite* sprite)
	{
		sprite->removeFromParentAndCleanup(true);
	}
}


飞机动画编码:Aircraft.h

#pragma once
#include <list>
#include <vector>
#include "cocos2d.h"
#include "audio/include/AudioEngine.h"
#include "Util.h"
#include "Control.h"
#include "AircraftSprite.h"
#include "BulletSprite.h"
#include "BombSprite.h"


using namespace std;
using namespace cocos2d;

namespace SpaceWar
{
	class ActionBase;

	class Aircraft
	{
	public:
		Aircraft();
		virtual ~Aircraft();

		ActionBase* actBase = nullptr;

		/*飞机精灵*/
		AircraftSprite* sprite = nullptr;

		/*玩家标识,0:玩家1,1:玩家2*/
		int playerTag = 0;

		/*游戏次数*/
		int playerCount = 5;

		/*得分*/
		int playerScore = 0;

		/*奖励游戏次数*/
		int rewardPlayerCount = 0;

		long baseTime = 0;

		/*初始化飞机*/
		virtual bool init(int pTag, int aType);

		void setCollisionEnable(bool isEnable);

		void controlAction(int player);

		void launchBullet();

		void launchBullet1();

		void createBullet1(PolygonInfo * pinfo, int tag, float speed, float startX);

		void launchBullet2();

		void createBullet2(PolygonInfo * pinfo, int tag, float speed, float startX, float endY);

		void launchMissile();

		void launchMissile1();

		void createMissile1(PolygonInfo * pinfo, int tag, float scale, int power, float speed, float startX, float startY);

		void launchMissile2();

		void createMissile2(PolygonInfo * pinfo, int tag, float scale, int power, float speed, float startX, float endX, float rotation);

		void launchBomb();

		void launchBomb1();

		void createBombAnimate(Vector<SpriteFrame*> frames, int tag, int playerTag, float sw, float ew, float sh, float eh);

		void launchBomb2();

		void createRuinAnimate(Node * node);

		void playBOMB(AircraftSprite * aSprite);

		void removeRuin(Sprite * sprite);

	};
}


C++ 与cocos2d-x-4.0完成太空飞机大战 (三)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值