Search code examples
iphonecocos2d-iphone

cocos2d sprite animation


How can I change an image position in cocos2d like background-position in css? By position I don't mean the position of background like ccp(200,150), I mean for example, I have an image with resolution of (1000,200) that contains 5 (200,200) images. I show this image in (200,200) sprite and I want to change the image position so I can show 5 pictures in 1 picture. I think If you want to show a movement like running, you use sprites with like 10 frames of movement and change the picture position so it looks like a person is running. How can I do that? thanx in advance


Solution

  • I've found it :

    AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"ax.png" capacity:6];
    AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 200, 200) spriteManager:mgr];
    [sprite setTransformAnchor:ccp(0,0)];
    sprite.position = ccp(100,100);
    [mgr addChild:sprite z:0];
    
    // Add manager to this layer
    [self addChild:mgr z:3];
    
    // Create animation
    AtlasAnimation* animation = [AtlasAnimation animationWithName:@"testAnimation" delay:0.1];
    assert( animation != nil );
    
    // Define the frames in the sprite sheet used for the animation
    [animation addFrameWithRect:CGRectMake(0, 0, 200, 200)];
    [animation addFrameWithRect:CGRectMake(300, 0, 200, 200)];
    [animation addFrameWithRect:CGRectMake(400, 0, 200, 200)];
    [animation addFrameWithRect:CGRectMake(500, 0, 200, 200)];
    [animation addFrameWithRect:CGRectMake(600, 0, 200, 200)];
    [animation addFrameWithRect:CGRectMake(700, 12, 200, 200)];  
    id action = [Animate actionWithAnimation:animation]; 
    assert( action != nil );
    
    // Run the animation
    id repeatAction = [Repeat actionWithAction:action times:100];
    
    // To repeat forever, use this
    // id repeatAction = [RepeatForever actionWithAction:action];
    
    [sprite runAction:repeatAction];