mercredi 14 janvier 2015

Creating an if statement where a coordinate must be in between a set of coordinates in Swift

I am currently in the process of writing a PONG game in Swift. I have gotten to the point where I am trying to get the game ball to rebound off the player's paddle. I am having trouble creating an if statement using the coordinates of the player's paddle. I will write in pseudo code what I mean.



If the game ball is in between the right most x coordinate of the player paddle and the right most coordinate of the player paddle and the y value of the game ball is the same as the y value of the player paddle {

Do this code

}


Currently, my code looks like this:



if GameBall.center.x <= PlayerPaddle.center.x + 50 && GameBall.center.x >= PlayerPaddle.center.x - 50 && GameBall.center.y - 5 == PlayerPaddle.center.y + 15 {
if GameBallDirection == "W" {
GameBallDirection = "E"
}


However this does not work. Is there a way to write a condition which requires the Game Ball to be within the left and right most x coordinates of the player paddle? I know this may seem vague but I have tried to explain it as best as possible and any and all help is appreciated.


Aucun commentaire:

Enregistrer un commentaire