Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Game with Sprite Kit Intro to Sprite Kit Extra Credit

Travis Golob
Travis Golob
8,794 Points

SKSpriteNode setPosition Method

To get the boxes to appear on the screen, it appears that when I create the CGPointMake object to pass to the setPosition method, I have to offset the x Attribute by about 300 points.

e.g. greenNode.position = CGPointMake(300, 10); redNode.position = CGPointMake(300, 10);

I know I'm using a newer build of xCode than the one used in the examples. There are slight differences such as the scene setup being in the didMoveToView method vs. initWithSize.

4 Answers

To fix that offset thing, go to the GameViewController.m file and change the scene's scaleMode property to SKSceneScaleModeResizeFill. this makes your stuff a little bit stretched or expanded depending on the device. the other scaleModes might give you a letter box effect on your game and so forth.

Jared Watkins
Jared Watkins
10,756 Points

Thank you for pointing this out. I'm using a a newer Xcode (6.1.1) than in the video and getting the same result, no matter which iPhone simulator I use. adding 300 to the x coordinates bring the green and red squares into the visible area of the screen.

Since it was not provided by my Xcode project, I also typed in the code from the video for the blue background:

self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:.3 alpha:1.0];

Travis Golob
Travis Golob
8,794 Points

Thanks Johan! I'm sure a lot of other users coming back to these videos will appreciate that as well.

Comment out or remove the didMoveToView and type out the initWithSize like so.

- (instancetype)initWithSize:(CGSize)size
{
    self = [super initWithSize:size];

    if(self)
    {
        self.backgroundColor = [SKColor colorWithRed:0.15
                                               green:0.15
                                                blue:0.3
                                               alpha:1.0];

        SKSpriteNode *greenNode = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor]
                                                              size:CGSizeMake(15, 150)];
        greenNode.position = CGPointMake(10, 10);
        greenNode.anchorPoint = CGPointMake(0, 0);

        SKSpriteNode *redNode = [SKSpriteNode spriteNodeWithColor:[SKColor redColor]
                                                             size:CGSizeMake(150, 15)];
        redNode.position = CGPointMake(10, 10);
        redNode.anchorPoint = CGPointMake(0, 0);


        [self addChild:greenNode];
        [self addChild:redNode];
    }

    return self;
}

You do not need to change the scaleMode like others have suggested. Mine works as should with SKSceneScaleModeAspectFill.

Please note, your GameViewController.m file should use GameScene *scene = [[GameScene alloc] initWithSize:skView.bounds.size]; to initialise instead and you also do not need the unarchiveFromFile class method so you can remove that too.

Im saying to set it up like this as you can then continue with the video tutorials on the newer Xcode versions without workarounds that may in the future have an affect on the game like the scaleMode.

Im currently doing these tutorials on Xcode 6.2 and getting all the same results as shown in the videos.