cocos2d中的调试标签y值已关闭

时间:2012-03-08 21:05:54

标签: iphone debugging cocos2d-iphone label

我有一个cocos2d游戏,其中一个名为GameplayLayer的CCLayer放置在一个场景中。这是图层的初始化代码:

    - (id)initWithScene1BackgroundLayer:(Scene1BackgroundLayer *)scene5UILayer {
    if ((self = [super init])) {
        uiLayer = scene5UILayer;
        startTime = CACurrentMediaTime();
        [self scheduleUpdate];
        self.isAccelerometerEnabled = YES;
        //chipmunk
        [self createSpace];
        [self createGround];
        mouse = cpMouseNew(space);
        self.isTouchEnabled = YES;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"escapesceneatlas.plist"];
            sceneSpriteBatchNode = [CCSpriteBatchNode
                                    batchNodeWithFile:@"escapesceneatlas.png"];
            hopper = [[[CPHopper alloc] initWithLocation:ccp(200,200) space:space groundBody:groundBody] autorelease];
        } else {
            [[CCSpriteFrameCache sharedSpriteFrameCache]
             addSpriteFramesWithFile:@"escapesceneatlas.plist"];
            sceneSpriteBatchNode = [CCSpriteBatchNode
                                    batchNodeWithFile:@"escapesceneatlas.png"];
            //Viking became hopper and starts at bottom
            hopper = [[[CPHopper alloc] initWithLocation:ccp(100,100) space:space groundBody:groundBody] autorelease];

            //An enemy robot called JJ1 also starts at bottom
            genericBody = [[[CPGenericBody alloc] initWithLocation:ccp(210,200) space:space groundBody:groundBody] autorelease];

            //add the batchnode to layer
            [self addChild:sceneSpriteBatchNode z:0];
        }

        [self addLabel];

        [sceneSpriteBatchNode addChild:hopper z:2];
        [sceneSpriteBatchNode addChild:genericBody z:2];

    }

    return self;
}

addLabel方法调用漏斗类的debugLabel,如下所示:

-(void)addLabel{
//set debuglabel
CCLabelBMFont *debugLabel=[CCLabelBMFont labelWithString:@"NoneNone" fntFile:@"SpaceVikingFont.fnt"];
[self addChild:debugLabel];
[hopper setMyDebugLabel:debugLabel];    

}

然后,漏斗类中的debugLabel代码为:

-(void)setDebugLabelTextAndPosition {
CGPoint newPosition = [self position];
NSString *labelString = [NSString stringWithFormat:@"X: %.2f \n Y:%d \n", newPosition.x, newPosition.y];

[myDebugLabel setString: [labelString stringByAppendingString:@" tracking..."]];

float yOffset = screenSize.height * 0.195f;
newPosition = ccp(newPosition.x,newPosition.y+yOffset);
[myDebugLabel setPosition:newPosition];

}

出于某种原因,当我运行它时,X值很好,它的值似乎合理,它从100开始但y值约为1,567,385然后如果我移动料斗它会变为35,633,753并且不断变为大量随机数字。看起来非常不稳定。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

调试标签代码中有一个简单的拼写错误。您将浮点数格式化为整数,只是给出了垃圾结果。因为stringWithFormat函数不会隐式地将float转换为int,而只是获取float的位表示并将其解释为整数。

更详细的解释是here

所以这个

NSString *labelString = [NSString stringWithFormat:@"X: %.2f \n Y:%d \n", newPosition.x, newPosition.y];

应该是

NSString *labelString = [NSString stringWithFormat:@"X: %.2f \n Y:%.2f \n", newPosition.x, newPosition.y];