How Do I Animate a sprite that is attached to a cpShape??

Official forum for the Chipmunk2D Physics Library.
Post Reply
NICKLOHAN
Posts: 5
Joined: Sat Feb 26, 2011 8:51 am
Contact:

How Do I Animate a sprite that is attached to a cpShape??

Post by NICKLOHAN »

Yes, I am a newbie - please help. Thanks!!

How Do I Animate a sprite that is attached to a cpShape??

here's part of my code - it may not make complete sense because i think i'm mixing two different ways of doing things.
Obviously, I am a little lost here. I can't figure out how to "attach" my SpriteFrames to cpShape (if that is even possible).

Code: Select all

-(id) initWithChipPlayerImage
{
	if ((self = [super init]))
	{
		CGSize screenSize = [[CCDirector sharedDirector] winSize];
		smgr = [[SpaceManagerCocos2d alloc] init];       
		[smgr addWindowContainmentWithFriction:0.8 elasticity:0.2 inset:cpvzero];
		[smgr addSegmentAtWorldAnchor:cpv(0,65) toWorldAnchor:cpv(480,65) mass:STATIC_MASS radius:1];
		
		
		cpShape *playerSprite = [smgr addCircleAt:cpv(350,160) mass:8 radius:15];
		[super initWithShape:playerSprite file:@"ship-anim0.png"];
		
		self.autoFreeShape = YES;
		self.spaceManager = smgr;
		[self schedule:@selector(startGravity:) interval:3.0];

		[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game-art.plist"];
		CCSpriteBatchNode *sheet1 = [CCSpriteBatchNode batchNodeWithFile:@"game-art.png" capacity:5];
		[self addChild:sheet1];

		CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
		CCSprite *sapusSprite1 = [[CCSprite alloc] initWithSpriteFrameName:@"ship-anim0.png"];
		CCAnimation	*sapusAnim = [[CCAnimation alloc] initWithName:@"select" delay:0.15f];
		[sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim1.png"]];
		[sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim2.png"]];
		[sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim3.png"]];
		[sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim4.png"]];
		//[sapusSprite1 addAnimation: sapusAnim];
		sapusSprite1.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);

		[sheet1 addChild:sapusSprite1];

		CCAnimate *animate = [CCAnimate actionWithAnimation: sapusAnim restoreOriginalFrame:NO];
		CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
		[sapusSprite1 runAction:repeat];
	}
	return self;
}
I've seen other methods of defining bodies and shapes like so .....

Code: Select all

cpSpace *space;
cpShape *blockShape;
cpBody *blockBody = blockShape->body;
CCNode *parent = (CCNode*)data;	
cpSpaceAddShape(space, blockShape);
cpSpaceAddBody(space, blockBody);
didn't know if this was a better approach to ultimately animate a sprite that is attached to a body.

Anyone have any links to tutorials or sample code that addresses this. Thanks very much!!!!!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by slembcke »

You should probably ask on the Cocos2D forums. (http://www.cocos2d-iphone.org/forum/) I don't know if there are a lot of people here familiar with how Cocos2D animations work.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
NICKLOHAN
Posts: 5
Joined: Sat Feb 26, 2011 8:51 am
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by NICKLOHAN »

well I guess that I would ask a general question here. How would you animate a cpShape. ie. Blinking the eyes of your main player in game.???
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by slembcke »

I think you misunderstand what a shape is. It's not a graphical thing at all, just a collision boundary. You want to animate the graphics (the sprite), and the code you posted is using Cocos2D for that. Chipmunk just does collisions and physics, not graphics.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
NICKLOHAN
Posts: 5
Joined: Sat Feb 26, 2011 8:51 am
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by NICKLOHAN »

ok. I think I understand it better now. I also understand, after some more web searching, that I am actually using spacemanager with chipmunk. Which seems to be a bit different (i guess easier) than just straight up chipmunk. I'm still having hard time linking an animated sprite via cocos2d to my cpShape. Hopefully I will have better luck in cocos forum. Thank you very much for your replies!!!!!
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by mobilebros »

Just saw this.. @NICKLOHAN I'm confused as to why you would think you need to attach sprite frames to a cpShape. Sprite frames would be associated with a sprite, and that sprite would be "attached" to a shape if using SpaceManager's cpCCSprite; you can just animate the sprite like you normally do and not even worry about the fact that chipmunk's controlling the position and rotation for you.
NICKLOHAN
Posts: 5
Joined: Sat Feb 26, 2011 8:51 am
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by NICKLOHAN »

mobilebros wrote:Just saw this.. @NICKLOHAN I'm confused as to why you would think you need to attach sprite frames to a cpShape. Sprite frames would be associated with a sprite, and that sprite would be "attached" to a shape if using SpaceManager's cpCCSprite; you can just animate the sprite like you normally do and not even worry about the fact that chipmunk's controlling the position and rotation for you.
Thank you mobilebros.. That was the piece of info that I was looking for -- you can "attach" a sprite to a shape. So, just so I have this straight. You have a shape that is added to the space. Then you can attach a sprite to the shape. Then your cocos2d will handle the animation for the sprite.
So, does anyone have an example or tutorial on this. I think I understand the concept now - but a specific code example would be greatly appreciated. Many, many thanks....

Here's my code again...

Code: Select all

-(id) initWithChipPlayerImage
{
   if ((self = [super init]))
   {
      CGSize screenSize = [[CCDirector sharedDirector] winSize];
      smgr = [[SpaceManagerCocos2d alloc] init];       
      [smgr addWindowContainmentWithFriction:0.8 elasticity:0.2 inset:cpvzero];
      [smgr addSegmentAtWorldAnchor:cpv(0,65) toWorldAnchor:cpv(480,65) mass:STATIC_MASS radius:1];
      
      
      cpShape *playerSprite = [smgr addCircleAt:cpv(350,160) mass:8 radius:15];
      [super initWithShape:playerSprite file:@"ship-anim0.png"];
      
      self.autoFreeShape = YES;
      self.spaceManager = smgr;
      [self schedule:@selector(startGravity:) interval:3.0];

      [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"game-art.plist"];
      CCSpriteBatchNode *sheet1 = [CCSpriteBatchNode batchNodeWithFile:@"game-art.png" capacity:5];
      [self addChild:sheet1];

      CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
      CCSprite *sapusSprite1 = [[CCSprite alloc] initWithSpriteFrameName:@"ship-anim0.png"];
      CCAnimation   *sapusAnim = [[CCAnimation alloc] initWithName:@"select" delay:0.15f];
      [sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim1.png"]];
      [sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim2.png"]];
      [sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim3.png"]];
      [sapusAnim addFrame:[cache spriteFrameByName:@"ship-anim4.png"]];
      //[sapusSprite1 addAnimation: sapusAnim];
      sapusSprite1.position = CGPointMake(screenSize.width / 2, screenSize.height / 2);

      [sheet1 addChild:sapusSprite1];

      CCAnimate *animate = [CCAnimate actionWithAnimation: sapusAnim restoreOriginalFrame:NO];
      CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
      [sapusSprite1 runAction:repeat];
   }
   return self;
}
akamal
Posts: 15
Joined: Fri Mar 11, 2011 3:32 pm
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by akamal »

In the draw method of layer:

[sprite setPosition:cpv(body->p.x, body->p.y)];
[sprite setRotation:-body->a*180.0f/M_PI];
NICKLOHAN
Posts: 5
Joined: Sat Feb 26, 2011 8:51 am
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by NICKLOHAN »

@akamal
would that cycle through a series of image files???
ndizazzo
Posts: 15
Joined: Thu Feb 10, 2011 7:53 pm
Contact:

Re: How Do I Animate a sprite that is attached to a cpShape??

Post by ndizazzo »

NICKLOHAN wrote:@akamal
would that cycle through a series of image files???
No it wouldn't. You're going to need to set up a frame counter in your sprite class (at the very least). In your render method, you render the sprite tile for the current frame. After some time period, update the frame (in your update method) and change the "current frame" to the next.
Post Reply

Who is online

Users browsing this forum: Heise IT-Markt [Crawler] and 13 guests