## Bounce Physics Tutorial

Score:
rated 3.64 / 5 stars
Views:
6,569 Views
Genre:
Tutorial
Tags:
tutorial
bounce
physics

## Credits & Info

Mar 8, 2009 | 1:44 PM EDT

#### If you liked this, check these out!

The movie you're about to see is the finished product. You can create this by making a Movie Clip ball and separate Movie Clips for walls and ground (with respective instance names). Put this script on the ball, and read the // for explanations.

onClipEvent(load){//Apply the following script once (hence 'load')
_root.grav=0;//Recognize the instance 'grav' as a rational number
_root.xmove=0;//Recognize the instance 'xmove' as a rational number
}//Terminate the handler
onClipEvent(enterFrame){/
/Apply this script constantly
this._y+=_root.grav;//Mak e the ball fall at 'grav'
_root.grav+=1;//Make 'grav' increment by 1; you can change the number
this._x+=_root.xmove;//Ma ke the ball move forward at 'xmove'
if(_root.ground.hitTest(_
x,_y,true)){//If this hits the ground do the following
_root.grav*=-1;//Multiply 'grav' by -1, which will make the ball bounce
}//Terminate the if statement
//OPTIONAL SCRIPT!! The following script is optional. Add a Movie Clip with the instance name 'restart' if you want it to do something.
if(_root.restart.hitTest(
_x,_y,true)){//If this hits the restart barrier
this._x=265;//Set the ball's X to 265
this._y=190;//Set the ball's Y to 190
_root.grav=0;//Sets 'grav' to 0
_root.xmove=0;//Sets 'xmove' to 0
}//Terminate the if statement
//WARNING! The optional script stops here! Everything below is essential!
if(_root.walls.hitTest(_x ,_y,true)){//If the ball hits the walls
_root.xmove*=-1;//Multipl y 'xmove' by -1 which will make the ball bounce sideways
}//Terminate the if statement
if(Key.isDown(Key.LEFT)){
//If the left key is pressed
_root.xmove-=1;//Decrease 'xmove' by 1, which will eventually result in 'negative-positive' movement, making the ball move left
}//Terminate the if statement
if(Key.isDown(Key.RIGHT))
{//If the right key is pressed
_root.xmove+=1;//Incremen t 'xmove' by 1, which will result in an eventual rightwards movement
}//Terminate the if statement
if(!Key.isDown(Key.RIGHT)
&&!Key.isDown(Key.LEFT)&&
_root.xmove>0){//If right nor left are pressed and 'xmove' is a positive number do the following
_root.xmove-=1;//Decrease xmove by 1, which will bring the ball to a horizontal standstill
}//Terminate the if statement
if(!Key.isDown(Key.RIGHT)
&&!Key.isDown(Key.LEFT)&&
_root.xmove<0){//If right nor left are pressed and 'xmove' is negative
_root.xmove+=1;//Increase xmove by 1, which will bring the ball to a horizontal standstill
}//Terminate the if statement
if(_root.xmove>15){//If 'xmove' is greater than 15
_root.xmove=15;//Set the speed back to 15, which caps the speed
}//Terminate the if statement
if(_root.xmove<-15){//If 'xmove' is less than -15
_root.xmove=-15;//Set the speed up to -15, which caps the speed
}//Terminate the if statement
}//Terminate the handler

## Reviews

#### Lifes-a-Bitch

##### Rated 1.5 / 5 stars2009-10-19 14:02:09

Incredibly flawed

For something as simple as this, your script is far too long and unneccessary (sp?).
This could easily be written 1/2 or 1/4 of what you have there. Also, the script could easily be cleaned up by having alot of the script on the walls and using tellTarget(_root.ball){ to change the variables. also, you could of threw in some nice easy trig in there to make the angles more fluent. Also, why is it that with the Gravity modifier at 1 the ball continues to rise? You need to re-read your script and fix it... And possibly slice and dice it.

People find this review helpful!
Kwing responds:

The script is not only short, but pretty concise.

It's also kind of old (and I'm still inexperienced), so sorry about the noobiness. At least I can make a lot with my limited knowledge.

#### Illogical-Ironical

##### Rated 4.5 / 5 stars2009-09-09 01:05:30

It looks pretty nice.

I was wondering, if you could make a rolling physics tutorial (like a ball rollwing down a slope) if you did so, i would be so thankful :)

Kwing responds:

Hmm, I can do sliding stops. Check out Platform Tutorial AS2 and edit the sliding variable so it's really low.

#### swarm420

##### Rated 5 / 5 stars2009-03-08 15:05:15

Good tutorial

Good tutorial, now is the code you posted pure code or is it with some info on what does what?

Also, HOW DID YOU MANAGE TO WRITE ALL THAT CODE?!?!

I fall asleep after ten liens of animation code... So, just wondering... >_> <_<

Kwing responds:

The // cancels any following script, so it has a description but also can be copy/pasted. Yeah, I wrote all the code. Only about half of it is original, though, because some of the functions are just left/right duplicates.

#### PlasticFisherMen

##### Rated 5 / 5 stars2009-03-08 14:55:53

Very helpful tutorial , that helped me out a lot , keep up the excellent work!

People find this review helpful!
Kwing responds:

:D Thanks!

#### KaosKilla9231

##### Rated 1.5 / 5 stars2009-03-08 14:04:36

Ummm...

Not Really Too Fun Or Imaginative..Only Cause I've Seen These All Over The Net.

The Ball Ocasionally Dissapers And Sometimes Stops In General...

Kwing responds:

I know there are glitches; it's an engine, nothing more.