# Bounce Ball off Wall - Change Direction

**URL:** https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355
**Category:** flash
**Created:** [April 14, 2002, 6:35pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355 "2002-04-14T18:35:19Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 6:35pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/1 "2002-04-14T18:35:19Z")

</div>

Hi,  
This will be an easy one for you experts out there. I just want to have a ball randomly float through the movie & realize when it hits the wall of the movie & change directions. The moving part is no problem. I just need the logic for when the ball encounters the wall. Any help would be greatly appreciated,

Tim

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 7:52pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/2 "2002-04-14T19:52:38Z")

</div>

when it hits the wall, all you want to do is reverse its x and y direction…

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 7:53pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/3 "2002-04-14T19:53:16Z")

</div>

let’s say your movie is 400x400 and the ball is 20x20…  
onClipEvent(load){  
nXmov=(Math.random()_9)-4;  
nYmov=(Math.random()9)-4;  
}  
onClipEvent(enterFrame){  
if(\_x\<10){  
\_x=10;  
nXmov=-1;  
} else if (\_x\>390){  
\_x=390;  
nXmov_=-1;  
}  
if(\_y\<10){  
\_y=10;  
nYmov\*=-1;  
} else if (\_y\>390){  
\_y=390;  
nYmov\*=-1;  
}  
\_x+=nXmov;  
\_y+=nYmov;  
}

🙂  
jeremy

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 8:36pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/4 "2002-04-14T20:36:41Z")

</div>

I just hate your nXmov and sValStringStart variables :lol:  
pom 0]

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 8:41pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/5 "2002-04-14T20:41:01Z")

</div>

i love using variables like that. So much cleaner and easier to understand, but they can seem confusing if you don’t know what to look for.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 14, 2002, 10:02pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/6 "2002-04-14T22:02:36Z")

</div>

You guys are the best.  
Thank you so much.  
Going to try it now . . .  
Thanks again,  
Tim

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 15, 2002, 7:04pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/7 "2002-04-15T19:04:41Z")

</div>

Ok, the code below works great & I think I understand it. I’m new to the game so bear with me. Anyone want to take a crack @ commenting this for me. Please don’t be afraid to over explain. Thanks again. It’s much appreciated,  
Tim

let’s say your movie is 400x400 and the ball is 20x20…  
onClipEvent(load){  
nXmov=(Math.random()_9)-4;  
nYmov=(Math.random()9)-4;  
}  
onClipEvent(enterFrame){  
if(\_x\<10){  
\_x=10;  
nXmov=-1;  
} else if (\_x\>390){  
\_x=390;  
nXmov_=-1;  
}  
if(\_y\<10){  
\_y=10;  
nYmov\*=-1;  
} else if (\_y\>390){  
\_y=390;  
nYmov\*=-1;  
}  
\_x+=nXmov;  
\_y+=nYmov;  
}

jeremy

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 15, 2002, 7:36pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/8 "2002-04-15T19:36:30Z")

</div>

onClipEvent(load){  
// set the speed for this movie clip.  
nXmov=(Math.random()_9)-4;  
nYmov=(Math.random()9)-4;  
}  
onClipEvent(enterFrame){  
if(\_x\<10){  
// if this clip is ‘out of bounds’ on the \_x axis place it ‘in bounds’ and reverse it’s direction on the \_x axis.  
\_x=10;  
nXmov=-1;  
} else if (\_x\>390){  
\_x=390;  
nXmov_=-1;  
}  
if(\_y\<10){  
// if this clip is ‘out of bounds’ on the \_y axis place it ‘in bounds’ and reverse it’s direction on the \_y axis.  
\_y=10;  
nYmov\*=-1;  
} else if (\_y\>390){  
\_y=390;  
nYmov\*=-1;  
}  
// move this clip.  
\_x+=nXmov;  
\_y+=nYmov;  
}

🙂  
jeremy

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 17, 2002, 3:01pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/9 "2002-04-17T15:01:28Z")

</div>

Jeremy,  
Wanted to thank you for the comments.  
Much appreciated. Sorry for being such a dummy.  
New to the game.

Tim

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 17, 2002, 4:12pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/10 "2002-04-17T16:12:47Z")

</div>

not a problem, we’ve all been there before!  
🙂  
jeremy

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [April 17, 2002, 4:40pm UTC](https://forum.kirupa.com/t/bounce-ball-off-wall-change-direction/2355/11 "2002-04-17T16:40:53Z")

</div>

ha! I don’t think you ever really started out slow with Flash. I think you were born (or produced rather) with the Macromedia symbol printed across your forehead. j/k…true…everyone has to start somewhere…
