Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How transfer from UIView to SKScene in iOS

I want to develop a game with SpriteKit on Apple iOS platform.My game has a setting page that contains label,button,table view ,... and a game page.In my test, the first page is the setting and when player press "Start", the game will begin.I do not know how transfer to the game scene and i do not know where is my problem , perhaps because i am new to SpriteKit. My setting page is a start page in storyboard and by default it was connect to GameViewController and when i press the "Start" button , i get error at the last line and the program will stop. I want to load my game scene in a new view but i have problem. Here is my code:

-(IBAction)btn_startGame:(id)sender
{
    SKView* skView = (SKView*)self.view;

    SKScene* obj_gameScene = [MyScene sceneWithSize:skView.bounds.size];

    obj_gameScene.scaleMode = SKSceneScaleModeAspectFill;

    SKTransition *transition = [SKTransition flipVerticalWithDuration:0.5];
    [skView presentScene:obj_gameScene];

}

This is the error:

2016-01-21 06:06:40.201 test_myGame[605:5321] -[UIView presentScene:]: unrecognized selector sent to instance 0x7fd809fcf570 2016-01-21 06:06:40.207 test_myGame[605:5321] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x7fd809fcf570'

like image 389
Mahdi Amrollahi Avatar asked Oct 18 '22 17:10

Mahdi Amrollahi


1 Answers

You have to make sure that the view of your GameController is an SKView instead an UIView.

You can check in the storyboard:

Select the View of the ViewController (at the left side) and check the type (in the right side).

enter image description here

In my example I've created a custom class GameMainView which inherits from SKView.

like image 79
Stefan Avatar answered Nov 01 '22 16:11

Stefan