Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在Cocos2D中,如何将精灵动画移动一步_Iphone_Cocos2d Iphone - Fatal编程技术网

Iphone 在Cocos2D中,如何将精灵动画移动一步

Iphone 在Cocos2D中,如何将精灵动画移动一步,iphone,cocos2d-iphone,Iphone,Cocos2d Iphone,我的环境是带有Cocos2d的iPhone 我试图在游戏中用精灵动画将玩家向前移动一步,尽管我不确定这样做的正确方法 首先,这里是我的init方法的一部分,用于将动画加载到NSDictionary中供以后的用户参考 -(id) init { if( (self=[super init]) ) { .. .. NSMutableArray *walkUpFrames = [NSMutableArray array]; [walkUpFrames addObject:

我的环境是带有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,但我不知道如何将其用于动画和帧。有什么想法吗?(请提供代码示例)


感谢您的回复。

运行
多个操作
成功了。谢谢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]];
}

运行
多个操作
成功了。谢谢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]];
}

为什么不在你的精灵上运行两个动作呢?一个会改变它的位置,第二个会改变动画帧EAH,这样做的技巧。谢谢作为第二个问题,我使用的创建NSDictionary来保存多个AnimationFrame并在需要时使用它们的方法是否是管理动画的最佳方法?或者我应该在玩家每次迈步时创建动画帧,然后销毁它们?为什么不在你的精灵上运行两个动作呢?一个会改变它的位置,第二个会改变动画帧EAH,这样做的技巧。谢谢作为第二个问题,我使用的创建NSDictionary来保存多个AnimationFrame并在需要时使用它们的方法是否是管理动画的最佳方法?或者我应该在玩家每次迈步时创建动画帧,然后销毁它们?