Reputation: 12444
I would like to animate my b2Body's in my game. Is it possible to use CCActions with them or does Box2D use a different way of animating things? Each b2Body I have is connected to a CCSprite and the CCSprite gets updated to the current position of the b2Body that corresponds to it. So anyway how could I animate a b2Body to move from position A to position B?
Any tips and advice would also be helpful.
Thanks!
Upvotes: 0
Views: 590
Reputation: 787
This is my new code:
-(void)addBody
{
boxImg=[CCSprite spriteWithFile:@"ballImg1.png"];
boxImg.position=ccp(30,100);
[self addChild:boxImg];
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(30.0/PTM_RATIO, 100.0/PTM_RATIO);
bodyDef.userData = boxImg;
body1= world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body1->CreateFixture(&fixtureDef);
posx1=300.0;
posy1=100.0;
[self performSelector:@selector(moveAni) withObject:nil afterDelay:1.0];
}
-(void)moveAni
{
body1->SetTransform(b2Vec2(posx1/PTM_RATIO, posy1/PTM_RATIO), 0);
id action = [CCMoveTo actionWithDuration:0.4 position:CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO)];
[boxImg runAction:action];
}
First u move the body manually using "SetTransform" ,and use this function
Upvotes: 1