与主角英雄类似,敌人狗博士的实现也差不多,但是狗博士没有分成多部分实现动态效果,所以构造就想对简单一点。同时我们加上了狗博士死亡时炸弹的爆炸效果图,同理也是通过几张渐变的图连续播放出来显示的爆炸效果。上代码先:
GameObjEnemy.h头文件
#ifndef _GAME_OBJ_ENEMY_H__
#define _GAME_OBJ_ENEMY_H__
#include "cocos2d.h"
using namespace cocos2d;
class GameObjEnemy : public CCNode
{
public:
CCSprite *boom;
CCSprite *mainbody;
GameObjEnemy(void);
virtual ~GameObjEnemy(void);
void releasebullet();
virtual void onEnter();
virtual void onExit();
void movestart();
void restart();
void setdie();
short type;
bool islife;
void setType(short var);
};
#endif
GameObjEnemy.cpp文件
#include "GameObjEnemy.h"
GameObjEnemy::GameObjEnemy(void)
{
}
GameObjEnemy::~GameObjEnemy(void)
{
}
void GameObjEnemy::onEnter()
{
CCNode::onEnter();
this->setContentSize(CCSizeMake(99 , 111));
mainbody = CCSprite::create("DrDog1.png");
//初始化动画
CCAnimation* animation = CCAnimation::create();
animation->addSpriteFrameWithFileName("DrDog1.png");
animation->addSpriteFrameWithFileName("DrDog2.png");
animation->setDelayPerUnit(0.1f);
animation->setRestoreOriginalFrame(true);
mainbody->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
addChild(mainbody);
boom =CCSprite::create("Boom1.png");
addChild(boom);
boom->setVisible(false); //设置不可见
islife = true;
}
//死亡
void GameObjEnemy::setdie()
{
islife = false;
mainbody->setVisible(false);
boom->setVisible(true);
this->stopAllActions();
//爆炸动画
CCAnimation* boomAnimation = CCAnimation::create();
boomAnimation->addSpriteFrameWithFileName("Boom1.png");
boomAnimation->addSpriteFrameWithFileName("Boom2.png");
boomAnimation->addSpriteFrameWithFileName("Boom3.png");
boomAnimation->addSpriteFrameWithFileName("Boom4.png");
boomAnimation->addSpriteFrameWithFileName("Boom5.png");
boomAnimation->setDelayPerUnit(0.1f);
boomAnimation->setRestoreOriginalFrame(true);
boom->runAction(CCSequence::create(CCAnimate::create(boomAnimation) , CCCallFuncN::create(this ,callfuncN_selector(GameObjEnemy::restart)) ,NULL));
}
//重置
void GameObjEnemy::restart()
{
mainbody->setVisible(true);
boom->setVisible(false);
CCSize size = CCDirector::sharedDirector()->getWinSize();
this->setPosition(ccp(size.width/4 * type , size.height + 50));
islife = true;
mainbody->setVisible(true);
boom->setVisible(false);
this->movestart();
}
//移动
void GameObjEnemy::movestart()
{
islife = true;
int type = CCRANDOM_0_1()*4;
//贝塞尔曲线移动
ccBezierConfig bezier2;
bezier2.controlPoint_1 = CCPointMake(this->getPosition().x - 400 , 330);
bezier2.controlPoint_2 = CCPointMake(this->getPosition().x +400 , 280);
bezier2.endPosition = CCPointMake(this->getPosition().x , -70);
CCBezierTo *bezierBy2 = CCBezierTo::create(6 , bezier2);
ccBezierConfig bezier1;
bezier1.controlPoint_1 = CCPointMake(this->getPosition().x + 400 , 330);
bezier1.controlPoint_2 = CCPointMake(this->getPosition().x -400 , 280);
bezier1.endPosition = CCPointMake(this->getPosition().x , -70);
CCBezierTo *bezierBy1 = CCBezierTo::create(6 , bezier1);
switch(type)
{
case 0:
case 1:
this->runAction(CCSequence::create(CCMoveBy::create(6 , ccp(0 , -600)) ,CCCallFuncN::create(this ,callfuncN_selector(GameObjEnemy::restart)) , NULL));
break;
case 2:
this->runAction(CCSequence::create(bezierBy2 , CCCallFuncN::create(this ,callfuncN_selector(GameObjEnemy::restart)), NULL));
break;
case 3:
this->runAction(CCSequence::create(bezierBy1 ,CCCallFuncN::create(this , callfuncN_selector(GameObjEnemy::restart)), NULL));
break;
default:
break;
}
schedule(schedule_selector(GameObjEnemy::releasebullet), 1.2f);
}
首先创建了狗博士精灵对象,然后定义了动画CCAnimation,主体部分运行动画。接着定义了爆炸精灵对象,在狗博士处于正常装饰,将爆炸精灵设置为不可见;在狗博士处于死亡状态时,其主体部分设置为不可见,爆炸部分设置为可见并运行爆炸动画。其实现在setdie函数中。在运行完爆炸动画后,会重新调用函数restart, 该函数重新设置位置,将主干部分设置为可见,将爆炸部分设置为不可见,然后调用movestar函数设置移动的方式。其中movestart设置了三种移动方式,包括两种曲线移动方式和一种直线运动方式。三个狗博士随机选择移动方式就会产生不同的组队效果。
知识点:
1.setRestoreOriginalFrame(true) : 设置播放完毕后恢复精灵原始帧;
2.stopAllActions():Stops and removes all actions from the running action list.
3.CCCallFuncN : 用来执行对应的回调函数,其中回调函数带一个参数。callfuncN_selector是用来选择回调哪个函数。即当该函数调用完后会重新调用restart函数。
4.CCSequence是用来按顺序执行一系列的动作,即动作按排列的顺序一个接一个的执行.在这里,creat中的三个参数是依次执行的,即播放完爆炸的动画后执行回调restart函数。
5.getWinSize():获取当前屏幕的大小。
6.贝塞尔曲线移动:CCBezierTo::create("时间","ccBezierConfig构造体") ,几秒内按指定贝塞尔曲线运动。详细参考下面链接:
http://xiandanboke.com.cn/cocos2d-xccbezierbyccbezierto.html
http://xiandanboke.com.cn/cocos2d-xbezier.html