Page 1 of 1

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

Posted: Sat Feb 26, 2011 9:05 am
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!!!!!

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

Posted: Sat Feb 26, 2011 2:48 pm
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.

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

Posted: Sat Feb 26, 2011 3:51 pm
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.???

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

Posted: Sat Feb 26, 2011 6:27 pm
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.

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

Posted: Mon Feb 28, 2011 7:06 am
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!!!!!

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

Posted: Wed Mar 02, 2011 3:47 pm
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.

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

Posted: Sun Mar 13, 2011 12:19 pm
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;
}

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

Posted: Mon Mar 14, 2011 1:36 pm
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];

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

Posted: Mon Mar 14, 2011 8:15 pm
by NICKLOHAN
@akamal
would that cycle through a series of image files???

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

Posted: Mon Mar 21, 2011 11:50 am
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.