Search code examples
iphonecocos2d-iphone

In Cocos2D how to move a sprite animation one step


My environment is iPhone with Cocos2d.

I'm trying to move a player in a game a single step forward with a sprite animation although I'm not sure of the correct way to do this.

First of all for reference here's part of my init method which loads the animations into a NSDictionary for later user.

-(id) init
{
if( (self=[super init]) ) {
    ..
    ..
    NSMutableArray *walkUpFrames = [NSMutableArray array];
    [walkUpFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player01.png"]];
    [walkUpFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player02.png"]];
    [walkUpFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player03.png"]];
    NSMutableArray *walkDownFrames = [NSMutableArray array];
    [walkDownFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player04.png"]];
    [walkDownFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player05.png"]];
    [walkDownFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player06.png"]];
    NSMutableArray *walkLeftFrames = [NSMutableArray array];
    [walkLeftFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player07.png"]];
    [walkLeftFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player08.png"]];
    [walkLeftFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player09.png"]];
    NSMutableArray *walkRightFrames = [NSMutableArray array];
    [walkRightFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player10.png"]];
    [walkRightFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player11.png"]];
    [walkRightFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"player12.png"]];

    self.walkingFrames = [NSDictionary dictionaryWithObjectsAndKeys:
                          walkUpFrames, @"up",
                          walkDownFrames, @"down",
                          walkLeftFrames, @"left",
                          walkRightFrames, @"right",
                          nil];
    ......
}
return self;
}

First I tried this code to move the player which successfully moves the sprite to the new position and runs the animation but the sprite gets to the position instantly and then runs the animation which is no good.

-(void)setPlayerPosition:(CGPoint)position {

  CCAnimation *walkAnim = [CCAnimation animationWithFrames:[self.walkingFrames valueForKey:@"right"] delay:0.05 ];
  self.walkAction = [CCAnimate actionWithAnimation: walkAnim restoreOriginalFrame:YES];

  [_player runAction:_walkAction];
  _player.position = position;
}

I'm guessing I'm suppose to use CCMoveTo but I don't see how to use it with animationWithFrames. Any ideas? (please provide code example)

-(void)setPlayerPosition:(CGPoint)position {

 self.walkAction = [CCMoveTo actionWithDuration:0.1 position:position];

 [_player runAction:_walkAction];

}

Thanks for any replies.


Solution

  • Running multiple actions did the trick. Thanks Morion....

    -(void)setPlayerPosition:(CGPoint)position
    {
        CCAnimation *walkAnim = [CCAnimation animationWithFrames:[self.walkingFrames valueForKey:@"right"] delay:0.1 ];
        [_player runAction:[CCAnimate actionWithAnimation: walkAnim restoreOriginalFrame:YES]];
    
        [_player runAction:[CCMoveTo actionWithDuration:0.3 position:position]];
    }