Background position alternating position every time I build in Xcode 5

117 Views Asked by At

I am just beginning programming games in Xcode 5 using cocos2D and found this pretty strange. I'm starting out fresh on a menu scene and was importing a background and a button. The following code positions my background just fine sometimes, but then other times it's adjusted upwards about 50 pixels (my simulator is on it's side, or it's length is lying horizontal, so technically it's shifting about -50 pixels in the "width" direction, although to the simulator it shifts upwards).

Note I found that every time I run my program, it alternates between being properly aligned and shifted. Why would this be happening ugh! Below is the code I'm using.

Note 2 I'm using Kobold2D and the framework I'm using has a config.lua that's a little beyond my scope for me to understand everything. The config.lua code is located here http://snipt.org/BEt6

-(id) init
{
    if ((self = [super init]))
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"background.png"];
        sprite.anchorPoint = CGPointZero;
        [self addChild:sprite z:-1];

        sprite = [CCSprite spriteWithFile:@"button.png"];
        sprite.anchorPoint = CGPointZero;
        sprite.position = CGPointMake(200,200);
        [self addChild:sprite z:0];
    }

    return self;
}
1

There are 1 best solutions below

0
On

The only problem with your code is that you are initializing sprite twice, which is NOT very good.
I would think it is causing your problem.

Try this code:

-(id) init
{
    if ((self = [super init]))
    {
        CCSprite *sprite = [CCSprite spriteWithFile:@"background.png"];
        sprite.anchorPoint = CGPointZero;
        [self addChild:sprite z:-1];

        CCSprite *sprite2 = [CCSprite spriteWithFile:@"button.png"];
        sprite2.anchorPoint = CGPointZero;
        sprite2.position = CGPointMake(200,200);
        [self addChild:sprite2 z:0];
}

return self;

}