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:
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?
- Add an SKView to your viewController.
- 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
:
It is by default an SKView
:
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
iOS Swift Remove Uitableview Cell Separator Space
My Reachability Notifier Is Only Able to Be Called Once
Array Element Checking in Swift
How to Authorize Twitter with Swifter
Skaction Works in Didmovetoview But Doesn't Works in Function
Table View Controller Duplicate Itself in iOS Swift
Collectionview Flowlayout Overlapping When Scrolling Not Working
Show More Button Next to End of Text Swift
Uitableviewcell Initwithstyle:Uitableviewcellstylesubtitle Is Not Working
Wkwebview: How to Preload Multiple Urls
How to Adjust the Uitableviewcell Height to the Content of Uitextview That's Inside
Stream Data from Network in Avaudioengine, Is It Possible
How to Read References Given by Ptr_Refs in iOS
iOS How to Restart App for Changing Language Swift 4
How to Remove Single Object in Array from Multiple Matching Object
How to Change How a Remote Notification Is Presented Before Presentation
Swift Good Coding Practice: If Statement with Optional Type Bool
How to Draw a Just the Corners of a Rectangle (Without Lines Connecting Them)