Bounce Ball off Wall - Change Direction

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

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

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;
}

:slight_smile:
jeremy

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

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.

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

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

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;
}

:slight_smile:
jeremy

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

Tim

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

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…