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?

Corey F
  • 621
  • 4
  • 14

1 Answers1

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)
mfessenden
  • 598
  • 4
  • 12
  • 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. – Confused Oct 27 '16 at 02:28
  • 2
    Found it... open "Assistant Editor". The most perfectly illogical way. Just like the keyboard shortcut for stopping and playing a playground. – Confused Oct 27 '16 at 04:16
  • 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. – mfessenden Oct 27 '16 at 15:10
  • 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. – Confused Oct 28 '16 at 12:30
  • @mfessenden about github.com/mfessenden I have open an issue, can you take a look pls? – Alessandro Ornano Dec 16 '16 at 12:10
  • what's the issue? – mfessenden Dec 16 '16 at 14:38