现在只有hero一个在走来走去没什么意思,现在添加几个npc,给hero点压力,好的那就开始吧!添加enemy.h文件和enemy.h文件#ifndef _ENEMY_H_//66define _ENEMY_H_include"cocos2d.h"include"DBgame.h"define MaxBuf 20
using namespace cocos2d;
class Enemy:public CCNode
{
public:
Enemy(void);
~Enemy(void);
//静态方法用于创建hero实例
static Enemy monster(char monsterName);
void setAnimation(Direction dir,Behaviour be,char* monsterName);
Direction getDirection();
Behaviour getBehaviour();
void getCurrentAnimationIndex();
void boxAttacked(Direction dir,Behaviour be,int i);
void boxAttackedDone(CCNode* pSender,void *i);
void portal(Direction dir,Behaviour be,int i);
void portalDone(CCNode* pSender,void *i);
void cube(Direction dir,Behaviour be,int i);
void cubeDone(CCNode* pSender,void *i);
void executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName);
void executeDone(CCNode* pSender,void* i);
void deadAnimation(int id,char* monsterName);
void enemyDeadAnimation(int id,char* monsterName);
void enemyDeadAnimationDone();
void enemypDingDeadAnimation(int id,char* monsterName);
void enemypDingDeadAnimationDone();
void enemyWingDeadAnimation(int id,char* monsterName);
void enemyWingDeadAnimationDone();
void dingDead(int id,char* monsterName);
void dingDeadDone();
int getDeadID();
int id;
void pDingDead(int id,char* monsterName);
void pDingDeadDone();
int getpDingDeadID();
int pId;
int wID;
int getWingDeadID();
//void wingDead(int id,char* monsterName);
//void wingDeadDone();
CCRect getRect();
CCRect getCollisionRect(int width,int height);
Behaviour behaviour;
Direction direction;
void jump();
CCSprite* getMonsterSprite();
CCAnimate *getAnimate(char* monsterName); protected:CCAnimation* getAnimation();
CCSprite *sprite;
float xVel;
float yVel;
CCPoint touchPoint;
bool init(char* monsterName); };
define MAXLIFE 100
class EnemyData
{ public:
//yDing 42
EnemyData(void);
float getMonsterHeight(int i);
//void initMonsterHeightArray(int i);
void setMonsterHeight(float height,int i);
int getEnemyLife(int i);
void setEnemyLife(int life,int i);
float getActionTime(int i);
void setActionTime(float val,int i);
CCPoint getPositionMonster(int i);
void setPositionMonster(CCPoint pos,int i);
private:
//CCMutableArray<float> monsterHeight;
CCPoint monPosition[MaxBuf];
float actionTime[MaxBuf];
int enemyLife[MaxBuf];
float monsterHeightArray[MaxBuf];
};
endif//enemy.h文件#include"enemy.h"//497include"cocos2d.h"include"core\boy.h"include"DBgame.h"include"core\Animation.h"
using namespace cocos2d;
Enemy::Enemy()
{
xVel=0;
yVel=0;
}
Enemy::~Enemy()
{
}
Enemy* Enemy::monster(char monsterName)
{
Enemy *monster = new Enemy();
if (monster && monster->init(monsterName))
{
monster->autorelease();
return monster;
}
CC_SAFE_DELETE(monster);
return NULL; } bool Enemy::init(char monsterName)
{ bool bRet = false;
do{//printf("hello-------------\n");
this->setAnchorPoint(CCPointZero);
//创建动画
//sprite=CCSprite::spriteWithSpriteFrameName("yDing_0.png");
char charBuffer[128]="\0";
sprintf(charBuffer,"%s_0.png",monsterName);
//CCLOG("hello-------------%s",charBuffer);
sprite=CCSprite::spriteWithSpriteFrameName(charBuffer);//yDing,pDing
sprite->setAnchorPoint(CCPointZero);
this->addChild(sprite);
//设置状态
const char* str=charBuffer;
if(strcmp("box_0.png",str)==0)
{
//CCLOG("BOX------------------");
behaviour=MOVE;
direction=BOVERLOOK;
}
else if(strcmp("pDing_0.png",str)==0)
{
//CCLOG("BOX------------------");
behaviour=MOVE;
direction=MOVERLOOK;
}
else if(strcmp("wing_0.png",str)==0)
{
behaviour=MOVE;
direction=WLEFT;
}
else if(strcmp("yDing_0.png",str)==0)
{
behaviour=MOVE;
//behaviour=DEAD;
direction=MOVERLOOK;
//direction=REMOVE;
}
else if(strcmp("portal_0.png",str)==0)
{
behaviour=MOVE;
//behaviour=DEAD;
direction=PORTALONE;
sprite->setIsVisible(false);
//direction=REMOVE;
}
else if(strcmp("cube_0.png",str)==0)
{
behaviour=MOVE;
direction=CUBEONE;
}
//behaviour=MOVE;
//direction=MOVERLOOK;
//direction=MATTACK;
CCAction* action=CCRepeatForever::actionWithAction(getAnimate(monsterName));//monsterName ---"yDing pDing"
//sprite->runAction(CCRepeatForever::actionWithAction(getAnimate()));
sprite->runAction(action);
//
bRet=true;
}while(0);
return bRet; } void Enemy::getCurrentAnimationIndex()
{ /*
假设有一个CCAnimation anim,由5张图组成。
将这个anim包装成CCAnimate animate。使用的时侯sprite->runAction(animate),播放动画。
*/
//这里我们来获取第几帧:
int currentAnimIndex = 0; //精灵当前播放的是第几帧
CCAnimation anim=getAnimation();
for(int i = 1; i <4; i++)
{//CCLOG("animIndex");
//5张图5帧
if(sprite->displayedFrame() == anim->getFrames()->getObjectAtIndex(i))
{
//这个i返回的只是一个索引,如果帧数是从1开始计算就要+1
currentAnimIndex = i+1;
CCLOG("animaIndex:%d",currentAnimIndex);
}
} } CCSprite Enemy::getMonsterSprite()
{ return this->sprite;
} void Enemy::boxAttacked(Direction dir,Behaviour be,int i)
{ behaviour=be;
direction=dir;this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(
getAnimate("box"),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::boxAttackedDone),(void*)i),
NULL);
sprite->runAction(sequneceAction); }//--------------------2013.3.3 //下一关的通道有关函数
void Enemy::portal(Direction dir,Behaviour be,int i)
{ behaviour=be;
direction=dir;sprite->setIsVisible(true);
this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(
getAnimate("portal"),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::portalDone),(void*)i),
NULL);
sprite->runAction(sequneceAction); } void Enemy::portalDone(CCNode* pSender,void i)
{ int j=(int)i;
if(j==1)
{behaviour=MOVE;
direction=PORTALTWO;
}
sprite->stopAllActions();
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("portal"))); }//---------------------2013.3.3 //道具获得箱子的有关函数
void Enemy::cube(Direction dir,Behaviour be,int i)
{ behaviour=be;
direction=dir;sprite->setIsVisible(true);
this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(
getAnimate("cube"),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::cubeDone),(void*)i),
NULL);
sprite->runAction(sequneceAction); } void Enemy::cubeDone(CCNode pSender,void *i)
{ int j=(int)i;
if(j==1)
{behaviour=MOVE;
direction=CUBEONE;
}
else if(j==2)
{
behaviour=MOVE;
direction=CUBETWO;
}
sprite->stopAllActions();
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("cube")));
sprite->setIsVisible(false); } void Enemy::enemyDeadAnimation(int id,char monsterName)
{ behaviour=DEAD;
direction=REMOVE;
this->id=id;
this->cleanup();
sprite->cleanup();
CCAction sequneceAction = CCSequence::actions(getAnimate(monsterName),
CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyDeadAnimationDone)),
NULL);
sprite->runAction(sequneceAction); } void Enemy::enemyDeadAnimationDone()
{ sGlobal->enemyDeadAnimationDone(1,true);
} void Enemy::enemypDingDeadAnimation(int id,char* monsterName)
{ behaviour=DEAD;
direction=REMOVE;
this->pId=id;
this->cleanup();
sprite->cleanup();
CCAction sequneceAction = CCSequence::actions(getAnimate(monsterName),
CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemypDingDeadAnimationDone)),
NULL);
sprite->runAction(sequneceAction); } void Enemy::enemypDingDeadAnimationDone()
{ sGlobal->enemyDeadAnimationDone(2,true);
} void Enemy::enemyWingDeadAnimation(int id,char monsterName)
{ behaviour=DEAD;
direction=WREMOVE;
this->wID=id;
this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(getAnimate(monsterName),
CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::enemyWingDeadAnimationDone)),
NULL);
sprite->runAction(sequneceAction); } void Enemy::enemyWingDeadAnimationDone()
{ sGlobal->enemyDeadAnimationDone(3,true);
} int Enemy::getWingDeadID()
{ return this->wID;
} void Enemy::deadAnimation(int id,char monsterName)
{ behaviour=DEAD;
direction=REMOVE;
this->id=id;
sprite->stopAllActions();
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
} void Enemy::dingDead(int id,char monsterName)
{ behaviour=DEAD;
direction=REMOVE;
this->id=id;
this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(getAnimate(monsterName),
CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::dingDeadDone)),
NULL);
sprite->runAction(sequneceAction); } void Enemy::pDingDead(int id,char monsterName)
{ behaviour=DEAD;
direction=REMOVE;
this->pId=id;
this->cleanup();
sprite->cleanup();
CCAction sequneceAction = CCSequence::actions(getAnimate(monsterName),
CCCallFunc::actionWithTarget(this, callfunc_selector(Enemy::pDingDeadDone)),
NULL);
sprite->runAction(sequneceAction); } int Enemy::getpDingDeadID()
{ return this->pId;
} int Enemy::getDeadID()
{ return this->id;
} void Enemy::dingDeadDone()
{ //behaviour=DEAD;
//direction=REMOVE;
//sprite->stopAllActions();
//运动的算法
CCPoint pos=this->getMonsterSprite()->getPosition();
//sprite->stopAllActions();
//sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
sprite->runAction(CCMoveTo::actionWithDuration(1, CCPointMake(pos.x+40,pos.y)));
//sprite->setIsVisible(false);
} void Enemy::pDingDeadDone()
{ //
} void Enemy::boxAttackedDone(CCNode* pSender,void i)
{ int j=(int)i;
if(j==1)
{behaviour=MOVE;
direction=BONE;
}
else if(j==2)
{
behaviour=MOVE;
direction=BTWO;
}
else if(j==3)
{
behaviour=MOVE;
direction=BTHREE;
}
sprite->stopAllActions();
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("box"))); } void Enemy::setAnimation(Direction dir,Behaviour be,char monsterName)
{ direction=dir;
behaviour=be;
sprite->stopAllActions();
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate(monsterName)));
} //1 2 3 yDing 11 12 13 pDing
void Enemy::executeAnimation(Direction dir,Behaviour be,int nextAni,char* monsterName)
{ behaviour=be;
direction=dir;this->cleanup();
sprite->cleanup();
CCAction *sequneceAction = CCSequence::actions(
getAnimate(monsterName),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(Enemy::executeDone),(void*)nextAni),
NULL);
sprite->runAction(sequneceAction); } void Enemy::executeDone(CCNode pSender,void i)
{ int next=(int)i;
if(next==1)
{behaviour=MOVE;
direction=MOVERLOOK;
}
else if(next==2)
{
behaviour=MOVE;
direction=MATTACK;
}
else if(next==3)
{
behaviour=DEAD;
direction=REMOVE;
}
else if(next==11)
{
behaviour=MOVE;
direction=MOVERLOOK;
}
else if(next==12)
{
behaviour=MOVE;
direction=MATTACK;
}
else if(next==13)
{
behaviour=DEAD;
direction=REMOVE;
}
else if(next==21)
{
behaviour=MOVE;
direction=WRIGHT;
}
else if(next=22)
{
behaviour=MOVE;
direction=WLEFT;
}
else if(next==23)
{
behaviour=MOVE;
direction=WATTACK;
}
else if(next=24)
{
behaviour=DEAD;
direction=WREMOVE;
}
sprite->stopAllActions();
if(next<20&&next>10)
{
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("pDing")));
}
else if(next<10)
{
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("yDing")));
}
else if(next>20)
{
sprite->runAction(CCRepeatForever::actionWithAction(getAnimate("wing")));
} } //注意,不同精灵,这个函数写法不同,主要区别都是OVERLOOK上,有些精灵是没有四方向的
CCAnimate * Enemy::getAnimate(char monsterName)
{ if (behaviour==DEAD)
{//direction=MOVERLOOK;
}
return AnimationManager::getInstance()->getAnimate(monsterName,behaviour,direction); } CCAnimation Enemy::getAnimation()
{ if (behaviour==DEAD)
{//direction=MOVERLOOK;
}
return AnimationManager::getInstance()->getAnimation("yDing",behaviour,direction); } Direction Enemy::getDirection()
{ return this->direction;
} Behaviour Enemy::getBehaviour()
{ return this->behaviour;
} CCRect Enemy::getRect()
{ float x=sprite->getPosition().x;
float y=sprite->getPosition().y;
//printf("x:%f y:%f\n",x,y);
return CCRectMake(x,y,70,62); } CCRect Enemy::getCollisionRect(int width,int height)
{ float x=sprite->getPosition().x;
float y=sprite->getPosition().y;
//CCLOG("Enemy--x:%f y:%f width:%d height:%d\n",x,y,width,height);
return CCRectMake(x,y,width,height);
} void Enemy::jump()
{ this->sprite->runAction(CCJumpTo::actionWithDuration(0.05f,ccp(this->sprite->getPosition().x , this->sprite->getPosition().y),20,100));
} EnemyData::EnemyData(void)
{ for(int i=0;i<MaxBuf;i++)
{enemyLife[i]=MAXLIFE;
monsterHeightArray[i]=2.5;
actionTime[i]=0.0;
monPosition[i]=CCPointZero;
}
//monsterHeight=new CCMutableArray<float*>(); } int EnemyData::getEnemyLife(int i)
{ return this->enemyLife; } void EnemyData::setEnemyLife(int life,int i)
{ this->enemyLife=life;
} float EnemyData::getMonsterHeight(int i)
{ return this->monsterHeightArray;
} void EnemyData::setMonsterHeight(float height,int i)
{ this->monsterHeightArray=height;
} void EnemyData::setActionTime(float val,int i)
{ this->actionTime=val;
} float EnemyData::getActionTime(int i)
{ return this->actionTime;
} void EnemyData::setPositionMonster(CCPoint pos,int i)
{ this->monPosition=pos;
} CCPoint EnemyData::getPositionMonster(int i)
{ return this->monPosition;
}现在可以在scene中添加npc了,说到就开始。Global.h中添加如下定义:CCMutableArray<Enemy*> enemyArray;
void addMonster(); Global.cpp中实现:CCMutableArray<CCStringToStringDictionary > * all = data->getObjects();
do
{
for (int i = 0 ; i <all->count(); i++)
{
CCStringToStringDictionary * temp = all->getObjectAtIndex(i);
if(temp->objectForKey("name")->toStdString() == std::string("y"))
{
float x = temp->objectForKey("x")->toFloat();
float y = temp->objectForKey("y")->toFloat();
CCVector2d* vector=new CCVector2d();
vector->x=x;
vector->y=5;
monsterPositionArray->addObject(vector);
Enemy* monster = Enemy::monster("yDing");
monster->getMonsterSprite()->setPosition(ccp(x,5));
map->addChild(monster,map->getChildren()->count());
enemyArray->addObject(monster);//yDing
} }
HelloworldScene.cpp中的init()中添加如下sGlobal->addMonster(); 下一篇将会讲解enemy的群集运动以及配合动画,以及hero的碰撞检测准备。待续。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。long原创
|