2011-07-02 2 views
0

私は、重力を持つbox2dでcocos2dの世界を持っています。 各スプライトのボディを追加するために、私は関数を呼び出してスプライトを送ります。世界で1つのスプライトにゼロの重力を適用する

sprite1は重力に従って移動する必要がありますが、スプライト1が重なることなく静的でなければなりません。

どのように私は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); 

} 

私はスプライト1の開始時に重力を適用したいが、私はまたsprite2のためのボディを作成したいです、なぜなら後でそれは世界の影響を受けます。

開始時に2つのボディを作成したら、どうやってsprite2だけが落ちるのを止めますか?
ありがとうございます。

答えて

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; 

+0

は素晴らしいですが、スリープ状態になると、他のスプライトがヒットしたときに連絡先リスナーがそれを検出できますか? – Curnelious

+0

睡眠体は物理的な世界の設定には影響されませんが、接触を検出すると起きます。私は完全にはわかっていませんが、私はあなたの連絡先を通って行くと体を起こし、次のチェックで両方とも連絡先を登録すると期待します。 –

+0

ボディを非アクティブに設定した場合、連絡先によってウォークアップされません。 –

1

世界の影響を受けたいスプライトだけをスペースに追加し、後で世界の影響を受けたいスプライトを追加することができます。

この場合、あなたのスペースにSprite1を追加しますがSprite2は追加しません。後でSprite2を追加してスペースの影響を受けます。

関連する問題