## AS: Boucing ball

• 2,353 Views
• 9 Replies
m4x0
m4x0
• Member since: Nov. 15, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
AS: Boucing ball 2007-02-07 15:36:50

AS MAIN
AS: Bouncing ball
Example

This tutorial shows you how to make a cool bouncy ball you control with the arrow keys, and will teach you how the AS works, alternatively, you can just c/p the code into flash, but you won't learn much by doing that, will you?

Okay, so firstly, create a movieClip, in the movie clip draw a circle. Make sure the little + is in the middle of the ball. Then, go back onto the main timeline and insert this AS into the ball:

grav = 1;
speed = 0;
_root.score = 0;
/*This sets the variables grav, speed and score. You should understand variables to use this code, if you don't, go find a tutorial on them*/
}
onClipEvent(enterFrame){
if(this._rotation > 0){
this._rotation-=4;
}if(this._rotation < 0){
this._rotation+=4;
}
}
/* This code means that if the ball has a rotation of lower than 0, the ball will rotatation will increase by 4 points every frame, while if the rotation is higher than zero, it will decrease 4 points every frame*/
onClipEvent(enterFrame){
if(speed > 0){
speed--;
}
if(speed < 0){
speed++;
}
if(grav < 0){
grav++;
}
}
/*This does the same as the previous block of code, except it changes the speed and grav variables instead of the rotation*/
onClipEvent(enterFrame){
grav++;
this._y+=grav;
this._x+=speed;
this._rotation+=rotate;
rotate = (speed);
}
/* This code makes it so every frame, the variable grav goes up by one, it also makes it so that the speed the ball moves left of right is determined by the variable speed, and the speed the ball falls is determined by the variable grav */
onClipEvent(enterFrame){
if(grav > 30){
grav =30;
}
if(grav < -30){
grav = -30;
}
if(speed > 30){
speed = 30;
}
if(speed < -30){
speed = -30;
}
/*This code makes it so if the variable grav is above 30, it goes back down to 30, and if it is below -30, it will go back up to -30, it does it with the variable speed too, this is so the ball dosen't move too fast.*/
if(Key.isDown(Key.UP)){
grav-=3;
}
if(Key.isDown(Key.LEFT)){
speed-=2;
}
if(Key.isDown(Key.RIGHT)){
speed+=2;

}
}
/*This code says that if the UP key is down, the variable grav goes down by 3 every frame, if you remember the grav variable determines how fast the ball falls, if its in the minus numbers to ball will go up, it also makes it so the left and right keys make the speed variable go up and down, as the speed variable determines how fast the ball goes left/right*/
onClipEvent(enterFrame){
if(this._x < 0){
this._x = 0;
speed = speed - (speed *2);
}
if(this._x > 550){
this._x = 550;
speed = speed - (speed * 2);
}
if(this._y < 0){
this._y = 0;
grav = grav - (grav * 2);
}
if(this._y > 400){
this._y = 396;
grav = grav - (grav * 2.2);
}
}
/* This final bit of code basically means that when a ball hits the side, it will bounce off it, this code assumes the flash document is 550x400. I will explain a bit more, basically, the first bit says that if the balls _x position of 0, the speed variable = speed - (speed times 2), so if speed was 2, and the balls x position was 0, speed would then be 2 - (2 times 2) which is 2 minus 4, which is -2, so the ball would bounce back off the wall*/

You may be able to tell that this is my first tutorial I am writing, but I hope it helps someone :)

m4x0
m4x0
• Member since: Nov. 15, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Boucing ball 2007-02-07 15:39:22

Oh, and I would like to add that it looks better if the ball has a smiley face or some sort of pattern on it, you can see the rotation working better.

ssjskipp
ssjskipp
• Member since: Oct. 16, 2003
• Offline.
Forum Stats
Member
Level 15
Programmer
Response to AS: Boucing ball 2007-02-07 15:54:43

...any reason his vertical bounce is much higher than his horizontal bounce? I know you added the whole slow-down-to-0 with _x velocity, but you may want to say "Remove that part to get a real ball"

"Give a man a match, and he'll be warm for a minute, but set him on fire, and he'll be warm for the rest of his life."

Kart-Man
Kart-Man
• Member since: Jan. 7, 2007
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: Boucing ball 2007-02-07 18:52:13

At 2/7/07 03:36 PM, VeinDigger wrote: AS: Bouncing ball

Great tutorial, being it your first one. It's a lot better than some "tutorials" around here. May I suggest getting rid of the three enterFrames in there? It's not necessary, and makes the code messier. But, you've explained the code well, and that's always good.

BTW, what would _root.score do? I assume it's for scoring, but can't people just add that in themselves if they wanted to? ;)

postcount +=1;
Newgrounds Photoshop Headquarters || Don't use MS Paint! Use Aviary!
SING US A SING YOU'RE THE PIANO MAN

m4x0
m4x0
• Member since: Nov. 15, 2005
• Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to AS: Boucing ball 2007-02-08 12:30:30

At 2/7/07 06:52 PM, Kart-Man wrote: BTW, what would _root.score do? I assume it's for scoring, but can't people just add that in themselves if they wanted to? ;)

Oh yeah, I forgot to remove that variable. It's because I copied the code straight from a game I made and I needed a score variable in there.

ivanmalo
ivanmalo
• Member since: Mar. 21, 2007
• Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: Boucing ball 2007-04-10 17:23:09

will this add mm sort of physics to the game, i dont mean gravity, but i want to bounca a ball of a diagonal wall and goes straight up like if it was a horizontal wall...
take a look....
My game....
so here you clikc on the bar and you rotate it.... but it alwasy bounces as if it was a horizontal pad....

thequackattack
thequackattack
• Member since: Nov. 6, 2006
• Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Boucing ball 2007-04-10 17:24:19

that's pretty cool

Flashweaver
Flashweaver
• Member since: Apr. 22, 2006
• Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS: Boucing ball 2008-10-11 18:28:53

I'm sorry for bumping this or whatever, but my ball has a huge problem!! Whenever it hits the left wall it goes haywire!!! I dont know whats wrong because the code is almost identical to when it hits the right wall!

Here take a look:

if(this._x < 0) {
this._x = 200;
speed += (speed*2);
}
if(this._x > 550) {
this._x = 550;
speed -= (speed*2);
}
if(this._y > 400) {
this._y = 390;
grav -= (grav*2);
}
if(this._y < 0) {
this._y = 10
grav -= (grav*2);
}

Flashweaver
Flashweaver
• Member since: Apr. 22, 2006
• Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS: Boucing ball 2008-10-11 18:34:14

Sorry nevermind....spent ages searching and came across one little flaw. its workin now. Sorry for bumping. Thanks for the tut anyway.

Flashweaver
Flashweaver
• Member since: Apr. 22, 2006
• Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to AS: Boucing ball 2008-10-11 18:40:08

Sorry nevermind....spent ages searching and came across one little flaw. its workin now. Sorry for bumping. Thanks for the tut anyway.