为什么我添加点击框后该功能就停止工作?

时间:2019-02-14 16:38:32

标签: swift skspritenode cgfloat

我正在尝试创建一个汽车游戏,在该游戏中,您可以通过触摸屏幕从左向右移动汽车,但是一旦我设置了“物理定义->身体类型”,汽车就到达了最左或最右在屏幕上,此移动功能停止工作。 我正在使用

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let touchLocation = touch.location(in: self)
        if touchLocation.x > centrePoint {
            if playerCar.position.x == playerCarAtMaxLeft {  
                playerCar.position.x = playerCarAtLeft

                playerCarMoveRight = true
                playerCarMoveLeft = false

            } else if playerCar.position.x == playerCarAtLeft {  
                playerCar.position.x = playerCarAtRight

                playerCarMoveRight = true
                playerCarMoveLeft = false

            } else if playerCar.position.x == playerCarAtRight {  
                playerCar.position.x = playerCarAtMaxRight

                playerCarMoveRight = true
                playerCarMoveLeft = false

            } else {
                playerCarMoveRight = false
                playerCarMoveLeft = true

            }
        } else {                                        
            if playerCar.position.x == playerCarAtMaxRight {     
                playerCar.position.x = playerCarAtRight

                playerCarMoveRight = false
                playerCarMoveLeft = true

            } else if playerCar.position.x == playerCarAtRight { 
                playerCar.position.x = playerCarAtLeft

                playerCarMoveRight = false
                playerCarMoveLeft = true

            } else if playerCar.position.x == playerCarAtLeft {   
                playerCar.position.x = playerCarAtMaxLeft

                playerCarMoveRight = false
                playerCarMoveLeft = true

            } else{
                playerCarMoveRight = true
                playerCarMoveLeft = false

            }
        }

        canMove = true

    }
}

playerCar是一个SKSpriteNode playerCarAt ...是CGFloat playerCarMove ...是布尔值

1 个答案:

答案 0 :(得分:0)

我建议您只是让汽车在x轴上滑动尽可能多的距离,然后使用update方法来跟踪汽车的位置。

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}

这样,您可以设置let maxXPosition,并且当汽车节点到达该点时,您就停止了运动。

一种方法是使汽车节点运动成为SKAction.moveTo

一个简单的版本是:

class GameScene: SKScene {

var car : SKSpriteNode! // Your car sprite
var maxXPosition : CGFloat! // The max point on the x-axis the car is allowed to move to

override func didMove(to view: SKView) {
    // Initiate car
    car = self.childNode(withName: "//car") as? SKSpriteNode

    // Setup the max x position
    maxXPosition = 200
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        moveCar(toPosition: location)
    }
}

override func update(_ currentTime: TimeInterval) {
    // Check the cars position
    if car.position.x >= 200 {
        // Stop the car is the point has been reached
        car.removeAction(forKey: "carDrive")
    }

}

func moveCar(toPosition position: CGPoint) {
    let moveCarToXPoint = SKAction.moveTo(x: position.x, duration: 1)
    car.run(moveCarToXPoint, withKey: "carDrive")
}

}