How do I get SpriteKit Playgrounds working in Xcode 8 beta?
Asked Answered
N

1

6

I want to be able to easily prototype my Sprite Kit code in Playgrounds. This works fine in Xcode 7

import UIKit
import SpriteKit
import XCPlayground


let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 480, height: 320))

let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.presentScene(scene)
XCPlaygroundPage.currentPage.liveView = sceneView

let square = SKSpriteNode(imageNamed: "square")
square.name = "shape"
square.position = CGPoint(x: scene.size.width * 0.25, y: scene.size.height * 0.5)

let circle = SKSpriteNode(imageNamed: "circle")
circle.name = "shape"
circle.position = CGPoint(x: scene.size.width * 0.50, y: scene.size.height * 0.5)

let triangle = SKSpriteNode(imageNamed: "triangle")
triangle.name = "shape"
triangle.position = CGPoint(x: scene.size.width * 0.75, y: scene.size.height * 0.5)

scene.addChild(square)
scene.addChild(circle)
scene.addChild(triangle)

In Xcode 7 I get a nice live view as such enter image description here

This just doesn't work in Swift 3 and Version 8.0 beta 3 (8S174q). What do I have to change?

Neiman answered 23/7, 2016 at 15:8 Comment(0)
M
9

The XCPlayground module has been replaced by the PlaygroundSupport:

import PlaygroundSupport
import SpriteKit

let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 480, height: 320))

let scene = SKScene(size: CGSize(width: 480, height: 320))
sceneView.showsFPS = true
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView

let square = SKSpriteNode(imageNamed: "square")
square.name = "shape"
square.position = CGPoint(x: scene.size.width * 0.25, y: scene.size.height * 0.5)

let circle = SKSpriteNode(imageNamed: "circle")
circle.name = "shape"
circle.position = CGPoint(x: scene.size.width * 0.50, y: scene.size.height * 0.5)

let triangle = SKSpriteNode(imageNamed: "triangle")
triangle.name = "shape"
triangle.position = CGPoint(x: scene.size.width * 0.75, y: scene.size.height * 0.5)

scene.addChild(square)
scene.addChild(circle)
scene.addChild(triangle)
Margay answered 25/7, 2016 at 15:43 Comment(6)
Ok, this is good. But how do I then SEE the SKView? I just have the normal right column with the results of variables etc. No view of an SKView.Jodijodie
Found it... open "Assistant Editor". The most perfectly illogical way. Just like the keyboard shortcut for stopping and playing a playground.Jodijodie
It can be confusing. You'll also want to make sure that the Assistant Editor is displaying the Timeline mode to see your updates. Sometimes Xcode doesn't update the AE when you're switching back to the page with your live view.Margay
I've found the performance of Playgrounds "assistant editor" view to be so bad I've headed back to dealing with projects and building to device. Perhaps good for positioning, but no good for testing out animations.Jodijodie
@Margay about github.com/mfessenden I have open an issue, can you take a look pls?Budget
what's the issue?Margay

© 2022 - 2024 — McMap. All rights reserved.