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;)
has been doing another RPG game recently, so the series has not been updated, the address of the previous post is:
(no.00005) iOS implementation bomb man game (vii): Serialization representation of game data
This article goes on to introduce the basic composition of the game in the bomber game, the protagonist of the game is naturally our bomber.
Because the protagonist will walk in all directions in the game scene, we need to prepare its corresponding image resources first:
As you can see, in addition to ordinary mobile movements, I have also chosen the main character of the human destruction and invincible state of action material, this in the back you will see how to integrate into the game logic. For convenience, I use Texturepacker to pack all of it into one texture to optimize performance.
Open Xcode and create a new class Bombmansprite. Open the BombMaSprite.h header file, replacing it with the following:
#import "CCSprite.h" #import "Comm.h" @class mainscene;//game lead bomb human @interface bombmansprite : ccsprite <roleacross>@property(nonatomic,Assign)BOOLIsdead;@property(nonatomic,Assign)BOOLCanacrossbrick;@property(nonatomic,Assign)BOOLCanacrossbomb;@property(nonatomic,Assign)BOOLisharmless;@property(nonatomic,Assign)CGFloatspeedperstep;-(ID) Initwithmainscene: (mainscene*) mainscene;-(void) Movetowardbyastar: (Cgpoint) TargetLocation;//a* algorithm moves to the target location (non-tile coordinates)-(void) fired;//Burn your own animation-(BOOL) isinvincible;-(void) Setisinvincible: (BOOL) isinvincible;-(void) Runwinanimation;@end
Five of them are the main characters are dead, whether they can wear walls, whether they can wear bombs, whether bombs can hurt the protagonist, as well as the protagonist walking speed.
The functions of several instance methods are explained separately as follows:
//初始化主角-(id)initWithMainScene:(MainScene*)mainScene;//A*算法移动至目标位置(非瓦块坐标)-(void)moveTowardByAStar:(CGPoint)targetLocation; //燃烧自身动画-(void)fired; //是否处于无敌状态-(BOOL)isInvincible;//设置无敌状态-(void)setIsInvincible:(BOOL)isInvincible;//运行游戏过关时的主角庆祝动作-(void)runWinAnimation;
Next open the implementation file, I first to implement the main character initialization method:
-(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; }returnSelf;}
You can see that the above code first saves the instance of the main scene, which facilitates the method of invoking the main scene later. Here's a more important point, where the main scene plays a pivotal role, and all related classes can use the help of the main scene to complete the specified function. Another way is to put all the help methods into a singleton class, Call to get an instance of the Singleton class, and then call it.
The next step is to pre-create animations of the protagonist's various behaviors, followed by initializing some of the variables required by the * algorithm and instance variables of the protagonist itself, such as the speed of the walk.
(no.00005) iOS implementation Bomb man game (eight): Game lead (i)