飛機遊戲(2)

今天對飛機遊戲的BOSS加入運動動畫(左右搖擺),用的是cocos內部的API中的MoveTo和MoveBy對它進行位移。
<span style="white-space:pre">	</span>auto enemy = Sprite::create("e_b_01.png");
	enemy->setPosition(Vec2(visibleSize.width / 2, visibleSize.height/1.2));
	auto move = MoveTo::create(3, Vec2(350, visibleSize.height / 1.2));
	auto enemy1_go1 = EaseInOut::create(move->clone(), 0.65f);
	auto enemy1_back1 = MoveBy::create(3, Vec2(-220, 0));
	this->addChild(enemy);
	auto delay = DelayTime::create(0.25f);
	auto seq1 = Sequence::create(enemy1_go1, delay, enemy1_back1, delay->clone(), nullptr);
	enemy->runAction(RepeatForever::create(seq1));
後,主要的是己方飛機的移動:用上cocos裏對方塊API進行移動:
bool Hero::init()
{
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto hero = Sprite::create("e_13.png");
	hero->setPosition(Vec2(visibleSize.width / 2,0));		//設置飛機初始座標
	hero->runAction(Sequence::create(MoveBy::create(1, Vec2(0, visibleSize.height /5)), ScaleTo::create(2, 5), ScaleTo::create(2, 1), nullptr));	//飛機出場特效
	addChild(hero);
	/*下面都是飛機的點擊狀態*/
	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);

	listener1->onTouchBegan = [](Touch* touch, Event* event){
		auto target = static_cast<Sprite*>(event->getCurrentTarget());

		Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
		Size s = target->getContentSize();
		Rect rect = Rect(0, 0, s.width, s.height);

		if (rect.containsPoint(locationInNode))
		{
			return true;
		}
		return false;
	};

	listener1->onTouchMoved = [](Touch* touch, Event* event){
		Size visibleSize = Director::getInstance()->getVisibleSize();		//	全都要這兩句獲取場景大小
		Vec2 origin = Director::getInstance()->getVisibleOrigin();

		auto target = static_cast<Sprite*>(event->getCurrentTarget());
		target->setPosition(target->getPosition() + touch->getDelta());

		/*X軸邊界*/
		if (target->getPositionX() + touch->getDelta().x >= visibleSize.width - target->getContentSize().width / 2){
			target->setPositionX(visibleSize.width - target->getContentSize().width / 2);
		}

		else if (target->getPositionX() + touch->getDelta().x <= target->getContentSize().width / 2){
			target->setPositionX(target->getContentSize().width / 2);
		}

		else{
			target->setPositionX(target->getPositionX() + touch->getDelta().x);
		}

		/*Y軸邊界*/
		if (target->getPositionY() + touch->getDelta().y >= visibleSize.height - target->getContentSize().height / 2){
			target->setPositionY(visibleSize.height - target->getContentSize().height / 2);
		}

		else if (target->getPositionY() + touch->getDelta().y <= target->getContentSize().height / 2){
			target->setPositionY(target->getContentSize().height / 2);
		}

		else{
			target->setPositionY(target->getPosition().y + touch->getDelta().y);
		}
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, hero);

	return false;
}

裏面的X軸Y軸加入了場景邊界的限制,讓飛機不能拖出場景之外。

另外加入了飛機的子彈動畫和子彈音效:

#include "HelloWorldScene.h"
#include "HeroBullet.h"
#include "AudioEngine.h"
using namespace std;
using namespace cocos2d::experimental;
USING_NS_CC;

HeroBullet::HeroBullet(){
	this->schedule(CC_SCHEDULE_SELECTOR(HeroBullet::autoremove), 0.1f);
}
 
void HeroBullet::autoremove(float dt){
	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	auto bullet = Sprite::create("blt_09.png");
	bullet->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 6));
	this->addChild(bullet);
	auto actionTo = MoveBy::create(3, Point(0, visibleSize.height));
	bullet->runAction(actionTo);
	//加載子彈聲
	int _audioID = AudioEngine::play2d("biu.mp3", false, 0.2);  //文件路徑,是否循環播放,播放音量
}

發佈了50 篇原創文章 · 獲贊 39 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章