0

I have a cocos2d game with a CCLayer called GameplayLayer placed inside a scene. Here is the layer's init code:

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

The addLabel method calls the debugLabel of the hopper class like this:

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

}

Then the debugLabel code in the hopper class is:

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

}

For some reason when I run it the X value is fine, its value seems reasonable, it starts out at 100 but the y value is approx 1,567,385 and then if i move the hopper it goes to 35,633,753 and keeps changing to huge random numbers. It seems very unsteady.

Why could this be?

damd
  • 6,116
  • 7
  • 48
  • 77
marciokoko
  • 4,988
  • 8
  • 51
  • 91

1 Answers1

0

There is a simple typo in your debug label code. You are formatting your floating point number as an integer which just gives garbage results. Because the stringWithFormat function will not implicitly convert the float to int but just take the bit representation of the float and interpret it as an integer number.

A more detailed explanation is given here.

So this

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

should be

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

.

Community
  • 1
  • 1
sietschie
  • 7,425
  • 3
  • 33
  • 54
  • thanks, you beat me by about 5 hrs :) I just started NSLogging the sprite.position.x & y directly and saw the values were fine...just fixed it. thanks! – marciokoko Mar 09 '12 at 15:53