00:00
00:00
Newgrounds Background Image Theme

bugblast just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Boucing ball

3,140 Views | 9 Replies
New Topic Respond to this Topic

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:

onClipEvent(load){
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 :)


BBS Signature

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.


BBS Signature

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."

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

BBS Signature

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.


BBS Signature

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....

Response to AS: Boucing ball 2007-04-10 17:24:19


that's pretty cool

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

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.

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.