[Cocos2d塔防游戏开发]Cocos2dx-3.X完成塔防游戏《王国保卫战》--防御塔(三)之初级炮塔

该章节主要介绍初级炮塔

与箭塔相比,箭塔一共需要只需要1-2张图片,除了弓箭手,塔是静止的,而炮塔相对比较复杂


从图中我们可以看出,炮塔的动作序列比较复杂,所以只需要将一个个动画序列分清楚,好在我们用的现成的图片资源,只要一个个通过addchild添加进去即可,然后用动画序列播放。

首先重载shoot

void BaseArtilleryTower::shoot(float dt)
{
    checkNearestMonster();
    if(nearestMonster!=NULL && nearestMonster->getCurrHp() > 0)
    {
	auto firePosition = nearestMonster->baseSprite->getPosition() - this->getParent()->getPosition();
	runAction(Sequence::create(
		CallFuncN::create(CC_CALLBACK_0(BaseArtilleryTower::fireAnimation,this)),
		CallFuncN::create(CC_CALLBACK_0(BaseArtilleryTower::fire,this,firePosition))
		,NULL));
    }
}
当射程范围内有敌人时,按顺序执行这个序列

先是左边的炮手蹲下并上抛这个动作,然后是炮弹飞到炮筒的动画

void BaseArtilleryTower::filledAnimation()
{
    leftShooter->runAction(Animate::create(AnimationCache::getInstance()->getAnimation(getName()+"leftShooter_throw")));
    c4->runAction(Animate::create(AnimationCache::getInstance()->getAnimation(getName()+"c4")));
}
之后执行fire将炮弹发射出去,基本原理和弓箭塔差不多,并且简单不少

void BaseArtilleryTower::fire(Point firePosition)
{
	auto currBullet = ArtilleryTowerBullet();

	auto shootVector = firePosition;

	Point highPoint = Point(shootVector.x,shootVector.y+200);

		
	ccBezierConfig bezier;

		
	bezier.controlPoint_1 = Point(currBullet->getPosition().x,currBullet->getPosition().y+200); 
	bezier.controlPoint_2 = Point(shootVector.x,shootVector.y+200);; 
	bezier.endPosition = shootVector;
	float endRotate;
	if(shootVector.x>currBullet->getPosition().x)
		endRotate = 180.0f;
	else
		endRotate = -180.0f;
	auto action = Spawn::create(BezierTo::create(1.0f, bezier),RotateTo::create(1.0f,endRotate),NULL);
	currBullet->setBulletAction(action);
	currBullet->shoot();
	runAction(Sequence::create(DelayTime::create(1.0f),
		CallFuncN::create(CC_CALLBACK_0(BaseArtilleryTower::filledAnimation,this)),
		NULL));
	currBullet = NULL;
}

filledAnimation是右边的炮手和炮筒的动画这里就省略了


其中炮弹的动画与弓箭的动画区别就是旋转角度是转一圈,其他区别不大~


因为比较简单,这章就粗略介绍下


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值