2009-11-20 173 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];