2012-01-16 55 views
0

我注意到在我的遊戲中我的b2Body與我的CCSprite不一樣大小。 這是我使用的代碼:b2Body與CCSprite的大小不一樣嗎?

- (void)addBoxBodyForSprite:(CCSprite *)sprite { 
    b2BodyDef spriteBodyDef; 
    spriteBodyDef.type = b2_dynamicBody; 
    spriteBodyDef.position.Set(sprite.position.x/CTM_RATIO, sprite.position.y/CTM_RATIO); 
    spriteBodyDef.userData = sprite; 
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef); 
    b2PolygonShape spriteShape; 
    spriteShape.SetAsBox(sprite.contentSize.width/CTM_RATIO/2, sprite.contentSize.height/CTM_RATIO/2); 
    b2FixtureDef spriteShapeDef; 
    spriteShapeDef.shape = &spriteShape; 
    spriteBody->CreateFixture(&spriteShapeDef); 
} 

如何將我編輯的代碼只是忽略了CTM(PTM)比一起,僅僅是確切大小爲CCSprite?我知道我應該使用PTM比率,但在我的情況下,這並不重要。

謝謝!

EDIT1

- (void)addBoxBodyForSprite:(CCSprite *)sprite { 
    b2BodyDef spriteBodyDef; 
    spriteBodyDef.type = b2_dynamicBody; 
    spriteBodyDef.position.Set(sprite.position.x/(CC_CONTENT_SCALE_FACTOR() * CTM_RATIO), sprite.position.y/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO)); 
    spriteBodyDef.userData = sprite; 
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef); 
    b2PolygonShape spriteShape; 
    spriteShape.SetAsBox(sprite.boundingBox.size.width/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO)/2, sprite.boundingBox.size.height/(CC_CONTENT_SCALE_FACTOR() * PTM_RATIO)/2); 
    b2FixtureDef spriteShapeDef; 
    spriteShapeDef.shape = &spriteShape; 
    spriteBody->CreateFixture(&spriteShapeDef); 
} 

EDIT2

- (void)addBoxBodyForSprite:(CCSprite *)sprite { 
    b2BodyDef spriteBodyDef; 
    spriteBodyDef.type = b2_dynamicBody; 
    spriteBodyDef.position.Set(sprite.position.x/CTM_RATIO, sprite.position.y/CTM_RATIO); 
    spriteBodyDef.userData = sprite; 
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef); 
    b2PolygonShape spriteShape; 
    spriteShape.SetAsBox(sprite.boundingBox.size.width/CTM_RATIO, sprite.boundingBox.size.height/CTM_RATIO); 
    b2FixtureDef spriteShapeDef; 
    spriteShapeDef.shape = &spriteShape; 
    spriteBody->CreateFixture(&spriteShapeDef); 
} 

回答

1

有兩件事情來我的心是:

  1. 你的視網膜屏幕上測試它沒有設定標定事實或爲你的精靈和形狀。

  2. 嘗試使用boundingBox屬性而不是contentSize。

在第一種情況

使用 - (CC_CONTENT_SCALE_FACTOR() * PTM_RATIO);

+0

好吧,我會盡量#2了。但是,「第一種情況」是什麼意思,你能更具體嗎? – 2012-01-16 06:19:21

+0

對不起,我的意思是,如果你不使用縮放因子和使用視網膜屏幕進行測試。 – Haroon 2012-01-16 06:21:23

+0

哦,我明白了!但是,我將如何在這段代碼中使用它? – 2012-01-16 06:23:44

相關問題