(No. 00005) iOS-based pop-up game (8): Game leader (1)

Source: Internet
Author: User

(No. 00005) iOS-based pop-up game (8): Game leader (1)

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

I have been playing another RPG Game recently, so this series has not been updated. The address of the previous blog post is:

(No. 00005) iOS game for bombing (7): serialized representation of game data

Next, I will introduce the basic components of the game's main character. The main character of the game is naturally our blow-up person.

Because the protagonist will walk in all directions in the game scenario, we need to prepare the corresponding image resources first:

As you can see, in addition to ordinary mobile actions, I have also selected action materials for the protagonist to be destroyed and invincible. This will show you how to integrate into the game logic. for convenience, I used TexturePacker to package all of them into a single texture to optimize performance. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160203/04133M500-1.png" title = "\"/>

Open Xcode and create a new class BombManSprite. Open the header file BombMaSprite. h and replace it with the following content:

# Import "CCSprite. h" # import "Comm. h" @ class MainScene; // game leader bomb human @ interface BombManSprite: CCSprite
  
   
@ Property (nonatomic, assign) BOOL isDead; @ 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) moveTowardByAStar :( CGPoint) targetLocation; // A * The algorithm moves to the target location (non-tile coordinates)-(void) fired; // burns its animation-(BOOL) isinvincvisible;-(void) setisinvincvisible :( BOOL) isInvincible;-(void) runWinAnimation; @ end
  

The five attributes respectively indicate whether the main character is dead, whether it can penetrate the wall, whether it can wear a bomb, whether it can hurt the main character, and the speed at which the main character walks.

The functions of several instance methods are explained as follows:

// Initialize the protagonist-(id) initWithMainScene :( MainScene *) mainScene; // A * algorithm moves to the target location (non-tile coordinate)-(void) moveTowardByAStar :( CGPoint) targetLocation; // burn the animation itself-(void) fired; // whether it is invincible-(BOOL) isinvincvisible; // set the invincible state-(void) setisinvincvisible :( BOOL) isinvincvisible; // The main character's celebration when the game passes through-(void) runWinAnimation;

Next, open the implementation file. First, we will implement the initialization method of the main character:

-(id)initWithMainScene:(MainScene *)mainScene{    self = [super initWithImageNamed:@"bm_forward.png"];    if (self) {        _mainScene = mainScene;        _facingForwardAnimation = [CCAnimation animation:@"bm" middle:@"forward" frameCount:3];        _facingBackAnimation = [CCAnimation animation:@"bm" middle:@"back" frameCount:3];        _facingLeftAnimation = [CCAnimation animation:@"bm" middle:@"left" frameCount:3];        _facingRightAnimation = [CCAnimation animation:@"bm" middle:@"right" frameCount:3];        _firedAnimation = [CCAnimation animation:@"bm" middle:@"fired" frameCount:5];        _facingForwardRideAnimation = [CCAnimation animation:@"bm" middle:@"rideForward" frameCount:3];        _facingBackRideAnimation = [CCAnimation animation:@"bm" middle:@"rideBack" frameCount:3];        _facingLeftRideAnimation = [CCAnimation animation:@"bm" middle:@"rideLeft" frameCount:3];        _facingRightRideAnimation = [CCAnimation animation:@"bm" middle:@"rideRight" frameCount:3];        _happyAnimation = [CCAnimation animation:@"bm" middle:@"dead" frameCount:3];        _spOpenSteps = nil;        _spClosedSteps = nil;        _shortestPath = nil;        _currentStepAction = nil;        _pendingMove = nil;        _speedPerStep = 0.8;        _curDirection = RoleDirectionDown;    }    return self;}

You can see that the above Code first saves the instances of the main scenario, so that you can call the methods of the main scenario in the future. the main scenario plays a pivotal role here. All related classes can be used with the help of the main scenario to complete the specified functions. in addition, you can place all the help methods in a singleton class, obtain the instance of the singleton class during the call, and then call it.

The next time we create an animation of the various actions of the main character in advance, we will initialize the variables required by the * algorithm and the instance variables of the main character, such as the walking speed.

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.