0

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;
}
spaderdabomb
  • 942
  • 12
  • 28
  • Nothing obvious to me...except you should not be setting the anchorPoint to zero...it should usually be left alone at (0.5,0.5) (the center of the image/sprite). – FuzzyBunnySlippers Dec 22 '13 at 20:53
  • While it seems unusual (to me) to specify -1 for the z-order, looking at the root code, it seems like it would work ok... – FuzzyBunnySlippers Dec 22 '13 at 21:01

1 Answers1

0

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;

}

zamfir
  • 161
  • 7