Im having a problem. When i present a new scene in spritekit (with objective c) It only shows a gray color with node and fps count on the screen.
I have animated the scene in the .sks file so it should not look like this. The transition when im presenting the new scene also works, but it just wont show the images and background that i animated in the .sks file.
Here is the code on how i do it.
//In gameviewcontroller.m
// Creating and configuring the scene.
GameScene *Level1 = [GameScene nodeWithFileNamed:@"GameSceneLevel1"];
Level1.scaleMode = SKSceneScaleModeAspectFill;
// GameScene.m inside touchesbegan
SKTransition *reveal = [SKTransition doorwayWithDuration:4];
GameScene *Level1 = [GameScene sceneWithSize:self.view.bounds.size];
Level1.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:Level1 transition:reveal];
You have originally loaded scene from .sks file and as you said you have some animations there. Later on, (inside touchesBegan
) you are creating a scene by providing a size (using sceneWithSize
static method). So the scene is not loaded from from the archive.
To fix the problem, load your scene like you did initially, in the GameViewController
.
EDIT:
To transition between two scenes you have to create two .sks
files. You can name them like this : MenuScene and GameScene (you already have this one by default). Note that when you create these files you don't have to write an extension (.sks
) but rather just file name. Then you have to create corresponding .m
and .h
files. So create MenuScene.m
and MenuScene.h
. GameScene
.m
and .h
files are there by default.
MenuScene
should be a subclass (not necessarily direct subclass) of an SKScene
.
Then inside of your view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
GameScene *scene = [GameScene nodeWithFileNamed:@"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
scene.size = skView.bounds.size;
[skView presentScene:scene];
}
Later on in your GameScene's touchesBegan
method if you want to transition to the MenuScene
, you should do something like this :
MenuScene *nextScene = [MenuScene nodeWithFileNamed:@"MenuScene"];
SKTransition *transition = [SKTransition fadeWithDuration:3];
[self.view presentScene:nextScene transition:transition];
Similarly, to transition back to the GameScene
, in your MenuScene's touchesBegan
, do this:
GameScene *nextScene = [GameScene nodeWithFileNamed:@"GameScene"];
SKTransition *transition = [SKTransition fadeWithDuration:3];
[self.view presentScene:nextScene transition:transition];
Of course right before each transition you can set scene's scale mode to match the scale mode of the old scene.
From your code, it looks like you are trying to make a transition to the same scene: GameScene
-> GameScene
. That is certainly possible, but are you sure you wanted that ?
If so, just use the code I've provided for MenuScene's touchesBegan
method.