1

我的环境是带有 Cocos2d 的 iPhone。

我正在尝试使用精灵动画将游戏中的玩家向前移动一步,尽管我不确定执行此操作的正确方法。

首先作为参考,这里是我的 init 方法的一部分,它将动画加载到 NSDictionary 中供以后的用户使用。

-(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;
}

首先,我尝试使用此代码移动成功将精灵移动到新位置并运行动画的播放器,但精灵立即到达该位置,然后运行动画,这是不好的。

-(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;
}

我猜我应该使用 CCMoveTo,但我不知道如何将它与 animationWithFrames 一起使用。有任何想法吗?(请提供代码示例)

-(void)setPlayerPosition:(CGPoint)position {

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

 [_player runAction:_walkAction];

}

感谢您的任何回复。

4

1 回答 1

1

跑步multiple actions成功了。谢谢莫里恩....

-(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]];
}
于 2012-11-13T06:53:36.237 回答