1
votes

I have some sprite-sheet that i have to animate forever , and i would like to add it as a CCLayer to my scene . Later on , i have to move this whole animation sprite on the screen. So, for example, i have some animation of a dog walking, from sprite sheet, this one is running forever. than i want to be able to move this dog on screen while animating.

What is the best way to do this ? (or the right way)

This is how i animate the frames :

    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;

How would you move this whole thing ?

1

1 Answers

0
votes

There are a few ways to do this. If you are not preocupied with collision detection, then one way would be to :

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];

otherwise, using your code as is

[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;

}