2012-08-09 48 views
0

我已經編寫了一個代碼來顯示使用CCLog時精靈的精確位置,當一個鼠標聯合移動它被釋放時。下面是Sprite.mm類和ccTouchesEnded方法(它在HelloWorldLayer.mm類中)。 CCLog不顯示消息。CCLOG不顯示

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

我有一種感覺,這個問題是我在訪問標籤,真的不知道的方式。請幫忙。

+0

設置通過線代碼行一個斷點和步驟:http://www.learn-cocos2d.com/2011/10/xcode-4-debugging-crashcourse/ – LearnCocos2D 2012-08-09 21:01:55

回答

1

在我看來可能會發生兩件事情。

  1. 未到達CCLOG。發生這種情況可能是因爲sprite tag = 1沒有設置,或者你想關注的對象沒有與b2Body鏈接。檢查一個斷點,你到達CCLOG

  2. 用NSLog替換CCLOG。如果它有效,那麼你沒有定義COCOS2D_DEBUG,或者在0上。驗證你的構建設置。

\\ 
/* 
* if COCOS2D_DEBUG is not defined, or if it is 0 then 
* all CCLOGXXX macros will be disabled 
* 
* if COCOS2D_DEBUG==1 then: 
*  CCLOG() will be enabled 
*  CCLOGERROR() will be enabled 
*  CCLOGINFO() will be disabled 
* 
* if COCOS2D_DEBUG==2 or higher then: 
*  CCLOG() will be enabled 
*  CCLOGERROR() will be enabled 
*  CCLOGINFO() will be enabled 
*/ 
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0 
#define CCLOG(...) do {} while (0) 
#define CCLOGINFO(...) do {} while (0) 
#define CCLOGERROR(...) do {} while (0) 

#elif COCOS2D_DEBUG == 1 
#define CCLOG(...) NSLog(__VA_ARGS__) 
#define CCLOGERROR(...) NSLog(__VA_ARGS__) 
#define CCLOGINFO(...) do {} while (0) 

#elif COCOS2D_DEBUG > 1 
#define CCLOG(...) NSLog(__VA_ARGS__) 
#define CCLOGERROR(...) NSLog(__VA_ARGS__) 
#define CCLOGINFO(...) NSLog(__VA_ARGS__) 
#endif // COCOS2D_DEBUG 
+0

@Andreas C:由於我現在得到輸出,但總是給出x:0.00和y:0.00。無論精靈的位置在哪裏。 – newbie 2012-08-09 15:28:01

+0

@newbie我的第一個猜測,爲什麼輸出爲0會是,也許你實際上並沒有正確存儲精靈作爲身體的用戶數據 – 2012-08-17 14:16:38