Now, only one hero is walking around. Now, add several NPCs to put pressure on hero. Okay, let's get started!
Add an enemy. h file and an enemy. h file
# Ifndef _ ENEMY_H _ // 66 # define _ ENEMY_H _ # include "cocos2d. h "# include" DBgame. h "# define MaxBuf 20 using namespace cocos2d; class Enemy: public CCNode {public: Enemy (void );~ Enemy (void); // The static method is used to create the hero instance 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 Merge (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 Merge (); void enemypDingDeadAnimation (int id, char * monsterName); void enemypDingDeadAnimationDone (); void enemyWingDeadAnimation (int id, char * monsterName); void quarter (); 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 ction; void jump (); CCSprite * extract (); 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 file
# Include "enemy. h "// 497 # include" 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); // create an animation // 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 ); // set the status const char * str = charBuffer; if (strcmp ("box_0.png", str) = 0) {// CCLOG ("BOX ----------------"); behaviour = MOVE; direction = BOVERLOOK;} else if (strc Mp ("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-> setI SVisible (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;} whil E (0); return bRet;} void Enemy: getCurrentAnimationIndex () {/* assume that there is a CCAnimation * anim, which consists of five graphs. Package this anim into CCAnimate * animate. When using sprite-> runAction (animate), play the animation. * /// Here we get the frame number: int currentAnimIndex = 0; // The number of frames currently played by the genie CCAnimation * anim = getAnimation (); for (int I = 1; I <4; I ++) {// CCLOG ("animIndex"); // 5 images 5 frames if (sprite-> displayedFrame () = anim-> getFrames ()-> getObjectAtIndex (I) {// This I returns only one index, if the number of frames starts from 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 ---------------------------- // The channel-related function void Enemy in the following section :: 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 functions // obtain the void Enemy function of the box: cube (Direction dir, Behaviour, 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: Role), NULL); sprite-> runAction (sequneceAction);} void Enemy :: dependencies () {sGlobal-> enemyDeadAnimationDone (1, true);} void Enemy: enemypDingDeadAnimation (int id, char * monsterName) {behaviour = DEAD; ction = REMOVE; this-> pId = id; this-> cleanup (); sprite-> cleanup (); CCAction * sequneceAction = CCSequence: actions (getAnimate (monsterName), CCCallFunc :: actionWithTarget (this, callfunc_selector (Enemy: Role), NULL); sprite-> runAction (sequneceAction);} void Enemy: Role () {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: Role), 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 (); // The motion algorithm 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 pDingvoid 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") ;}// note that different genie use different methods to write this function. The main difference is OVERLOOK, some GPCs do not have a four-direction 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 [I];} void EnemyData: setEnemyLife (int life, int I) {this-> enemyLife [I] = life ;} float EnemyData: getMonsterHeight (int I) {return this-> monsterHeightArray [I];} void EnemyData: setMonsterHeight (float height, int I) {this-> monsterHeightArray [I] = height;} void EnemyData: setActionTime (float val, int I) {this-> actionTime [I] = val;} float EnemyData :: getActionTime (int I) {return this-> actionTime [I];} void EnemyData: setPositionMonster (CCPoint pos, int I) {this-> monPosition [I] = pos ;} CCPoint EnemyData: getPositionMonster (int I) {return this-> monPosition [I];}
Now you can add an npc in scene.
Add the following definition to Global. h:
CCMutableArray<Enemy*> *enemyArray;void addMonster();
Implementation in 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 }}
Add the following content to init () in HelloworldScene. cpp:
sGlobal->addMonster();
In the next article, we will explain the group movement of enemy and the combination of animation and hero collision detection preparation.
To be continued ....................................... .......................
This article is from the "Cocos2D-X" blog, please be sure to keep this source http://lonag.blog.51cto.com/3340984/1215454