重置精灵 - Box2d(Cocos2d)

时间:2011-06-12 22:58:03

标签: iphone cocos2d-iphone box2d

我有一个球在与精灵碰撞时触发动作。然后在动作完成后,用

重置它的位置
-(void)removeBall
{
    [self stopAllActions];
    _ballBody->SetTransform(b2Vec2(10, 2), 0);
}

但是,[self stopAllActions]不会停止动作。我怎么能停止行动? 有任何想法吗?

由于

3 个答案:

答案 0 :(得分:3)

==>您可以按照以下方式进行操作

==>销毁身体对象并创建新的并重置您想要设置的位置 这肯定会有用

     _world->DestroyBody(_body);
    b2BodyDef  _playerBodyDef;
    _playerBodyDef.type=b2_dynamicBody;
    _playerBodyDef.position.Set(160/PTM_RATIO,240/PTM_RATIO);
    _playerBodyDef.userData=_ball;
    _body=_world->CreateBody(&_playerBodyDef);
    //create the shape for the rounded stick
    b2CircleShape PlayerShape;
    PlayerShape.m_radius = 15.0/PTM_RATIO;  

    //fixtureDef  specifying the shape to circle
    b2FixtureDef _playerShapeDef;
    _playerShapeDef.shape = &PlayerShape;
    _playerShapeDef.density = 1.0f;
    _playerShapeDef.friction = 0.5f;
    _playerShapeDef.restitution = 1.0f;
    _playerShapeDef.filter.groupIndex =k_largeGroup;
    _body->CreateFixture(&_playerShapeDef);
祝你好运

答案 1 :(得分:2)

StopAllActions是Cocos2D,而不是Box2D。我如何重用身体,精灵,粒子等设置为visible = NO和body-> SetActive(false)。

这是停用对象:

CCSprite *sprite = [projectiles objectAtIndex:i];

// Just to continue a loop if the sprite is not visible         
if(sprite.visible == NO) continue;

sprite.visible = NO;

b2Body *body = projectileBodyTracker[i];
body->SetActive(false);

这是为了重新激活对象:

CCSprite *sprite = [projectiles objectAtIndex:i];

sprite.position = moveToPosition;

b2Body *body = projectileBodyTracker[i];
body->SetTransform(moveToPositionVector, rotation);
body->SetActive(true);

我已经把额外的代码写在我的头顶,所以我不能完全确定它是正确的(语法上),但这是我的方式。

答案 2 :(得分:0)

我正在使用

-(void)update:(ccTime)delta
{

}

以constaly检查ballBody是否与另一个身体相撞并使用Accelerometer移动ballBody。因此,要重置ballBody的位置,我只需替换调用“dealloc”的场景,然后再次启动场景。

[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];