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?