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

Lee Hardy
Lee Hardy
5,407 Points

spriteNodeWithColor not appearing in SKScene

Hi all,

I'm currently doing the Sprite Kit Game iOS tutorial, and have just gone through the World Setup and Physics Bodies video. This asks you to create a spriteNodeWithColor:[SKColor greenColor]

However in my project, this green sprite node doesnt appear in my scene when I run the app

I did test this and changed spriteNodeWithColor to spriteNodeWithImageNamed and pointed to an image, and this image appeared fine in my scene

I check my code against the project sample and its ok, even copied it and this still didnt work

I am using XCode 6, do I need to be doing something differently to get this sprite node to appear?

Thanks

Lee

Make sure you are adding the view to the scene.

self.view.addSubview(self.aBox!) //swift code.

Can you post a copy of the code for us to look at?

Lee Hardy
Lee Hardy
5,407 Points

Hi Aaron

I'm actually using objective C, not swift at the moment.

My code is identical to Amit's in the tutorial, specifically I have a GroundNode class with the following code

+ (instancetype) groundWithSize:(CGSize)size {
GroundNode *ground = [self spriteNodeWithColor:[SKColor greenColor] size:size];
ground.name = @"Ground";
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:size];
ground.position = CGPointMake(size.width/2,size.height/2);
ground.physicsBody.affectedByGravity = NO;

return ground;
}

Then in my GamePlayScene class I create an instance with:

GroundNode *ground = [GroundNode groundWithSize:CGSizeMake(self.frame.size.width, 22)];
        [self addChild:ground];

However, if I change GroundNode *ground = [self spriteNodeWithColor:[SKColor greenColor] size:size] to GroundNode *ground = [self spriteNodeWithImageNamed:@"myImage" size:size] then this image is added to the scene ok

I did run Amit's project sample and his runs ok for me, so am wondering if theres something in XCode 6 thats different now when creating new SpriteKit games from template?

8 Answers

Sebastian Röder
Sebastian Röder
13,878 Points

I experienced a possibly related problem during the "Intro to SpriteKit" stage of the "Build a Game wit SpriteKit" course. I am also using Xcode 6.0.1. I put the following code into GameScene.m but the green square did not show up in the iOS emulator:

-(void)didMoveToView:(SKView *)view {
    /* Setup your scene here */
    SKSpriteNode *greenNode = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor]
                                                           size:CGSizeMake(100, 100)];    
    [self addChild:greenNode];
}

Then I realized that it does show up once I rotate the iOS emulator into landscape mode. So the green square is there, but simply out of view in portrait mode. The scene's dimensions must be wrong.

This can be fixed by changing line 45 in GameViewController.m from this:

    GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];

to this:

    GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];

I hope this helps some people to work through the course in Xcode 6.

Lee Hardy
Lee Hardy
5,407 Points

Thanks Sebastian,

I actually deleted all of the code from the 6.0.1 SpriteKit template as it wasn't the same as what was used in the tutorial. Basically I made it the same as whats in the video, but the above is definitely good to know for future games!

Lee Hardy
Lee Hardy
5,407 Points

Hi Aaron,

Thanks for the help with this. I finally managed to get this sorted. I removed the background image I'd set, after that I could see the green square. I did some googling and found it may be the layer ordering. I'm not sure why green sknode wasn't visible as it is the last item added to the scene, however it appears to be getting pushed behind my background image

I fixed this by adding the line for zPosition for the ground node, as below. Apparently this moves the image up the layer hierarchy manually. The documentation I followed is here, from around halfway down the page where the helicopter example is: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html

GroundNode *ground = [GroundNode groundWithSize:CGSizeMake(self.frame.size.width, 22)];
ground.zPosition = +1;
[self addChild:ground];

What Version of Xcode are you running?

I have 6.0.1 (6A317)

Lee Hardy
Lee Hardy
5,407 Points

I have the same version as you Aaron

Hello Lee,

I noticed that you have size:size

Did you define this size somewhere? If not try adding CGSizeMake(100,100)

//Your Code now:
GroundNode *ground = [self spriteNodeWithColor:[SKColor greenColor] size:size];


//example: (Change the size to CGSizeMake(100,100)

SKSpriteNode *greenNode = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(100, 100)];
Lee Hardy
Lee Hardy
5,407 Points

Hi Aaron

size is passed from my GamePlayScene class when it calls [GroundNode groundWithSize:size] using the frames width and 22 for height as below

GroundNode *ground = [GroundNode groundWithSize:CGSizeMake(self.frame.size.width, 22)];

Its then added to the scene (or should be added to the scene) using

[self addChild:ground];

I did put 100 in for the width to test, but it still doesnt appear for me

Can you host this on github and provide a link. I'll take a look at it for you.

Lee Hardy
Lee Hardy
5,407 Points

Thanks for the help Aaron, will do.

I wont be able to do this until later this evening (around 7 - 8 hours from now) but will pop a link up once its done

Sounds good. :)

Lee Hardy
Lee Hardy
5,407 Points

Also, I'm not sure why I have to do this in my project, yet in the sample project from Amit it works without this line. I'm wondering if it is the way the SpriteKit template is in Xcode 6

Thank you Sebastien for this Fix :) I got the same issue .

I was running into the same exact issue as Lee Hardy has described. I found that in GameViewController.m, changing skView.ignoresSiblingOrder from YES to NO solved the issue for me (running Xcode 6.1). YES was the default value from the Xcode template.

Thank you Lee Hardy I had the same issue and your fix worked for me :).