In GameScene.sks I have created a Color Sprite with a Texture and provided a Name “hero”, within the Custom Class tab I have put “CharacterNode” class.
In a file “CharacterNode.swift” (the Character class), I just hold BOOL properties relating to the hero (e.g. walkLeft, walkRight, actionEnabled..). The class is defined as:
class CharacterNode: SKSpriteNode {
var left = false
var right = false
var actionEnabled = false
//
}
In a file “PlayerControlComponent.swift”:
class PlayerControlComponent: GKComponent, ControlInputDelegate {
var cNode: CharacterNode?
//
}
I create an instance of the Character class.
Within the PlayerControlComponent file I have a switch statement, depending on which buttons are pressed on screen, the flags in the Character class (cNode) “walkLeft”, “walkRight”, “actionEnabled”.. get set to true e.g “cNode?.walkLeft = true”
func follow(command: String?) {
if (cNode != nil){
switch(command!){
case "circle":
print("PlayerControlComponenet circle pressed")
cNode?.actionEnabled = true
case "cancel circle","stop circle":
cNode?.actionEnabled = false
print("PlayerControlComponenet circle unpressed")
case("left"):
cNode?.left = true
case "cancel left","stop left":
cNode?.left = false
When buttons are released the flag/s get set to false. The movement all works fine, the issue I have is with the actionEnabled button.
Back in the GameScene.swift file, I want to be able to retrieve what the actionEnabled flag is currently set as (e.g true or false). The reason being is the file is also the SKPhysicsContactDelegate, and I need to know if the hero is near a certain object (which I have working fine), AND the actionEnabled button is currently pressed.
I am aware if I create an instance of the Character class in GameScene.swift, that is a separate instance to the PlayerControlComponent uses, so the flag updates will not be available.
In GameScene I have also tried:
var playCC = PlayerControlComponent()
Then as a test in:
override func update(_ currentTime: TimeInterval) {
let latestAction = playCC.cNode?.actionEnabled
print("action is: \(String(describing: latestAction))")
The output shows "action is: nil” repeatedly (I expected it to show false, then true when i press the action button)
How do I retrieve the values of the CharacterNode class being used in the PlayerControlComponent, from within GameScene.swift?