(No. 00005) iOS game for the blow-up man (11): the fire genie of monsters

Source: Internet
Author: User

(No. 00005) iOS game for the blow-up man (11): the fire genie of monsters

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 ;)

Starting from this article, we will introduce how to create enemies in the game. all the friends who have read the first article know that four types of enemies are ready in the pop-up games. different monsters have different characteristics. Otherwise, if the shape changes and other actions are the same, it will be meaningless.

This article introduces the first and most common FireSprite.

First, you need to find the materials corresponding to the flame genie: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160203/0414003914-1.png" title = "\"/>

You can use TexturePacker to create Cocos2D textures.

Open Xcode, create a FireSprite class, and replace the header file with the following content:

# Import "CCSprite. h" # import "Comm. h" @ class MainScene; // enemy FireSprite class @ interface FireSprite: CCSprite
  
   
{@ Protected MainScene * _ mainScene; // The following two variables prevent the wizard from clicking the screen again when the previous movement is not complete. // The current step Action CCAction * _ currentStepAction; // The pending move action NSValue * _ pendingMove; NSInteger _ maxHScore;} @ property (nonatomic, assign) NSInteger score; @ property (nonatomic, assign) BOOL canAcrossBrick; @ property (nonatomic, assign) BOOL canAcrossBomb; @ property (nonatomic, assign) BOOL isHarmless; @ property (nonatomic, assign) CGFloat speedPerStep; -(id) initWithMainScene :( MainScene *) mainScene;-(void) latency :( NSInteger) maxHScore;-(void) fade;-(BOOL) moveTowardByAStarSingleCall :( CGPoint) targetLocation; + (void) initClassNames; + (FireSprite *) randomFS :( MainScene *) mainScene; + (Class) randomFSClass; @ end
  

Note that the interface definition indicates that the class complies with the role‑ss protocol, which is defined in Comm. h:

// Role traversal protocol @ protocol rolew.ss-(BOOL) canw.ssbrick; // can it pass through bricks-(BOOL) canw.ssbomb; // can it pass through the bomb-(BOOL) isHarmless; // whether it is harmless (only used on the enemy role for the time being) @ optional-(void) otherMovingWay; // optional method to implement special movement behavior of the enemy role @ end

The FireSprite Class header file is similar to the one defined in the main character of the game. To make this class the base class of all other monster classes, we put some instance variables that may be used by subclass into the interface declaration, use the @ protected directive to modify the file. I have commented on the names of other instance variables.

The behavior of enemies and protagonists in the game is similar, and can be moved or killed. but the main difference between them is that the movement of the game's leading role is manually controlled, while the monsters in the game are moved by AI. for this difference, we need to modify its moving method. Let's take a look at how to complete this function.

First, because you do not need to consider the next move operation when the target is moved, we only need to consider a non-reentrant move method:

// The AStar mobile algorithm (BOOL) moveTowardByAStarSingleCall (CGPoint) targetLocation {if (_ currentStepAction) {_ pendingMove = [NSValue valueWithCGPoint: targetLocation]; return NO ;} if (CGPointEqualToPoint (fromTileCoord, toTileCoord) {return NO;} if (! [_ MainScene isWalkableTile: toTileCoord forRole: self]) {return NO;} // omitting the same code as the mobile method of the game protagonist... // If the fs is locked in a tile, there will be deadlocks with no neighbor reachable blocks. Here, we simply let it try again in 5 seconds. if (adjSteps. count = 0) {[self defined mselector: @ selector (autoMoveScopeWithMaxHScoreInside) withObject: nil afterDelay: 5.0f]; return YES ;} // omitting the same code as the mobile method of the game protagonist... return YES ;}

The above code is very similar to the mobile code of the game's main character, but some small adjustments have been made. For example, if the method returns YES, it indicates that the mobile behavior is completed. Otherwise, it indicates no. in this example, the path-seeking deadlock occurs when a monster is locked in a closed space.

We know that the above Code is only the first half of A * path finding. In the next article, let's take A look at the encoding of the second half. See the next article.

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.