jess
jess

Reputation: 3

SpriteKit scene in a single view application?

I'm creating a minigame app on Xcode, using the single view app format. However, I'm hoping to add a brick-laying game in one of the Views- which would greatly benefit from the physics features in SpriteKit. I have tried using this code from a previous question asked on here, implementing my own scene:

let scene = Brick(fileNamed: "Brick")
scene?.scaleMode = .aspectFill
self.view?.presentScene(scene!, transition: SKTransition.flipHorizontal(withDuration: 0.42))

This gives me an error that UIKit does not contain the method of ".presentScene" no matter how many imports I add. I have also tried to use the SpriteKit View but I am honestly not sure how it works and Google isn't helping. TLDR: How do I put a SpriteKit Scene minigame in a single view app using ViewControllers and segues etc??

Upvotes: 0

Views: 169

Answers (1)

Vym
Vym

Reputation: 543

Assuming you're in a regular UIViewController, then your view is of type UIView and it, indeed, doesn't know about the presentScene method, which is part of SKView. You have to use an SKView for presenting your scene.

Assuming you want to add one and have it be the size of your view controller's view:

let scene = Brick(fileNamed: "Brick")!
scene.scaleMode = .aspectFill
let skView = SKView(frame: view.frame)
view.addSubview(skView)
skView.presentScene(scene, transition: SKTransition.flipHorizontal(withDuration: 0.42))

Or use Interface Builder to add an SKView to your view controller UI, then connect it with an IBOutlet.

Upvotes: 1

Related Questions