Iphone cocos2d中的调试标签y值已关闭
我有一个cocos2d游戏,在场景中放置了一个名为GameplayLayer的CCLayer。以下是图层的初始代码:Iphone cocos2d中的调试标签y值已关闭,iphone,debugging,cocos2d-iphone,label,Iphone,Debugging,Cocos2d Iphone,Label,我有一个cocos2d游戏,在场景中放置了一个名为GameplayLayer的CCLayer。以下是图层的初始代码: - (id)initWithScene1BackgroundLayer:(Scene1BackgroundLayer *)scene5UILayer { if ((self = [super init])) { uiLayer = scene5UILayer; startTime = CACurrentMediaTime();
- (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,如下所示:
-(void)addLabel{
//set debuglabel
CCLabelBMFont *debugLabel=[CCLabelBMFont labelWithString:@"NoneNone" fntFile:@"SpaceVikingFont.fnt"];
[self addChild:debugLabel];
[hopper setMyDebugLabel:debugLabel];
}
那么hopper类中的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值约为1567385,然后如果我移动料斗,它会转到35633753,并不断更改为巨大的随机数。看起来很不稳定
为什么会这样?调试标签代码中有一个简单的输入错误。您正在将浮点数格式化为一个只给出垃圾结果的整数。因为stringWithFormat函数不会隐式地将浮点转换为int,而只是获取浮点的位表示形式并将其解释为整数 给出了更详细的解释 那么这个
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];
.谢谢,你比我快了大约5个小时:我刚开始直接记录sprite.position.x&y,看到值很好……就把它修好了。谢谢