0

我已经编写了一个代码来使用 CCLog 显示当释放移动鼠标时精灵的确切位置。下面是 Sprite.mm 类和 ccTouchesEnded 方法(在 HelloWorldLayer.mm 类中)。CCLog 不显示该消息。

精灵.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;
}

ccTouchesEnd:

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

我感觉问题出在我访问标签的方式上,不太确定。请帮忙。

4

1 回答 1

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
于 2012-08-09T13:44:27.673 回答