将零的重力应用于世界上只有一个精灵

时间:2011-07-02 16:09:35

标签: xcode cocos2d-iphone box2d

我在cocos2d中有一个带有box2d的世界,它有一个引力。 现在为了为每个精灵添加一个体,我正在调用一个函数并向她发送精灵。

sprite1,必须根据重力移动,但是sprite2必须是静态的,没有重力,直到sprite1击中它,然后世界力量应该影响他。

如何将sprite1 / body gravity设置为零,直到另一个精灵击中他?

我的问题是所有精灵都为身体使用相同的功能:

- (void)addBoxBodyForSprite:(CCSprite *)sprite {

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
    spriteBodyDef.userData = sprite;
    spriteBody = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 10.0;
    spriteShapeDef.isSensor = true;
    spriteBody->CreateFixture(&spriteShapeDef);

}

我想在sprite1上仅在开始时应用重力,但是我也想为sprite2创建一个体,因为以后它会受到世界的影响。

所以,在开始创建2个实体后,如何才停止sprite2掉落? 非常感谢。

2 个答案:

答案 0 :(得分:3)

我通常最常使用SetActive(),但根据您的需要,我认为SetAwake()就是您想要的。

b2Body.h

/// You can disable sleeping on this body. If you disable sleeping, the
/// body will be woken.
void SetSleepingAllowed(bool flag);

/// Is this body allowed to sleep
bool IsSleepingAllowed() const;

/// Set the sleep state of the body. A sleeping body has very
/// low CPU cost.
/// @param flag set to true to put body to sleep, false to wake it.
void SetAwake(bool flag);

/// Get the sleeping state of this body.
/// @return true if the body is sleeping.
bool IsAwake() const;

/// Set the active state of the body. An inactive body is not
/// simulated and cannot be collided with or woken up.
/// If you pass a flag of true, all fixtures will be added to the
/// broad-phase.
/// If you pass a flag of false, all fixtures will be removed from
/// the broad-phase and all contacts will be destroyed.
/// Fixtures and joints are otherwise unaffected. You may continue
/// to create/destroy fixtures and joints on inactive bodies.
/// Fixtures on an inactive body are implicitly inactive and will
/// not participate in collisions, ray-casts, or queries.
/// Joints connected to an inactive body are implicitly inactive.
/// An inactive body is still owned by a b2World object and remains
/// in the body list.
void SetActive(bool flag);

/// Get the active state of the body.
bool IsActive() const;

这应该是你需要的一切。

答案 1 :(得分:1)

您可以尝试仅将想要受世界影响的精灵添加到空间,然后添加您想要受世界影响的精灵。

因此,在这种情况下,将Sprite1添加到您的空间但不添加Sprite2,稍后您可以添加Sprite2,以便它受空间影响。