Now we can turn our attention to our hero, the little spaceman. Let's start by looking through the
ERHero
class.
Filename: ERHero.m
+(id)spriteWithSpriteFrameName:(NSString *)spriteFrameName { return [[[self alloc] initWithSpriteFrameName:spriteFrameName] autorelease]; } -(id) initWithSpriteFrameName:(NSString *)spriteFrameName { if(self = [super initWithSpriteFrameName:spriteFrameName]) { _state = kHeroFalling; // Let the hero take 5 hits before death heroHealth = 5; } return self; }
The ERHero
class is a subclass of CCSprite
class. Because we need a custom init
method for the sprite, we have overridden the
spriteWithSpriteFrameName
class method and the corresponding init
method. As the _state
variable might lead you to guess, the hero will be operating as a simple state machine. Let's see what states are valid:
typedef enum { kHeroRunning = 1, kHeroJumping, kHeroInAir, kHeroFalling...