Swift: Skspritekit, Using Storyboards, Uiviewcontroller and Uibutton to Set in Game Parameters

Swift: returning to Main Menu after condition is met in GameScene using StoryBoards and UIViewControllers?

You could use the userData instance property of your next scene to store the current scene, something like:

nextScene.userData = NSMutableDictionary()
nextScene.userData?.setObject(actualScore, forKey: "actualScore" as NSCopying)

and when you are in the next scene you can ask to this NSMutableDictionary who is the previous scene as:

if let actualScore = self.userData?.value(forKey: "actualScore") {
print("well done!, the score was: \(actualScore)")
}

Swift3: adding a UIAlertController in a SKView

UIAlert is an UIKit element, so you can easily present where do you want in your SKScene take references from your view with few code..
In other words you can access to the main window that display your app’s content, then use the rootViewController that provides the content view of the window.

Code sample:

if sprite.contains(touchLocation) {
print("You tapped the blue sprite")
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction(title: "Ok", style: .default) { action in
// Handle when button is clicked
}
alert.addAction(action)
if let vc = self.scene?.view?.window?.rootViewController {
vc.present(alert, animated: true, completion: nil)
}
}

Swift: How to handle view controllers for my game

Your way seems very complicated to essentially present 3 scenes. Its not what you are supposed to do for SpriteKit games, you only really need 1 view controller (GameViewController).

Load your first scene from GameViewController (e.g HomeScene) and nothing else.
Create your playButton and other UI directly in HomeScene. Use SpriteKit APIs for your UI (SKLabelNodes, SKNodes, SKSpriteNodes etc).

You should never really use UIKit (UIButtons, UILabels) in SpriteKit. There are some exceptions to this, like maybe using UICollectionViews for massive level select menus, but basic UI should be done with SpriteKit APIs.

There is plenty tutorials to google on how to create sprite kit buttons, how to use SKLabelNodes etc. Xcode has a SpriteKit level editor so you can do all that visually similar to storyboards.

Than from HomeScene transition to the LevelSelect Scene and than to the GameScene and vice versa. Its super easy to do.

/// Home Scene
class HomeScene: SKScene {

...

func loadLevelSelectScene() {

// Way 1
// code only, no XCode/SpriteKit visual level editor used
let scene = LevelSelectScene(size: self.size) // same size as current scene

// Way 2
// with xCode/SpriteKit visual level editor
// fileNamed is the LevelSelectScene.sks you need to create that goes with your LevelSelectScene class.
guard let scene = LevelSelectScene(fileNamed: "LevelSelectScene") else { return }

let transition = SKTransition.SomeTransitionYouLike
view?.presentScene(scene, withTransition: transition)
}
}

/// Level Select Scene
class LevelSelectScene: SKScene {
....

func loadGameScene() {

// Way 1
// code only, no XCode/SpriteKit visual level editor used
let scene = GameScene(size: self.size) // same size as current scene

// Way 2
// with xCode/SpriteKit visual level editor
// fileNamed is the GameScene.sks you need to create that goes with your GameScene class.
guard let scene = GameScene(fileNamed: "GameScene") else { return }

let transition = SKTransition.SomeTransitionYouLike
view?.presentScene(scene, withTransition: transition)
}
}

/// Game Scene
class GameScene: SKScene {
....
}

I strongly recommend you scratch your storyboard and ViewController approach, and just use different SKScenes and 1 GameViewController.

Hope this helps

How do I use storyboards with spriteKit using swift

A SpriteKit Scene is presented on an instance of a SKView, which is a subclassed UIView.

For an iOS game made using SpriteKit, one needs to have at least one viewController set up, either programatically in the App delegate or in a storyboard, on which the SKScene can be displayed. It is on the main view of this VC that a SKScene will be presented in.

So if you are using a storyboard, the iOS game will have to instantiate the root viewController from it. You can easily design your UI on the viewController, and present the game from the code on the press of a button, either in the same viewController or a new one. All this will be evident once you read a beginner tutorial for SpriteKit using Swift like this.

Let's say your root viewController has your main menu (on another view called menuView), with a play button on it. Now presenting a game on the press of a button would look something like this:

class MyViewController: UIViewController {
@IBOutlet wear var menuView: UIView?
@IBOutlet weak var playButton: UIButton?

@IBAction func likedThis(sender: UIButton) {
//Hide the menu view
menuView.hidden = true

//instantiate and present the scene on the main view
let scene = MyScene(size: view.bounds.size)
let skView = self.view as SKView
skView.presentScene(scene)
}
}

As for going back to the main menu from the scene, have a look at this answer.

Segue from SKScene and back again cause game to freeze

Performing a segue will present a new view on top of the stack. By using a segue to return to the main menu, the app will create a new main menu on top of the UIViewController. I can't say for certain why your app freezes. But I would guess that there are some properties or functions that don't work properly when a new main menu is created and presented from your UIViewController.

I recommend unwinding the segue used to reach the UIViewController instead. This way you can return to the previous view and conserve memory.

You'll need to write an IBAction in your destination view, the main menu in this case:

@IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {
}

Then ctrl-drag from your button in the UIViewController to the exit icon in storyboard. You should see an option to use the IBAction unwindToMainMenu that you just wrote. This will create an unwind segue.

A complete guide to unwind segues and multiple examples can be found in this answer: What are Unwind segues for and how do you use them?

Hiding UIButton from GameScene

First you want to make a reference to the button when you assign the scene. You want to make sure you create an IBOutlet from your storyboard to your view controller as well.

In your game's view controller before you present the scene.

yourGameScene.button = _yourViewControllerButton;

Declare the button reference in your scene header.

@property UIButton *button;

Then in your implementation of the scene make the button hidden by typing...

_button.hidden = YES;

And make it appear by typing...

_button.hidden = NO;

Edit in more detail:

First you want to make sure that you have an IBOutlet connected to your view controller. The easiest way for someone starting out to do this is by switching to the assistant editor and then holding control button down and then clicking on your game button and dragging it into the interface section of the game view controller class. It will make a line. (Make sure it is the right view controller on your second view and make sure to choose outlet, not action, if it prompts.)

assign ib outlet

Secondly you want to add a property in your game scene header. You can name it whatever you want but for the tutorial sake just name it button and change it around later if you wish.

@property UIButton *button;

Third, when you created this game scene and started assigning properties, go ahead and set the view controller button reference to the game scene button reference.

view controller implementation

Finally, do whatever you want to implement the hiding and the showing. For me I made it by toggling it in the touchesBegan method.

game scene implementation
off
on



Related Topics



Leave a reply



Submit