飞机游戏(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);  //文件路径,是否循环播放,播放音量
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值