2011-07-02 4 views
0

저는 중력을 가지고있는 box2d와 함께 cocos2d에있는 세계를 가지고 있습니다. 이제는 각 스프라이트에 본문을 추가하기 위해 함수를 호출하고 스프라이트를 보냅니다.세계에서 오직 하나의 스프라이트에 0의 중력을 적용합니다.

sprite1은 중력에 따라 움직여야하지만 sprite1이 충돌하기 전까지는 중력이없는 상태에서 정적이어야합니다. 그러면 세계 세력이 영향을 미치게됩니다.

다른 스프라이트가 그를 때릴 때까지 어떻게 sprite1/body gravity를 0으로 설정합니까?

내 문제는 몸에 동일한 기능을 사용하여 모든 스프라이트 :

- (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의 몸을 만들려는 않습니다, 때문에 나중에 그것은 세계에 의해 영향을받을 것이다.

시작시 두 개의 바디를 만든 후 sprite2 만 떨어지는 것을 어떻게 막을 수 있습니까?
감사합니다.

답변

3

나는 일반적으로 SetActive()를 가장 많이 사용하지만 필요에 따라 SetAwake()가 원하는 것입니다.

당신이 필요로하는 모든 것을해야한다

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

b2Body.h

.

+0

잘 작동하지만 잠자기 때 다른 스프라이트가 충돌하면 연락처 수신기에서 감지 할 수 있나요 ??? – Curnelious

+0

수면 시체는 물리적 세계 설정에 영향을받지 않지만 접촉이 감지되면이를 깨 웁니다. 나는 완전히 확신 할 수는 없지만 귀하의 연락처를 통해 가면 한 번 시신을 깨우고 다음 수표에서 두 사람 모두 연락 할 것이라고 기대합니다. –

+0

본문을 비활성으로 설정하면 연락처로 깨우지 않습니다. –

1

세상에 영향을 받고 싶은 스프라이트 만 공간에 추가하고 나중에 세계에 영향을주고 자하는 스프라이트를 추가 할 수 있습니다.

이 경우 Sprite1을 공간에 추가하고 Sprite2는 추가하지 말고 나중에 Sprite2를 추가하여 공간의 영향을 받도록 할 수 있습니다.

관련 문제