Changing balls direction in Pong
- by hustlerinc
I'm making a Pong game to get started with game-developement but
I've run into a problem that i can't figure out.
When trying to change the balls direction it doesn't change. This is the relevant code:
function moveBall(){
this.speed = 2.5;
this.direction = 2;
if(this.direction == 1){
ball.X +=this.speed;
}
else if(this.direction == 2){
ball.X -=this.speed;
}
}
function collision(){
if(ball.X == 500){
moveBall.direction = 2;
}
if(ball.X == 300){
moveBall.direction = 1;
}
}
Why doesn't it work? I've tried many different ways, and none of them seem to work.
The moveBall.direction changes though, since it alerts the new direction once it reaches the defined ball.X position.
If someone could help me I would deeply appreciate it.
I've included a JSFiddle link.
http://jsfiddle.net/hustlerinc/y4wp3/