ios - 快速移动按钮和相机

标签 ios swift button sprite-kit skcameranode

我正在开发一款游戏,按下按钮时玩家会移动,因此我将按钮设为相机的子项。然而,当我的相机移动时,按钮出现在应有的位置,但当我再次按下时什么也没有发生。 这是一些关于相机和按钮的代码。

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

最佳答案

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

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

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

关于ios - 快速移动按钮和相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53504455/

相关文章:

iphone - 添加按钮部分标题?

ios - 如何按列值对 CoreData 中的记录进行分组?

ios - 游戏中的SpriteKit Shop场景

Android onClick 只能工作一次

ios - 无法使用点符号快速访问函数成员

ios - NSURLProtocol 和 post 上传进度

swift - ARKit 条形码跟踪和视觉框架

ios - 如何从另一个 ViewController 中的按钮更改单独 ViewController 中的 textView?

android - 2个带声音的按钮

iphone - 有没有办法知道 iOS 设备何时锁定/解锁?