Enemy类没什么内容,就create和init方法,根据参数来创建不同的敌机,头文件代码如下:
//飞机的类型
enum planeType {smallPlane, midPlane, bigPlane}; class Enemy : public CCSprite
{ public: void loseLife(); CC_SYNTHESIZE(float, m_speed, Speed);
CC_SYNTHESIZE(int, m_life, Life);
CC_SYNTHESIZE(int, m_score, Score);
CC_SYNTHESIZE(planeType, m_type, Type); ~Enemy();
virtual bool init(planeType type, CCTexture2D* texture);
static Enemy* create(planeType type, CCTexture2D* texture); };
create和init方法实现如下:
Enemy* Enemy::create(planeType type, CCTexture2D* texture)
{
Enemy *pRet = new Enemy();
if (pRet && pRet->init(type, texture))
{
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = NULL;
return NULL;
}
} bool Enemy::init(planeType type, CCTexture2D* texture)
{
bool bRet = false;
do
{
const char* fileName;
float score = ;
int life = ;
float speed = ; int random_speed = rand() % + ; switch (type)
{
case bigPlane:
fileName = "enemy3_n1.png";
life = ;
score = ;
break; case midPlane:
fileName = "enemy2.png";
life = ;
score = ;
break; case smallPlane:
fileName = "enemy1.png";
life = ;
score = ;
break; default:
break;
}
;
CC_BREAK_IF(!CCSprite::initWithSpriteFrameName(fileName)); this->m_score = score;
this->m_life = life;
this->m_speed = *random_speed;
this->m_type = type;
bRet = true; } while (); return bRet;
}
EnemyLayer类则负责敌机除了创建之外的所有工作,包括开始启动创建方法、停止创建、移除敌机、敌机爆炸等。头文件:
class EnemyLayer : public cocos2d::CCLayer
{ protected: CCSize designResolutionSize;
CCSpriteBatchNode* batchNode; public: CCArray *m_enemys; EnemyLayer(void);
~EnemyLayer(void);
virtual bool init(); void startTakeEnemys(float dt);
void stopTakeEnemy(void); void EnemyMoveToFinish(CCNode* pSender);
void removeEnemy(CCNode* pTarget, void* enemy);
void removeAllEnmeys(); void addEnemy(float dt); void bomb(CCNode* pSender); CREATE_FUNC(EnemyLayer);
};
敌机的创建和子弹不同,还记得子弹的创建我是保证屏幕上只有一粒子弹的吧,敌机则不然,固定每隔多少时间就会创建
void EnemyLayer::startTakeEnemys(float dt)
{ float delay = 1.0f;//多少秒后调用
float interval = 0.3f;//调用时间间隔
this->schedule(schedule_selector(EnemyLayer::addEnemy), interval, kCCRepeatForever, delay);
}
addEnemy和bomb方法实现:
void EnemyLayer::addEnemy(float dt)
{
if (true)
{
int seed = ;
int random_planeType = rand() % seed + ;//1~19
planeType type; Enemy* enemy = NULL; //按概率随机生成大中小三种敌机,或者不产生
//big
if (random_planeType % == )
{
type = bigPlane;
SimpleAudioEngine::sharedEngine()->playEffect("music/big_spaceship_flying.wav",false);
}
//mid
else if(random_planeType % == )
{
type = midPlane;
}
//small
else if(random_planeType % == )
{
type = smallPlane;
}
// no
else
{
return;
} enemy = Enemy::create(type, this->batchNode->getTexture()); //初始位置(随机)
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
float x = rand() % (int)(winSize.width - (int)enemy->boundingBox().size.width * ) + enemy->boundingBox().size.width;
float y = winSize.height - enemy->boundingBox().size.height/; CCPoint position = ccp(x,y); enemy->setPosition(position);
this->m_enemys->addObject(enemy);
batchNode->addChild(enemy); //敌机的运动轨迹CCDirector::sharedDirector()->getWinSize()
float length = enemy->getContentSize().height + designResolutionSize.height;//飞行距离,超出屏幕即结束
float velocity = /;//飞行速度:420pixel/sec
float realMoveDuration = length/enemy->getSpeed();//飞行时间 CCFiniteTimeAction* actionMove = CCMoveTo::create(realMoveDuration,ccp(x, -enemy->getContentSize().height));
CCFiniteTimeAction* actionDone = CCCallFuncN::create(this,callfuncN_selector(EnemyLayer::EnemyMoveToFinish));//回调一个敌机结束处理函数 CCSequence* sequence = CCSequence::create(actionMove,actionDone,NULL); enemy->runAction(sequence); } } void EnemyLayer::bomb(CCNode* pSender)
{ CCAnimation *animationBomb = NULL;
Enemy *enemy = (Enemy*) pSender;
char bombMusciName[]; if (enemy->getType() == bigPlane)
{
sprintf(bombMusciName, "music/enemy3_down.wav");
animationBomb = CCAnimationCache::sharedAnimationCache()->animationByName("animation_Enemy3Bomb");
}
else if(enemy->getType() == midPlane)
{
sprintf(bombMusciName, "music/enemy2_down.wav");
animationBomb = CCAnimationCache::sharedAnimationCache()->animationByName("animation_Enemy2Bomb");
}
else if(enemy->getType() == smallPlane)
{
sprintf(bombMusciName, "music/enemy1_down.wav");
animationBomb = CCAnimationCache::sharedAnimationCache()->animationByName("animation_Enemy1Bomb");
} SimpleAudioEngine::sharedEngine()->playEffect(bombMusciName, false);//开始播放背景音效,false表示不循环 CCActionInterval *action = CCScaleTo::create(0.5f, pSender->getScale() + 0.05f);
CCCallFuncND *funcND = CCCallFuncND::create(this,callfuncND_selector(EnemyLayer::removeEnemy),(void*)pSender);
CCFiniteTimeAction* seq = CCSequence::create(CCAnimate::create(animationBomb), funcND, NULL); pSender->stopAllActions();
pSender->runAction(seq); }
可以看得出,这两个方法其实放到Enemy类里也很正常完全没问题的,这个要看coder想怎么写了。另外,敌机飞出屏幕外时的删除和被子弹打中时爆炸的回收是不同,差一个爆炸效果,而子弹在这两种情况的处理是一样的。