Changing balls direction in Pong

Posted by hustlerinc on Game Development See other posts from Game Development or by hustlerinc
Published on 2012-03-16T21:37:00Z Indexed on 2012/03/19 2:17 UTC
Read the original article Hit count: 278

Filed under:

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/

© Game Development or respective owner

Related posts about JavaScript