首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >cocos2d中的调试标签y值为off

cocos2d中的调试标签y值为off
EN

Stack Overflow用户
提问于 2012-03-09 05:05:54
回答 1查看 98关注 0票数 0

我有一个cocos2d游戏,场景中有一个名为GameplayLayer的CCLayer。下面是层的初始化代码:

代码语言:javascript
运行
复制
    - (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方法像这样调用hopper类的debugLabel:

代码语言:javascript
运行
复制
-(void)addLabel{
//set debuglabel
CCLabelBMFont *debugLabel=[CCLabelBMFont labelWithString:@"NoneNone" fntFile:@"SpaceVikingFont.fnt"];
[self addChild:debugLabel];
[hopper setMyDebugLabel:debugLabel];    

}

那么hopper类中的debugLabel代码是:

代码语言:javascript
运行
复制
-(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,并不断变化为巨大的随机数。它看起来非常不稳定。

为什么会这样呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-09 18:10:57

您的调试标签代码中有一个简单的拼写错误。您正在将浮点数格式化为整数,这只会产生无用的结果。因为stringWithFormat函数不会隐式地将浮点数转换为整数,而只是获取浮点数的位表示形式,并将其解释为整数。

更详细的解释给出了here

所以这就是

代码语言:javascript
运行
复制
NSString *labelString = [NSString stringWithFormat:@"X: %.2f \n Y:%d \n", newPosition.x, newPosition.y];

应该是

代码语言:javascript
运行
复制
NSString *labelString = [NSString stringWithFormat:@"X: %.2f \n Y:%.2f \n", newPosition.x, newPosition.y];

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9624935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档