swiftskspritenodecgfloat

Why this function stop working as soon as I add hit boxes?


I'm trying to crate a car game where you move the car from left to right by touching the screen but as soon as I set "physics definition -> body type" and the car reach the far left or the far right of the screen, this movement function stop working. I'm using

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 is a SKSpriteNode playerCarAt... are CGFloat playerCarMove... are Boolean


Solution

  • I'd suggest you just let the car slide on the x-axis as much as it wants and then use the update method to keep track of the cars position.

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

    In that way you can set up let maxXPosition and as soon as the car-node hits that point, you stop the movement.

    A way of doing so is to make the car-node movement an SKAction.moveTo.

    A simple version would be:

    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")
    }
    

    }