(No. 00005) iOS pop-up player game (9): Game protagonist (2)

Source: Internet
Author: User

(No. 00005) iOS pop-up player game (9): Game protagonist (2)

The original or translated works of the giant panda and pig hope. You are welcome to reprint them. Please indicate the source for reprinting.
If you think it is not easy to write, Please give more comments. If you think it is good, please give more support. Thank you! Hopy ;)

The previous article introduced the initialization method of the main character of the game. Next we will implement other methods of the main character. First, let's take a look at the runAnimation method. We will use this method to play the animation of the main character:

-(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 whether the animation to be played is the same as the animation currently being played. If the animation is the same, exit directly. Then, if the animation is being played, stop it.

Next, create a permanent loop action for the animation to be played, and then run the action.

Expires + expires/MCouMPTzs + 31tDL + expires + zcrHQSq1xNLGtq/L47eoLrK7yuzPpEEqy + expires + DQo8cD64w7e9t6i + expires "brush: java;">// Use the * search algorithm to move to the target coordinate-(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 = [empty array]; _ blank = [NSMutableArray array]; _ shortestPath = nil; [StarA insertStep: [[Export alloc] initWithPosition: Counter] toOpenList: _ spOpenSteps]; do {ShortestPathStep * currentStep = _ spOpenSteps [0]; [_ export addObject: currentStep]; [_ spOpenSteps removeObjectAtIndex: 0]; if (CGPointEqualToPoint (currentStep. position, toTileCoord) {// If the shortest path has been found, move the shortest path [self constructPathAndStartAnimationFromStep: currentStep]; _ spOpenSteps = nil; _ spClosedSteps = nil; break;} NSArray * adjSteps = [_ mainScene walkableAdjacentTilesCoordDiagonallyForTileCoord: currentStep. position forRole: self]; for (NSValue * v in adjSteps) {ShortestPathStep * step = [[ShortestPathStep alloc] initWithPosition: [v CGPointValue]; if ([_ spClosedSteps containsObject: step]) {continue;} int moveCost = [StarA progress: 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: step toOpenList: _ spOpenSteps];} else {// already exists in the open list step = _ spOpenSteps [index]; if (currentStep. gScore + moveCost) <step. gScore) {step. gScore = currentStep. gScore + moveCost; step. parent = currentStep; [_ spOpenSteps removeObjectAtIndex: index]; [StarA insertStep: step toOpenList: _ spOpenSteps] ;}}} while (_ spOpenSteps. count> 0 );}

It is relatively long, and I will not elaborate on it here. The information you need is fully covered in the above two series of blog posts. one thing to note is that I started to write such code, so I didn't have a strong grasp of the class elements at the beginning. It is reasonable to say that this method is required by all game players, so they should be placed in their parent class. this is indeed the case. I am doing this in another RPG Game <the soul of pandatv> and we have the opportunity to repeat it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.