vote up 3 vote down
star
2

Hi all, How can I change an image position 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

flag

1 Answer

vote up 1 vote down

Instead of using the Sprite class, you would use the AtlasSprite class along with an AtlasSpriteManager

The way it works is you have one manager (which manages AtlasSprites for one image) and one or more AtlasSprites (which display the whole, or part of an image)

/* with cocos2d 0.8 (and perhaps lower, haven't tested)
   First you create your manager */
AtlasSpriteManager * spriteManager = [[AtlasSpriteManager alloc] initWithFile:@"filename" capacity:5];
/* add the sprite manager to your scene (or whatever layer it needs to be in) */
[scene addChild: spriteManager];
/* then you create sprites using the manager */
AtlasSprite * sprite1 = [spriteManager createSpriteWithRect:CGRectMake(0.0f,0.0f,200.0f,200.0f)];

/* then add the sprite to the AtlasManagers children */
[spriteManager addChild: sprite1]; // beats me why createSpriteWithRect doesn't do this.

/* you can then change the part that the sprite shows by calling the following */
/* just move the Y position 200px down */
[sprite1 setTextureRect:CGRectMake(0.0f,200.0f,200.0f,200.0f)];

/* this atlas sprite can do most things a sprite can. */
/* the above may change with cocos2d 0.9.0 */
link|flag
thanx alot for your reply. problem solved ;) – Ali Bozorgkhan Nov 29 at 18:26

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.