1
votes

I think I am mostly after a critique of the way I am using my Player class, and how to improve it.

My spritesheet contains all the sequences I feel I need for the actions I am after. But what I would like initially, is that when my "jump" button is pressed, that the animation the character is going through is stopped, and the "jump" sequence from the spritesheet it used. (walk - for example is frame 7 through 9 where jump is frame 10 through to 13).

Here is how I produce my player sprite :

Player.m :

-(id)playerCharacter {
     if (self = [super init]) {

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Soldier.plist"];

    // Create a sprite sheet with the Player images
    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Soldier.png"];
    [self addChild:spriteSheet z:15];

    NSMutableArray *walking = [NSMutableArray array];
    for(int i = 7; i <= 9; ++i) {
        [walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];
    }

    CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f];

    _walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]];

    walkAnim.restoreOriginalFrame = NO;

    [self runAction:_walkAction];

    self.velocity = ccp(0.0, 0.0);  
}
return self;
}

GameLevelLayer.m

      player = [[Player alloc] playerCharacter];
      player.position = ccp(100,50);
      [map addChild:player z:15];

I guess my question is .. Should I move the whole CCAnimation component of that method out of there, into a new method that is checking a series of BOOLs that define the spritesheet / animation sequences?

Am I even using the above code in the most efficient way? While I appreciate that it works as it is .. I would rather understand if I am doing things correctly or not than carry on down a path that adds inefficiencies to my project.

I greatly appreciate any feedback and assistance.

1

1 Answers

1
votes

You are not using CCSpriteBatchNode properly. Think of that as a layer which will hold all of your sprites from a single sprite sheet. Your player should be added to the batchnode, and your batchnode added to your game layer. So your Player should be created as follows:

-(id)playerCharacterOnSheet:(CCSpriteBatchNode*)batchNode
{
     // this is assuming Player is a CCSprite subclass
     // set this to the "static"/default player sprite image
     if (self = [super initWithSpriteFrameName:@"Soldier1.png"])
     {
        // Load self to the batchnode
        [batchNode addChild:self];

        NSMutableArray *walking = [NSMutableArray array];
        for(int i = 7; i <= 9; ++i) {
            [walking addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];

        CCAnimation *walkingAnimation = [CCAnimation animationWithSpriteFrames:walking delay:0.2f];

        _walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkingAnimation]];

        walkAnim.restoreOriginalFrame = NO;

        [self runAction:_walkAction];

        self.velocity = ccp(0.0, 0.0);  
    }
    return self;
}

Then in GameLevelLayer, create the batchNode and player:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Soldier.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Soldier.png"];
[map addChild:spriteSheet];

player = [[Player alloc] playerCharacterOnSheet:spriteSheet];
player.position = ccp(100,50);

To perform other actions, simply add methods to your Player class to perform them (and stop any other running actions). For example:

-(void)jumpAction
{
    [self stopAllActions];

    NSMutableArray *jumping = [NSMutableArray array];
    for(int i = 10; i <= 13; ++i) {
        [jumping addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Soldier%d.png", i]]];
        }

    CCAnimation *jumpingAnimation = [CCAnimation animationWithSpriteFrames:jumping delay:0.2f];

    CCAction *jumpAction = [CCAnimate actionWithAnimation:jumpingAnimation ];
    [self runAction:jumpAction];

}

To perform, call:

[player jumpAction];