2009-11-20 91 views
4

如何更改cocos2d中的图像位置,如css中的background-position?通过位置我不是指像ccp(200,150)这样的背景位置,我的意思是,例如,我有一个图像,分辨率为(1000,200),包含5个(200,200)图像。我在(200,200)sprite中显示此图像,我想更改图像位置,以便在1张图片中显示5张图片。我认为如果你想显示像跑步一样的动作,你可以使用像10帧动作一样的动作,并改变图像位置,使它看起来像一个人在跑步。我怎样才能做到这一点? thanx提前cocos2d sprite动画

回答

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