迅速移动按钮和相机

时间:2018-11-27 16:51:23

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

1 个答案:

答案 0 :(得分:0)

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

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

然后您的按钮应该可以正常工作。