【问题标题】:How do I use the update function in Swift Playground如何在 Swift Playground 中使用更新功能
【发布时间】:2016-04-14 12:56:52
【问题描述】:

在 Xcode 项目中使用 SpriteKit,我可以使用更新功能来更新我在帧之间需要的所有内容。

例如

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

如何在 Xcode Playground 中做同样的事情?

我不认为这是NSTimer.scheduledTimerWithTimeInterval in Swift Playground的重复

/// 更新 ///

我发现我可以添加一个类,因此可以重写该类以获得更新功能。

在任何其他代码之前添加此代码,例如

//: 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' 类没有初始化器”

谢谢

【问题讨论】:

    标签: swift sprite-kit swift-playground


    【解决方案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(){
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多