How to switch from UIView to SKScene in iOS

I want to develop a game using SpriteKit on the Apple iOS platform. My game has a settings page containing a shortcut, button, table view, ... and a game page. In my test, the first page is the setting and when the player clicks "Start", the game will begin. I donโ€™t know how to go to the game scene, and I donโ€™t know where my problem is, maybe because I am new to SpriteKit. My settings page is the start page in the storyboard, and by default it connected to the GameViewController, and when I click the "Start" button, I get an error message on the last line, and the program stops. I want to load my game scene into a new view, but I have a 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 mistake:

2016-01-21 06: 06: 40.201 test_myGame [605: 5321] - [UIView presentScene:]: unrecognized selector sent to the instance 0x7fd809fcf570 2016-01-21 06: 06: 40.207 test_myGame [605: 5321] *** Termination application due to the non-displayable exception "NSInvalidArgumentException", reason: '- [UIView presentScene:]: unrecognized selector sent to instance 0x7fd809fcf570'

+5
source share
1 answer

You need to make sure that the representation of your GameController is an SKView, not a UIView.

You can check the storyboard:

Select the ViewController view (left) and check the type (on the right).

enter image description here

In my example, I created a custom GameMainView class that inherits from SKView.

+3
source

Source: https://habr.com/ru/post/1241065/


All Articles