简体   繁体   中英

how to define the area of a node to make it a button

AS always, I am not familiar with swift and SpriteKit, so apologies.

I am creating a button that changes scene. I am using a simple code that detects if the touch is in the SKNode area, if yes it changes the scene.

My problem is that the node.position is defined by a CGPoint, not an area, so when you touch the screen you never actually touch the node.

Any suggestion?

I don't know how to solve the problem.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let touchedNode = atPoint(location)
        if touchedNode.name == "B" {
            let menuScene = MenuScene(size: view!.bounds.size)
            view!.presentScene(menuScene)
        }
    }
}

SOLVED! I created the node "B" with text initialiser and not with name initialiser, that is why I could not find it.

let b = SKLabelNode(name: "B")
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let nodeB = self.childNode(withName: "B")
        let frameB = nodeB!.calculateAccumulatedFrame()
        let minX = frameB.minX
        let maxX = frameB.maxX
        let minY = frameB.minY
        let maxY = frameB.maxY
        if location.x > minX && location.x < maxX {
            if location.y > minY && location.y < maxY {
                let gameScene = GameScene(size: view!.bounds.size)
                view!.presentScene(gameScene)
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM