如何移动CCSprite

时间:2011-04-18 14:19:17

标签: objective-c cocos2d-iphone

我需要从一边到另一边移动精灵。我用:

CCSprite *man;

id actionMove = [CCMoveTo actionWithDuration:1 position:ccp(20,300)];

[man runAction:actionMove];

当它移动到(20,300)时,我需要将精灵移动到(20,0),然后将其移动到(20,300)并返回。 我怎样才能做到这一点?感谢。

2 个答案:

答案 0 :(得分:7)

CCMoveTo *move = [CCMoveTo actionWithDuration:1 position:ccp(20,300)];
CCCallFuncN *move_done = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished)];
[man runAction:[CCSequence actions:move1,move_done1,nil]];

-(void)spriteMoveFinished
{
    CCMoveTo *move1 = [CCMoveTo actionWithDuration:1 position:ccp(20,0)];

    [man runAction:[CCSequence actions:move1,nil]];
}

答案 1 :(得分:5)

不确定我是否让你正确,但是这应该让你的男人在y轴上向上移动300 px,然后在2秒内再次在同一轴上回到0 px。 不依赖于那个男人在y上移动+300然后在y上移动-300之前的位置。

这是CCMoveBy:

CCAction *moveOne = [CCMoveBy actionWithDuration:1 position:ccp(0, 300)];
CCAction *moveTwo = [CCMoveBy actionWithDuration:1 position:ccp(0, -300)];
CCSequence *manMoving = [CCSequence moveOne, moveTwo, nil];
[man runAction:manMoving];

这是CCMoveTo:

CCAction *moveOne = [CCMoveTo actionWithDuration:1 position:ccp(20, 300)];
CCAction *moveTwo = [CCMoveTo actionWithDuration:1 position:ccp(20, 0)];
CCSequence *manMoving = [CCSequence moveOne, moveTwo, nil];
[man runAction:manMoving];