Detecting touching a SKSpriteNode within a touchesBegan event?

31 views Asked by At

Detecting touching a SKSpriteNode within a touchesBegan event?

My experience to date has focused on using GamepadControllers with Apps, not a touch-activated iOS App.

Here are some short code snippets:

Note: the error I am trying to correct is noted in the very first snippet = touchesBegan within the comment <== shows "horse"

Yes, there is a "horse", but it is no where near the "creditsInfo" SKSpriteNode within my .sksfile =

enter image description here

Please note that this "creditsInfo" SKSpriteNode is programmatically generated by my addCreditsButton(..) and will be placed very near the top-left of my GameScene.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    if let ourScene = GameScene(fileNamed: "GameScene") {
        
        if let touch:UITouch = touches.first {
            let location = touch.location(in: view)
            let node:SKNode = ourScene.atPoint(location)
            
            print("node.name = \(node.name!)")   // <== shows "horse"
            if (node.name == "creditsInfo") {
                showCredits()
            }
        }
        
    }   // if let ourScene
    
}   // touchesBegan

The above touchesBegan function is an extension GameViewController which according to the docs is okay, namely, touchesBegan is a UIView method besides being a UIViewController method.

Within my primary showScene() function, I have:

    if let ourScene = GameScene(fileNamed: "GameScene") {

#if os(iOS)
    addCreditsButton(toScene: ourScene)
#endif

     }

with:

func addCreditsButton(toScene: SKScene) {
            
    if thisSceneName == "GameScene" {
                    
        itsCreditsNode.name = "creditsInfo"

        itsCreditsNode.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        
        itsCreditsNode.size = CGSize(width: 2*creditsCircleRadius,
                                     height: 2*creditsCircleRadius)
        itsCreditsNode.zPosition = 3
        creditsCirclePosY = roomHeight/2 - creditsCircleRadius - creditsCircleOffsetY
        creditsCirclePosX = -roomWidth/2 + creditsCircleRadius + creditsCircleOffsetX
        itsCreditsNode.position = CGPoint(x: creditsCirclePosX,
                                          y: creditsCirclePosY)
        
        toScene.addChild(itsCreditsNode)
                    
    }   // if thisSceneName

}   // addCreditsButton

To finish, I repeat what I stated at the very top:

The error I am trying to correct is noted in the very first snippet = touchesBegan within the comment <== shows "horse"

1

There are 1 answers

1
John Love On

HERE is the THE answer ...

Make touchesBegan an extension of GameScene, not GameViewController with:

extension GameScene {
        
    // "if we set isUserInteractionEnabled = false, the Scene will receive touch events"
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        //  not needed because addCreditsButton() isn't called
        //  unless we're staring at "GameScene"
    //  if let ourScene = GameScene(fileNamed: "GameScene") {
            
            if let touch:UITouch = touches.first {
                let location = touch.location(in: self)
                let ourNode:SKNode = atPoint(location)   // not ourScene.atPoint(location)
                
            //  print("location = \(location)")
                print("ourNode.name = \(ourNode.name!)")
                if (ourNode.name == "creditsInfo") {
                    showCredits()
                }
            }   // if let touch:UITouch
            
    //  }   // if let ourScene
        
    }   // touchesBegan