Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please more advice, if you feel good please support a lot of praise. Thank you! Hopy;)
The previous chapter introduced the game protagonist's initialization method, the following we once to implement the protagonist's other methods, first of all to see the Runanimation method, we use this method to play the protagonist's animation:
-(void)runAnimation:(CCAnimation*)animation{ if (_curAnimation == animation) { return; } _curAnimation = animation; if (_curAnimate) { [self stopAction:_curAnimate]; } _curAnimate = [CCActionRepeatForever actionWithAction: [CCActionAnimate actionWithAnimation:animation]]; [self runAction:_curAnimate];}
The code first checks to see if the animation that will play is the same as the animation that is currently playing, and exits if the same. Then if the animation action is currently playing, stop it.
Next, create a permanent loop action for the animation you want to play, and then run the action.
Is the player controls the game protagonist in the scene freely walk the various animations, the final protagonist meets the monster hangs off the animation also so.
There is also an important method in the protagonist class, which should include all the monsters in the game need one method, that is, a * mobile algorithm. Small partners unfamiliar with a * algorithm can be found in my translation and original * Series blog:
Original: How to implement A * pathfinding algorithm in cocos2d game (i.)
Translation: Introduction to A * pathfinding algorithm (i)
This method is the Movetowardbyastar method:
//Use a * pathfinding algorithm to move to target coordinates-(void) Movetowardbyastar: (cgpoint) targetlocation{if(_currentstepaction) {_pendingmove = [nsvalue valuewithcgpoint:targetlocation];return; } cgpoint Fromtilecoord = [_mainscene tileCoordForPosition:self.position]; Cgpoint Totilecoord = [_mainscene tilecoordforposition:targetlocation];if(Cgpointequaltopoint (Fromtilecoord, Totilecoord)) {return; }if(! [_mainscene Iswalkabletile:totilecoord forrole:self]) {return; } _spopensteps = [Nsmutablearray array]; _spclosedsteps = [Nsmutablearray array]; _shortestpath = nil; [Stara Insertstep:[[shortestpathstep alloc] Initwithposition:fromtilecoord] Toopenlist:_spopensteps]; Do{Shortestpathstep *currentstep = _spopensteps[0]; [_spclosedsteps Addobject:currentstep]; [_spopensteps Removeobjectatindex:0];if(Cgpointequaltopoint (Currentstep.position, Totilecoord)) {//If you have found the shortest path, complete the move action for the shortest path[Self constructpathandstartanimationfromstep:currentstep]; _spopensteps = nil; _spclosedsteps = nil; Break; } nsarray *adjsteps = [_mainscene Walkableadjacenttilescoorddiagonallyfortilecoord: Currentstep.position Forrole:self]; for(Nsvalue *vinchAdjsteps) {Shortestpathstep *Step= [[Shortestpathstep alloc]initwithposition:[v Cgpointvalue]];if([_spclosedsteps Containsobject:Step]) {Continue; }intMovecost = [Stara costtomovediagonallyfromstep:currentstep toadjacentstep:Step]; Nsuinteger index = [_spopensteps indexofobject:Step];if(index = = nsnotfound) {Step. parent = Currentstep;Step. Gscore = Currentstep.gscore + movecost;Step. Hscore = [Stara Computehscorefromcoord:Step. Position Tocoord:totilecoord]; [Stara Insertstep:StepToopenlist:_spopensteps]; }Else{//already exists in open list Step= _spopensteps[index];if((Currentstep.gscore + movecost) <Step. Gscore) {Step. Gscore = Currentstep.gscore + movecost;Step. parent = Currentstep; [_spopensteps Removeobjectatindex:index]; [Stara Insertstep:StepToopenlist:_spopensteps]; } } } } while(_spopensteps.count >0);}
Which is longer, here is not detailed, we need information on the above 2 series of blog posts completely covered. One thing to point out is that since I started writing this kind of code, it's not too strong to grasp the class elements at first, which is supposed to be what all game characters need, So it should be in their parent class. Yes, I did it in another RPG << panda Soul >> and we have a chance to do that again.
(no.00005) iOS implementation bomber game (ix): the protagonist of the Game (ii)