First I would like to say that I am completely self taught following as many tutorials on the internet that I can find, so apologies in advance if there are certain concepts Im not too familiar with.
Alright with that out of the way I'd like to ask help on something that's been stumping me for days.
I have a reference node(referenceNode) in a SpriteKit Scene(GameScene),
That reference node references another SpriteKit Scene( referenceScene),
and this scene has a Custom class(referenceClass.swift).
I have a boolean variable in referenceClass.swift called trueOrFalse, and I want to be able to change the vale from my code in gameScene.swift.
I have tried casting from the SceneEditor to my code by creating instances as you would do SKSpriteNode's and SKLableNodes but this does not seem to be working
class GameScene: SKScene
{
var referenceNode: SKReferenceNode! //link to SceneEditor Reference Node
override func didMove(to view: SKView)
{
referenceNode = self.childNode(withName: "referenceNode") as! SKReferenceNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
referenceNode.trueOrFalse = false
}
}
this is my custom class
import UIKit
import SpriteKit
class referenceClass: SKScene
{
var trueorfalse:Bool = true
func oneFunction()
{
}
}
This is just a small example project that I made so I could get my head around the concept trying the different solutions I could find but with no success. I realise I could create global variables but I am trying to keep my code Clean as In all functionality related to this node to be kept in this referenceClass. Also I've been told its not good programming?
Any help would be greatly appreciated :)
Barney
First of all I would check whether the search is done properly - https://developer.apple.com/documentation/spritekit/sknode/searching_the_node_tree
For instance, in the template SpriteKit project nodes are being searched with
//
prefix, i.e.//referenceNode
in your case.Secondly, do not force-cast it to
SKReferenceNode
immediately, first check what kind of object is returned (if any).