Search code examples
iosswiftobjectnodes

Obtain reference to object from node using touchesBegan method


Essentially I have a game that requires me to call two methods that are contained in my objects that extend the skpritenode class. I want to use the touches began method to detect the node touches, pull the referent to the object from that node, and then assign the reference to a temporary variable inside the touchesBegan method that would allow me to call its methods. Sorry if this is unclear, I am still trying to get used to the "right" way to formulate my questions.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if(atPoint((touch?.location(in: self))!) == homeButton) && scene?.isPaused == false{
        goToHomeG()
    }

    else {
        if let location = touch?.location(in: self){
            let theNodes = nodes(at: location)
            for node in  theNodes{
                if node.name == "yesText" {
                    let transition = SKTransition.flipHorizontal(withDuration: 0.5)
                    let menuSceneOB = MenuScene(fileNamed: "MenuScene")
                    menuSceneOB?.scaleMode = .aspectFill
                    self.view?.presentScene(menuSceneOB!, transition: transition)
                    node.removeFromParent()
                }
                else if node.name == "noText" {
                    for child in self.children {
                        if child.name == "goToHomeText"{
                            child.removeFromParent()
                        }
                        if child.name == "noText"{
                            child.removeFromParent()
                        }
                        if child.name == "yesText"{
                            child.removeFromParent()
                        }
                        if child.name == "box"{
                            child.removeFromParent()
                        }
                    }
                    scene?.isPaused = false
                }
                else if node.name == "enemy"{
                    //here is where I want to detect if the object is an enemy and then use the assignment to the location to modify the object with its own internal methods.
                    var modify : EnemyChar?
                    modify = node
                    modify.update
                }
            }
        }
    }

}

Solution

  • If I understood correctly, you just need to cast the node:

    else if node.name == "enemy"{
        //here is where I want to detect if the object is an enemy and then use the assignment to the location to modify the object with its own internal methods.
        var enemy = node as? EnemyChar
        enemy.update()
    }
    

    By the way, this code:

    for child in self.children {
        if child.name == "goToHomeText"{
            child.removeFromParent()
        }
        if child.name == "noText"{
            child.removeFromParent()
        }
        if child.name == "yesText"{
            child.removeFromParent()
        }
        if child.name == "box"{
            child.removeFromParent()
        }
    }
    

    can be simplified to:

    for child in self.children {
        if ["goToHomeText", "noText", "yesText", "box"].contains(child.name) {
            child.removeFromParent()
        }
    }