How to Indicate a View Which Has Created with Uikit from a Skscene

How can I indicate a view which has created with UIKit from a SKScene?

This sounds like a job for the delegate pattern.
Add a protocol for LevelScene;

// LevelScene.swift
protocol LevelScene : class {
var gameDelegate: GameDelegate? { get set }
}

Add a protocol for GameDelegate;

// GameDelegate.swift
protocol GameDelegate : class {
func gameOver()
}

In your mainScene add the protocol reference and create a property called gameDelegate;

class MainScene: SKScene, LevelScene {
weak var gameDelegate: GameDelegate?
}

In your GameViewController add the protocol reference and implement the required protocol function - in this case it's called gameOver and seque to your UIKit View as usual;

class GameViewController: UIViewController, GameDelegate {
func gameOver() {
self.performSegue(withIdentifier: ExitGameSegueKey, sender: self)
}
}

Set the delegate to gameViewController when presenting the scene;

scene.gameDelegate = self

Then in mainScene call the delegate function when required;

self.gameDelegate?.gameOver()

How To Present a SKScene from a UIViewController

I think I might have found a solution-I didn't present the GameScene directly from the MenuViewController, but instead, when the playButton is pressed I transitioned to the GameViewController and then presented the GameScene from there.

I don't know why it doesn't work in the MenuViewController, but this method works.

Is it possible to use a SKScene with a UIView Controller?

Is it possible to use a SKScene with a UIView Controller?

Yes. You're probably doing it already without realizing it.

The bridge between SpriteKit and Cocoa Touch's view architecture is SKView. A lot of SpriteKit-based apps just have one view controller whose view is a SKView, and then they handle different phases of the game by doing all their drawing in SpriteKit and just changing sprite scenes. But you don't have to work that way -- you can mix SKView with other views and controls, and you can use as many view controllers as you like.

Here's a shot of a SKView inside another view and managed by a view controller that's embedded in a navigation controller:

Hello, world!

The purple area is a plain old UIView -- only the black area is a SpriteKit view.

Is there a way I can show a ball that uses CoreMotion to move without using SKScene?

Yes. If all you want to do is to move a ball across the screen, the UIKit animation built into UIView is almost certainly more than enough.

Adding an SKScene to a UIViewController?

  1. Add an SKView to your viewController.
  2. Add an SKScene to your SKView.

You can overlay UIKit items from inside your SKScene. This is how I use collection and table views in my games. However they would need to be added as a subview on the SKView holding your SKScene.

SKScene has a view property that should hold a reference to the SKView its contained in. So it would just be a matter of doing something like this.

class MyScene: SKScene {
var someView: UIView

override func didMoveToView(view: SKView) {
someView = //setup view here
view.addSubview(someView)
}
}

Then inside this scene you can access your UIKit objects wherever you like.

Segue in SKScene to UIViewController

You are calling the segue on the root viewController. I think that is the problem. You need to call the segue on the scene's viewController instead (where I am assuming you have created the segue, hence it is not being found on the root viewController).

Now the problem is that an SKScene does not have direct access to it's viewController, but just the view in which it is contained. You need to create a pointer to it manually. This can be done by creating a property for the SKScene:

class GameScene: SKScene {
weak var viewController: UIViewController?
...
}

Then, in the viewController class, just before skView.presentScene(scene)

scene.viewController = self

Now, you can access the viewController directly. Simply call the segue on this viewController:

func returnToMainMenu(){
viewController?.performSegueWithIdentifier("menu", sender: vc)
}

Swift - Overlaying a SKScene onto a UIView - Grey Background

Note the view of your GameViewController:

Sample Image

It is by default an SKView:

Sample Image

Change the class of it to UIView and you will be good. By the way, setting the backgroundColor on an SKView has no effect.



Related Topics



Leave a reply



Submit