0

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

  • 1
    Not related, but having variables for moving left and right is slightly bad code. I would recommend you create an enum as so: `enum PlayerCarDirection { case left, none, right }` – George Feb 14 '19 at 16:47
  • @George_E thank you for your suggestion! I’ll try that. P.S. I’m just a newbie in coding – cilippofilia Feb 14 '19 at 16:51
  • No problem! You can then declare a variable as so: `var playerCarDirection: PlayerCarDirection = .none` and change that. – George Feb 14 '19 at 16:53
  • Yeah, when you first start coding it's not so much so what you _try_ to make - as most likely your first 10 projects won't be any good or do anything, but **always keep them saved**. It is more about what you learn in that experience, which you can use later on. – George Feb 14 '19 at 16:55

1 Answers1

0

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

}

theloneswiftman
  • 188
  • 1
  • 4
  • 17
  • Thanks for replying! I will try to implement that asap. – cilippofilia Feb 14 '19 at 16:57
  • I've updated my answer. Check that out :-) Please note that only works for the positive x-axis, but you should be able to make it work on the negative x-axis as well. Let me know if you need anything :-) – theloneswiftman Feb 14 '19 at 16:58