# Bounce of wall with direction changes

**URL:** https://forum.kirupa.com/t/bounce-of-wall-with-direction-changes/214049
**Category:** flash
**Created:** [January 25, 2007, 4:04am UTC](https://forum.kirupa.com/t/bounce-of-wall-with-direction-changes/214049 "2007-01-25T04:04:05Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![chup](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/chup/32/925_2.png) [@chup](https://forum.kirupa.com/u/chup)
#### Post date: [January 25, 2007, 4:04am UTC](https://forum.kirupa.com/t/bounce-of-wall-with-direction-changes/214049/1 "2007-01-25T04:04:05Z")

</div>

Hi Guys,

I have this following situation.

Basically, I want my balls to move around a border and bounce off the walls when it hit the walls.

I have this done up and working.  
Here an example of what I had achieve  
[http://64.13.207.72/download/arrow.swf](http://64.13.207.72/download/arrow.swf)

But upon hitting the wall, I want the balls to rotated to a reflect the correct direction also, this part I’m stuck =(

Please click on this link to have an idea of what I mean  
[http://64.13.207.72/download/example.jpg](http://64.13.207.72/download/example.jpg)

The script is use is as follow:

```auto

onClipEvent (load) {
	fanX = this._x;
	fanY = this._y;
	leftWall = 0;
	rightWall = 600;
	topWall = 150;
	bottomWall = 400;
	fanRadius = this._width/2;
	distX = (Math.random()*3)-1.5;
	distY = (Math.random()*3)-1.5;
}
//
//
//
onClipEvent (enterFrame) {
	//
	fanX = fanX+distX;
	fanY = fanY+distY;
	this._x = fanX;
	this._y = fanY;
	// check right wall
	if (fanX+fanRadius>rightWall) {
		overshoot = (fanX+fanRadius)-rightWall;
		fanX -= overshoot*2;
		distX *= -1;
	}
	// check left wall
	if (fanX-fanRadius<leftWall) {
		overshoot = leftWall-(fanX-fanRadius);
		fanX += overshoot*2;
		distX *= -1;
	}
	// check bottom wall
	if (fanY+fanRadius>bottomWall) {
		overshoot = (fanY+fanRadius)-bottomWall;
		fanY -= overshoot*2;
		distY *= -1;
	}
	// check top wall
	if (fanY-fanRadius<topWall) {
		overshoot = topWall-(fanY-fanRadius);
		fanY += overshoot*2;
		distY *= -1;
	}
}

```

The file can be download via this link  
[http://64.13.207.72/download/arrow.zip](http://64.13.207.72/download/arrow.zip)

Any input? Thanks =)  
🐇
