我已經編寫了一個代碼來顯示使用CCLog時精靈的位置,當一個鼠標聯合移動它被釋放時。下面是Sprite.mm類和ccTouchesEnded方法(它在HelloWorldLayer.mm類中)。精靈位置不更新,輸出始終爲x:0.00和y:0.00。sprite未更新到正確的位置
Sprite.mm:
-(id)addSprite:(CCLayer *)parentLayer
inWorld:(b2World *)world
{
PhysicsSprite *aSprite = [PhysicsSprite spriteWithFile:@"spriteIm.png"];
aSprite.tag = 1;
[parentLayer addChild:aSprite];
b2BodyDef spriteBodyDef;
spriteBodyDef.userData = aSprite;
spriteBodyDef.type = b2_dynamicBody;
CGSize s = [CCDirector sharedDirector].winSize;
spriteBodyDef.position = [Convert toMeters:ccp(s.width * 0.25,s.height-400)];
b2FixtureDef fixtureDef;
fixtureDef.density = 0.01;
b2CircleShape circleShape;
circleShape.m_radius = aSprite.contentSize.width/2/PTM_RATIO;
fixtureDef.shape = &circleShape;
spriteBody = world->CreateBody(&spriteBodyDef);
spriteFixture = spriteBody->CreateFixture(&fixtureDef);
[aSprite setPhysicsBody:spriteBody];
return aSprite;
}
ccTouchesEnded:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (mouseJoint)
{
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *mySprite = (CCSprite *)b->GetUserData();
if (mySprite.tag == 1) {
CGPoint spritePosition = mySprite.position;
CCLOG(@"the sprite position is x:%0.2f, y:%0.2f", spritePosition.x, spritePosition.y);
}
}
}
world->DestroyJoint(mouseJoint);
mouseJoint = NULL;
}
}
請幫助。我已經呆了幾天了。
這將是更容易得到身體像:B = mouseJoint-> GetBodyB()比遍歷世界上每一個機構。不知道其餘的問題對不起:) – iforce2d 2012-08-13 12:53:49
感謝您的意見。 – newbie 2012-08-13 13:55:59
請發佈您的' - (無效)更新:(ccTime)dt' ..我認爲這就是問題所在。 – Lukman 2012-08-13 13:59:16