0

Using SpriteKit in Xcode projects, I am able to use the update function to update everything I need to in between frames.

e.g.

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

How can I do the same thing in Xcode Playground?

I do not think that this is a duplication of NSTimer.scheduledTimerWithTimeInterval in Swift Playground

/// UPDATE ///

I have discovered that I can add a class and therefore override the class to get the update functionality like so.

Add this code before any other code eg

//: 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(){
}

The next issue is scope. How do I call a function in the body of the Playground from the update function I have overridden?

If I try to add any properties to the Class I get an error - "error: class 'PrototypeScene' has no initializers"

Thanks

Community
  • 1
  • 1
gingrrr
  • 87
  • 1
  • 8

1 Answers1

1

Here is an example of working code. Your class needs an initializer in order to set properties! Some classes need the required init like the one shown below (This is the case for 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(){
}
Juan
  • 21
  • 2