Search code examples
iosswiftbuttonsprite-kitskcameranode

moving buttons along with the camera in swift


I am working on a game where my player moves when buttons are pressed, so I made the buttons children of the camera. However when my camera moves, the buttons appear where they should, but when I press again nothing happens. Here is some code about the camera and buttons.

    override func didMove(to view: SKView) {

    physicsWorld.contactDelegate = self

    enumerateChildNodes(withName: "//*", using: { node, _ in
        if let eventListenerNode = node as? EventListenerNode {
            eventListenerNode.didMoveToScene()
        }
    })


    world = childNode(withName: "World")!
    player = (world.childNode(withName: "player") as! Player)


    spikes = world.childNode(withName: "spikes") as! SKSpriteNode
    spikes.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: spikes.size.width/2, height: spikes.size.height/2))
    spikes.physicsBody?.categoryBitMask = PhysicsCategory.Spikes
    spikes.physicsBody?.isDynamic = false

    self.addChild(cameraNode)
    camera = cameraNode
    cameraNode.position = CGPoint(x: 0 , y: 0)
    cameraToMove = cameraNode.position.y - player.position.y
    setupCamera()




    left = SKSpriteNode(imageNamed: "leftButton")
    right = SKSpriteNode(imageNamed: "rightButton")
    jump = SKSpriteNode(imageNamed: "upButton")


    jump.size.width /= 2
    jump.size.height /= 2
    left.position = CGPoint(x: -self.size.width/2 + left.size.width, y: -self.size.height/2 + left.size.height)
    right.position = CGPoint(x: left.position.x + 2 * right.size.width, y:(camera?.position.y)! - self.size.height/2 + left.size.height)
    jump.position = CGPoint(x: self.frame.width/2 - jump.size.width, y:(camera?.position.y)! - self.size.height/2 + left.size.height)

    cameraNode.addChild(left)
    cameraNode.addChild(right)
    cameraNode.addChild(jump)


}

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let location:CGPoint = (touch?.location(in: self))!
    if left.contains(location){
        move(forTheKey: "left")
        print("left")
    }
    else if right.contains(location){
        move(forTheKey: "right")
        print("right")
    }
    else if jump.contains(location){
        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 240))

    }else{
        print(location)
    }


}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    let location:CGPoint = (touch?.location(in: self))!
    if left.contains(location){
        player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        print("left removed")
    }
    else if right.contains(location){
        player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
        print("right removed")
    }
    else if jump.contains(location){
        print("jump removed")
    }else{
        print("else")
        print(location)
        print(left.position)
        print(convert(left.position, from: cameraNode))
        print(convert(left.position, to: cameraNode))
        print(camera?.position.y)

        player.physicsBody?.velocity.dx = 0
    }
}

As you can see I have a lot of prints to check the left button position. In the beginning the y position is at -577. after the camera moves, the button appears where it should at -450. but cant press it because its position is still at -577. I also tried to update its position by converting the position to the camera, but didn't work. I would really appreciate if someone can help.


Solution

  • You want to use the touch as it is relative to your camera, not relative to the scene, since your buttons are in the coordinate system of the camera.

    Change let location:CGPoint = (touch?.location(in: self))! to let location:CGPoint = (touch?.location(in: self.camera))!

    then your buttons should function properly.