Cocos2d 精灵动画

4
我如何在cocos2d中更改图像位置,就像css中的background-position一样?这里的“位置”不是指背景的位置,例如ccp(200,150),而是指我有一个分辨率为(1000,200)的图像,其中包含5个(200,200)的图像。我在(200,200)精灵中显示此图像,并希望更改图像位置,以便我可以在1个图像中显示5个图片。我认为,如果您想显示像奔跑这样的动作,则使用具有10个运动帧的精灵,并更改图像位置,以便看起来像一个人在奔跑。我该怎么做呢? 提前感谢。
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接