0
votes

I have the following setup in my game:

  • An SKNode with the name _backgroundLayer
  • A repeating texture which I have added to the _backgroundLayer 9 times to make a larger background
  • An SKSprite with the name levelButton, which is added to the _backgroundLayer ([_backgroundLayer addChild:levelButton];).

I use levelButton.anchorPoint = CGPointMake(0.5, 0.5); to make the levelButton have an anchor point in the middle. Now, when I make levelButton.position = CGPointMake(0, 0); and _backgroundLayer.position = CGPointMake(0, 0); the levelButton's middle is correctly situated in (0, 0) with its middle being the in lower left corner of the screen. So that's fine. However, if I move the levelButton to be levelButton.position = CGPointMake(100, 0); and _backgroundLayer.position = CGPointMake(-100, 0);, as I show below, the levelButton should still have its middle in (0,0) aka the lower left corner of the screen. However, that is not the case, and the levelButton is more to the right, its something like 50 pixels to the right. But it shouldn't be, since I am moving the _backgroundLayer 100 to the left (-100) and _levelButton 100 to the right (100). It should have stayed in place.

These are basic stuff which I do not understand why they are not working as they should. I am probably doing something wrong but I can not find it even though I've read through the iOS Games by Tutorials and a bunch of tutorials and tips.

Please help.

Now, my code is the following:

@implementation LevelSelectScene
{
   SKNode *_backgroundLayer;
}

-(id)initWithSize:(CGSize)size {

    /* Setup your scene here */
    _backgroundLayer = [SKNode node];
    _backgroundLayer.name = @"backgroundLayer";

    SKTexture *backgroundTexture = [SKTexture textureWithImageNamed:@"levelSelect"];

    int textureID = 0;

    for (int i = 0; i<3; i++) {
        for (int j = 0; j<3; j++) {

            SKSpriteNode *background = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];

            background.anchorPoint = CGPointZero;
            background.position = CGPointMake((background.size.width)*i,
                                              (background.size.height)*j);
            background.zPosition = 0;
            background.name = [NSString stringWithFormat:@"background%d", textureID];

            textureID++;

            [_backgroundLayer addChild:background];
        }
    }

    [self addChild:_backgroundLayer];

    SKSpriteNode * levelButton = [SKSpriteNode spriteNodeWithImageNamed:@"lock"];
    levelButton.anchorPoint = CGPointMake(0.5, 0.5);
    levelButton.position = CGPointMake(100, 0);                 //IMPORTANT
    levelButton.zPosition = 150;
    levelButton.name = @"test";

    [_backgroundLayer addChild:levelButton];

    _backgroundLayer.position = CGPointMake(-100, 0);     //IMPORTANT
}
return self;
}
1

1 Answers

0
votes

Found it! I was changing the scale of the _backgroundLayer SKnode so the coordinates were different.