问题
I try to use spriteKit to show something in playground in assistant editor. However, nothing showed. Below is the code. And if any one can show the results (a blue rectangle), please inform me. If not, please figure out where the problem is.
import UIKit
import SpriteKit
let view:SKView = SKView(frame: CGRectMake(0, 0, 1000, 800))
let scene:SKScene = SKScene(size: CGSizeMake(1000, 800))
scene.scaleMode = SKSceneScaleMode.AspectFit
let blueBox: SKSpriteNode = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(300, 300))
blueBox.position = CGPointMake(512, 384)
scene.addChild(blueBox)
view.presentScene(scene)
回答1:
You can see the current state of a view by clicking the QuickLook (eye) or circled-plus button in the sidebar. But to see your SpriteKit scene animate, you probably want a live view. For that, you need the XCPlayground framework.
import XCPlayground
XCPShowView("my SpriteKit view", view)
For details, read up on Exploring and Evaluating Swift Code in a Playground in Xcode Help or watch WWDC 2014 session 408: Swift Playgrounds.
回答2:
As XCPShowView
was deprecated in Xcode 7.1, use PlaygroundPage
in PlaygroundSupport
instead. The following is an example of using SpriteKit in Playground which is inspired from this.
import SpriteKit
import PlaygroundSupport
// Create the SpriteKit View
let view = SKView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
// Create the scene and add it to the view
let scene = SKScene(size: CGSize(width: 500, height: 500))
scene.scaleMode = SKSceneScaleMode.aspectFit
scene.backgroundColor = .white
view.presentScene(scene)
// Add a red box to the scene
let redBox = SKSpriteNode(color: SKColor.red, size: CGSize(width: 200, height: 200))
redBox.position = CGPoint(x: 250, y: 250)
redBox.run(SKAction.repeatForever(SKAction.rotate(byAngle: -5, duration: 5)))
scene.addChild(redBox)
// Show in assistant editor
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
来源:https://stackoverflow.com/questions/24317153/why-it-cannot-show-anything-in-playground-in-swift-2