用CCSprites动画b2Body?

时间:2011-11-08 00:54:50

标签: ios animation cocos2d-iphone box2d

我想在我的游戏中为我的b2Body制作动画。是否可以使用CCAction或Box2D使用不同的动画方式?我所拥有的每个b2Body都连接到一个CCSprite,并且CCSprite会更新到与其对应的b2Body的当前位置。 所以无论如何我怎么能让b2Body动画从位置A移动到位置B?

任何提示和建议也会有所帮助。

谢谢!

1 个答案:

答案 0 :(得分:1)

这是我的新代码:

-(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];
  }

首先使用“SetTransform”手动移动身体,然后使用此功能