开发者

Reset Sprite - Box2d(Cocos2d)

I have a ball that triggers an action when it collides with a sprite. Then after the action is finished it resets it's position with

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

But, [self 开发者_如何学PythonstopAllActions] doesn't stop the action. How can I stop the action? Any ideas?

Thanks


==>You can do the way i have done below

==> Destroy the the body object and create new and reset the position were you want to set this will definitely work

     _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);

Good luck


StopAllActions is Cocos2D, not Box2D. What I do to reuse bodies, sprites, particles etc is set visible=NO and body->SetActive(false).

This is to deactivate the object:

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);

This is to reactivate the object:

CCSprite *sprite = [projectiles objectAtIndex:i];

sprite.position = moveToPosition;

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

I have written the additional code off the top of my head so I can't completely be sure it is correct (syntactically), but this is the way I do it.


I am using

-(void)update:(ccTime)delta
{

}

to constaly check if the ballBody has collided with the other body and using the Accelerometer to move the ballBody. So, to reset the position of the ballBody I just replace the scene, which calls the "dealloc" then starts the scene again.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜