为什么这个功能在我添加命中框后立即停止工作?

问题描述 投票:0回答:1

我正在试图通过触摸屏幕从左到右移动汽车的汽车游戏,但是一旦我设置“物理定义 - >体型”并且汽车到达屏幕的最左侧或最右侧,此运动功能停止工作。我正在使用

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 ...是布尔值

swift skspritenode cgfloat
1个回答
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")
}

}

© www.soinside.com 2019 - 2024. All rights reserved.