【问题标题】:moving buttons along with the camera in swift与相机一起快速移动按钮
【发布时间】:2019-04-29 11:25:28
【问题描述】:

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

    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。我还尝试通过将位置转换为相机来更新其位置,但没有奏效。 如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

标签: ios swift button sprite-kit skcameranode


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2017-01-08
    • 2022-07-26
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多