移动整个动画精灵表

时间:2014-01-03 14:06:37

标签: cocos2d-iphone

我有一些精灵表,我必须永远动画,我想将其添加为CCLayer 到我的场景。 稍后,我必须在屏幕上移动整个动画精灵。 所以,例如,我有一些狗的动画,从精灵表,这一个永远在运行。我希望能够在动画制作时将这只狗移动到屏幕上。

最好的方法是什么? (或正确的方式)

这是我为帧设置动画的方式:

    CCSprite *boom;
    boom = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"%@_00000.png",file]];
    boom.position=touch;
    [self addChild:boom];


    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 0; i < 5; i++)

    {

        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:
                                                                                               @"%@_0000%i.png",file,i]];
        [animFrames addObject:frame];
    }

    CCAnimation* boxAnimation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.075f];
    CCAnimate * boxAction = [CCAnimate actionWithAnimation:boxAnimation];
    CCAction *call=[CCCallBlock actionWithBlock:^{[self removeFromParentAndCleanup:YES];}];
    CCAction * sequence=[CCSequence actions:boxAction,[CCHide action],call,nil];
    [boom runAction:sequence];

    return self;

你会如何移动这整件事?

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。如果你没有专注于碰撞检测,那么一种方法是:

CGPoint egressPosition = ccp(0,0); // figure this out in your app
float moveDuration = 1.5f ;        // whatever time you compute for desired speed and distance 
id move = [CCMoveTo actionWithDuration:moveDuration position:egressPosition];
id spawn = [CCSpawn actions:sequence,move,nil];
[boom runAction:spawn];

否则,按原样使用您的代码

[self schedule:@selector(moveBox:)];  // optional, you could do this in update method
[boom runAction:sequence];


-(void) moveBoom:(CCTime) dt {
    CGPoint newPosition;
    delta = ccp(dt*speedX,dt*speedY);       // crude , just to get the idea
    newPosition = ccpAdd(boom.position,delta);

    // here you can figure out collisions at newPosition before the collision
    // and do whatever seems appropriate

    boom.position = newPosition;

}