CCSprite动画问题:循环移动 - [从左出口进入右侧]永远执行此操作

时间:2010-10-20 11:53:04

标签: iphone animation cocos2d-iphone

这是我的代码,我一直在尝试 -

-(void) animatedMove:(CCSprite *)character 
{
    ccTime actualDuration = 10;
    id actionMove = [CCMoveBy actionWithDuration:actualDuration
                                        position:ccp(screen_width+50, character.position.y)];
    id actionBack = [CCMoveBy actionWithDuration:1
                                        position:ccp(50, screen_height/2)];
    id actionFinished = [CCCallFuncN actionWithTarget:self
                                            selector:@selector(animatedMoveFinished:)];
    [character runAction:[CCSequence actions:actionMove,actionBack,actionFinished,nil]];
}

-(void) animatedMoveFinished:(id)sender 
{
    CCSprite *character = (CCSprite *)sender;
    [self animatedMove:character];

    NSLog(@"( %.2f, %.2f", character.position.x, character.position.y);
}

将角色移出屏幕而不是重新初始化到开始位置。

1 个答案:

答案 0 :(得分:1)

看起来你想要它从左边出现,退出右边,从左边重新出现等等......

ccTime actualDuration = 10;
id actionMoveBy = [CCMoveBy actionWithDuration:actualDuration
    position:ccp(screen_width+50, character.position.y];
id actionMoveTo = [CCMoveTo actionWithDuration: 0.0f position: character.position];
id actionSequence = [CCSequence actions: actionMoveBy, actionMoveTo, nil];
[character runAction: [CCRepeatForever actionWithAction: actionSequence]];

这将“永远循环”(由于使用'CCRepeatForever'),导致角色对象从其原始起始位置移动到屏幕的最右侧(使用您已经应用的ccp()数学) ,然后立即“立即移动”(由于0s持续时间)回到它的原始起始位置,然后重复......