# Bouncing Ball X and Y \*tears hair out\*

**URL:** https://forum.kirupa.com/t/bouncing-ball-x-and-y-tears-hair-out/258325
**Category:** flash
**Created:** [April 23, 2008, 2:45am UTC](https://forum.kirupa.com/t/bouncing-ball-x-and-y-tears-hair-out/258325 "2008-04-23T02:45:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![The\_Splice](https://avatars.discourse-cdn.com/v4/letter/t/8e7dd6/32.png) [@The\_Splice](https://forum.kirupa.com/u/The_Splice)
#### Post date: [April 23, 2008, 2:45am UTC](https://forum.kirupa.com/t/bouncing-ball-x-and-y-tears-hair-out/258325/1 "2008-04-23T02:45:51Z")

</div>

I got (off a tutorial) this actionscript code which makes a Ball bounce up and down (with gravity in mind) in the Y axis. How could I extend this code so it bounced not only up and down but also in the X direction and off the walls. A ball bouncing around in a box in other words. There will probably have to be 4 if statements for each wall/floor/ceiling and an X values declared I have tried myself but I am not good enough or smart enough to do it. Please please help me!

This is the unaltered code from the tutorial I mentioned:

onClipEvent (load) {

```
gravity = 2 ;

// We get the time when the ball is released for the
// first time. Be careful with the division by 100
time = getTimer () ;
time /= 100 ;

floor = 500 ;

bounce = 0.92 ;

// We set the speed of the ball when it is released.
speedx = 0 ;
speedystart = 0 ;

```

}

onClipEvent (enterFrame) {

```
// We get the current time (still /100)
timenow = getTimer () ;
timenow /= 100 ;
speedy = gravity * (timenow - time) + speedystart ;

//We move the ball
this._x += speedx/5 ;
this._y += speedy/5 ;

if (this._y &gt; floor) {
    this._y = floor ;
    speedy *= -bounce ;
    time = getTimer () ;
    time /= 100 ;
    // reset the timer
    speedystart = speedy ;
    // reset the starting speed
}

```

}

I really need some advice. :te:
