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
Simone Giuditta
1,443 PointsSpriteKit course in Xcode 6
Hello, I m trying to follow along the Spritekit course but unfortunately my starting code is different since I'm using Xcode 6. When i try to add the green cube i get nothing on screen and the view seems to unarchive the .sks file in the project which i don't get what is for. Can i follow along anyway? if so how? Thank you =)
The code I'm using is
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
SKSpriteNode *greenNode = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(100, 100)];
[self addChild:greenNode];
}
that's just the code used by the teacher so I don't think the problem is this ;)
3 Answers
agreatdaytocode
24,757 PointsHello Simone,
Try adding a position to your "greenNode"
Below is an example in swift.
var box = SKSpriteNode()
override func didMoveToView(view: SKView) {
//box
box.color = UIColor .greenColor()
box.size = CGSizeMake(100, 100)
box.position = CGPointMake(500, 280)
self.addChild(box)
}
Alex Romanillos
16,144 PointsIn the Xcode6 Sprite Game boilerplate they added a visual interface in the GameScene.sks file, that works almost the same as the Storyboard for interface building. So in this case to add the SpriteNode you can do it using the GameScene.sks tool.
In the GameViewController.m the GameScene object is loaded from the GameScene.sks file, as seen in the code below.
@implementation SKScene (Unarchive)
+(instancetype)unarchiveFromFile:(NSString *)file {
/* Retrieve scene file path from the application bundle */
NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"];
/* Unarchive the file to an SKScene object */
NSData *data = [NSData dataWithContentsOfFile:nodePath
options:NSDataReadingMappedIfSafe
error:nil];
NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[arch setClass:self forClassName:@"SKScene"];
SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey];
[arch finishDecoding];
return scene;
}
@end
@implementation GameViewController
-(void)viewDidLoad{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
// Create and configure the scene.
GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
eric hughes
12,345 Pointscould somebody help me with this, I removed the GameScene.sks file from my project and copy pasted this code into the gameviewcontroller.m file and my build wont run. Doing this course on xcode 6 is very confusing, this is the error that I have inside my console even though the build is successful
2015-01-08 16:11:34.608 SpaceCat[6045:8243532] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_NSPlaceholderData initWithContentsOfFile:options:error:]: nil file argument'
Simone Giuditta
1,443 PointsSimone Giuditta
1,443 PointsHi, thank you for answering =) I solved the problem deleting all the code in the project that was different from the iOS 7 SpriteKit template and the .sks file. Then I initialized the gamescene as it is in iOS 7 and everything started working ;)
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsAwesome. Glad you got it to work.
Alejandro Chiari
1,981 PointsAlejandro Chiari
1,981 PointsSimone how did you do that? I'm doing the same course and had the same problem but didn't understand what you did.
Thanks