[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是右邊的炮手和炮筒的動畫這裏就省略了


其中炮彈的動畫與弓箭的動畫區別就是旋轉角度是轉一圈,其他區別不大~


因爲比較簡單,這章就粗略介紹下


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章