1

I've a method in my SKScene as:

  func someTypeMethod() {
      print("someTypeMethod...")
  }

I tried to call it from my 'ViewController' as:

  if let scene = SKScene(fileNamed: "Scene") {
      scene.someTypeMethod()
  } 

But Xcode is saying 'Value of type SKScene has no member 'scene.someTypeMethod'

I've even tried to make the method 'public' and 'class'. But still the error appears. So, how can I call such methods in my 'SKScene' from its 'ViewController'?

Please help...

Thampuran
  • 644
  • 5
  • 22

1 Answers1

1

Is someTypeMethod() declared on an extension of SKScene or on a subclass?

If it is on a subclass, you need to change to

  if let scene = SKScene(fileNamed: "Scene") as? MySKSceneSubclass {
      scene.someTypeMethod()
  } 

The compiler only knows that scene has the native SKScene methods if you don't tell it which proper subclass it's expecting. Making it a class method means you'd call SKScene.someTypeMethod(), rather than scene.someTypeMethod(), but then it won't have access to your scene's data.

Jake T.
  • 4,308
  • 2
  • 20
  • 48