0

我正在开发一个游戏,当按下按钮时我的玩家会移动,所以我制作了相机的按钮子项。但是,当我的相机移动时,按钮会出现在应有的位置,但是当我再次按下时,没有任何反应。这是一些关于相机和按钮的代码。

    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
    }
}

正如你所看到的,我有很多打印来检查左按钮的位置。开始时 y 位置为 -577。相机移动后,按钮出现在它应该在 -450 的位置。但不能按它,因为它的位置仍然在-577。我还尝试通过将位置转换为相机来更新其位置,但没有奏效。如果有人可以提供帮助,我将不胜感激。

4

1 回答 1

1

您想使用相对于相机而不是相对于场景的触摸,因为您的按钮位于相机的坐标系中。

更改let location:CGPoint = (touch?.location(in: self))!let location:CGPoint = (touch?.location(in: self.camera))!

那么您的按钮应该可以正常工作。

于 2018-11-27T19:28:51.253 回答