2016-04-14 25 views
0

在Xcode項目中使用SpriteKit,我可以使用更新功能在幀之間更新我需要的所有內容。如何在Swift Playground中使用更新功能

例如

override func update(currentTime: CFTimeInterval) { 
    /* Called before each frame is rendered */ 
} 

我該如何在Xcode Playground中做同樣的事情?

我不認爲這是一個重複NSTimer.scheduledTimerWithTimeInterval in Swift Playground

/// UPDATE ///

我發現,我可以添加一個類,因此覆蓋類來獲得,如更新功能所以。

任何其他代碼如

//: Playground - noun: a place where people can play 

    import SpriteKit 
    import XCPlayground 

    class PrototypeScene: SKScene { 

    override func update(currentTime: CFTimeInterval) { 

     print("update" ); 
     callFunctionOutsideClass(); 

    } 
    } 

func callFunctionOutsideClass(){ 
} 

下一個問題是範圍之前添加該代碼。我如何從更新函數中調用Playground主體中的函數?

如果我嘗試任何屬性添加到類我得到一個錯誤 - 「錯誤:類‘PrototypeScene’沒有初始化」

感謝

回答

1

這裏是工作的代碼示例。您的課程需要一個初始化程序才能設置屬性!有些課程需要required init(如下所示)(這是SKScene的情況)

import SpriteKit 
import XCPlayground 

class PrototypeScene: SKScene { 

    var blah: String? 
    var foo : Int! 
    var boo = 2 

    init(blah: String) { 
    super.init() 
    self.blah = blah 
    self.foo = 1 
    } 

    required init(coder aDecoder: NSCoder) { 
    super.init() 
    } 

    override func update(currentTime: CFTimeInterval) { 

     print("update") 
     callFunctionOutsideClass() 

    } 


} 

func callFunctionOutsideClass(){ 
} 
相關問題