cocos2d精灵动画

时间:2009-11-20 07:13:29

标签: iphone cocos2d-iphone

如何更改coc中的背景位置cocos2d中的图像位置?按位置我不是指背景的位置,如ccp(200,150),我的意思是,例如,我有一个分辨率为(1000,200)的图像包含5(200,200)个图像。我在(200,200)精灵中显示这个图像,我想改变图像位置,这样我就可以在1张图片中显示5张图片。我想如果你想展示一个像跑步一样的运动,你可以使用10帧运动的精灵,并改变图片的位置,使它看起来像一个人正在跑步。我怎样才能做到这一点? 提前谢谢

1 个答案:

答案 0 :(得分:9)

我找到了它:

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