2014-06-19 70 views
3

我嘗試在助理編輯器中使用spriteKit在操場上顯示某些東西。然而,沒有顯示。以下是代碼。如果有任何人可以顯示結果(藍色矩形),請通知我。如果不是,請找出問題所在。爲什麼它不能在swift 2中顯示任何東西?

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) 
+0

如何訪問您的遊樂場的TouchesBegan? –

回答

3

您可以通過點擊QuickLook的(眼),或盤旋加按鈕,在側邊欄查看視圖的當前狀態。但要查看SpriteKit場景的動畫,您可能需要實時視圖。爲此,您需要XCPlayground框架。

import XCPlayground 
XCPShowView("my SpriteKit view", view) 

有關詳細信息,在Xcode幫助閱讀了關於Exploring and Evaluating Swift Code in a Playground或觀看WWDC 2014 session 408: Swift Playgrounds

+1

btw'XCPlayground'是OSX Only –

+0

這裏我使用UIKit,它可能不能在OSX中運行,它可以運行XCPlayground。我試了按鈕,但沒有發生任何事情。 – Peterxwl

+2

SpriteKit是跨平臺的,因此您可以在OS X遊樂場中創建原型,然後將代碼粘貼到iOS項目中。 (注意一些注意事項,其中一些可以使用'SKColor'代替'UIColor'處理。) – rickster

0

由於XCPShowView已在Xcode 7.1中棄用,請改爲在PlaygroundSupport中使用PlaygroundPage。以下是在Playground中使用SpriteKit的示例,其靈感來源於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