3
我想將圖像添加到CCSprite,然後在我的Box2d世界中使用它。 createBoxAtLocation
正確拾取contentSize
,但從不顯示圖像...我做錯了什麼?我需要添加任何東西到update()函數嗎?使用Box2D將UIImage添加到CCSprite
CCSprite *sprite = [CCSprite spriteWithFile:@"slider_piece.png"];
[self createBoxAtLocation:loc1 withSize:boxSize forSprite:sprite];
-(void) createBoxAtLocation:(CGPoint)location withSize:(CGSize)size forSprite:(CCSprite *)_sprite
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
bodyDef.allowSleep = false;
//bodyDef.userData = _sprite;
b2Body *body = world->CreateBody(&bodyDef);
body->SetUserData(_sprite);
b2FixtureDef fixtureDef;
b2PolygonShape shape;
shape.SetAsBox(_sprite.contentSize.width/2/PTM_RATIO, _sprite.contentSize.height/2/PTM_RATIO);
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);
}
編輯
裏面的更新功能我有這個,但它會導致對sprite.position線
for(b2Body *b = world->GetBodyList(); b != NULL; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) b->GetUserData();
sprite.position = ccp(b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
sprite.rotation = CC_RADIANS_TO_DEGREES(b->GetAngle() * -1);
}
}
謝謝Kazuki,保留停止了EXC_BAD_ACCESS錯誤。但圖像仍未顯示。 – daidai
更新了我的答案。 –
謝謝,這樣做! [(CCSprite *)body-> GetUserData()release];走? – daidai