00:00
00:00

AS: Bouncing Object

10,309 Views | 37 Replies
New Topic Respond to this Topic

AS: Bouncing Object 2005-08-26 19:59:20


AS: Main, better then Google!

Related Topics:
AS: Movement - Basic
AS: Varials
AS:Math
AS: Conditions

Next Step:
AS: Platform game

Ok, in this tutorial I'll teach you how to make basic bouncing physics. It's pretty simple once you understand basic math and conditions in flash. My first real flash game used this in it, and by the end of this tutorial you will be able to make one too.

1.) Make a movieClip, i'd suggest a circle(my movieclip was 50x50). You dont have to name it. Make sure the center is on the bottom of the movieclip.

2.)Set the specifications of your objects "physic's" like how fast it falls, where to bounce, how much the ground will lower the speed. Heres mine:

onClipEvent (load) {
ySpeed = 0;
//starting fall speed of object
gravity =3;
//how fast the ySpeed rises, should be no higher then 3
bounce = .8;
//how much the speed decreases when for each bounce
ground = 350;
//where to bounce back
}

When the rest of the code is finished you should change the numbers around so they fit to your liking.

3.) Set up your main code.

onClipEvent (enterFrame) {
if (_y<ground) {
ySpeed += gravity;
} else if (ySpeed>gravity*4) {
//change 4 to a higher number if it doesnt work
_y = ground;
ySpeed *= -bounce;
} else {
ySpeed = 0;
_y = ground;
}
_y += ySpeed;
}

Basically what this does is check if the object is above the specified "ground", if it is then the speed at which it falls it increased. Otherwise, the speed is multiplied negatively, so it will move in the opposite direction.

So that the ball would'nt bounce as high after each bounce i multiplied it by a decimal less than 1(variable-"bounce").

you may be wondering what "else if (ySpeed>gravity*4)" is for. well it basically stops the balls from moving when it speed is really low. without that, the ball would bounce forever. It may have a little jerk at the end but its better than nothing.

and that is all you need for a bouncing ball!

But George, thats really boring!! that cant possibly be all! and endless flight sucks.

well now that you understand the basics, you can add on to it, and make a cool game with it. for instance add this right after _y+=ySpeed:

if (_x<0 || _x>550) {
xSpeed *= -1;
}
_x += xSpeed;
xSpeed *= .95;

and add this at the bottom:

onClipEvent (mouseDown) {
//when mouse is clicked
if (hitTest(_root._xmouse, _root._ymouse, true)) {
// if touching the mouse
xSpeed = (_x-_root._xmouse)/2;
_y--;
ySpeed = (_y-_root._ymouse-_height)/1.5;
}
}

you now have your very own kick-ups game!

i quit

This blog I made | This game sucks | I, twit

Just trying to get enough Twitter followers to quit Facebook forever

Response to AS: Bouncing Object 2005-08-26 20:00:59


why thankyou


WEBSITE

BLOG ~ Dont fuck around with my dog. All that I can see I steal. ~

NG FFR ~ Automatic for the people.

BBS Signature

Response to AS: Bouncing Object 2005-08-26 20:34:59


yes thank you very much now could this be a proloader game?

If yes how will I go about doing this.

Response to AS: Bouncing Object 2005-10-22 05:30:24


It really looks similiar to AS: Pong game physics except it's not specific, it's more basic, and it's well explained :D
Nice work.


BBS Signature

Response to AS: Bouncing Object 2005-10-22 05:50:20


reflection math: can be used in bouncing too for arbitary ground (ie not just flat and horizontal)

if V is the vector describing the motion of the object before hitting the ground
if N is the normal to the ground (unit vector)

the Vr (reflected vector) is V + (2N * - N . V);
-> (N . V is the dot product of N and V)


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to AS: Bouncing Object 2005-10-22 09:43:22


At 10/22/05 05:50 AM, dELta_Luca wrote: reflection math: can be used in bouncing too for arbitary ground (ie not just flat and horizontal)

if V is the vector describing the motion of the object before hitting the ground
if N is the normal to the ground (unit vector)

the Vr (reflected vector) is V + (2N * - N . V);
-> (N . V is the dot product of N and V)

english please

Response to AS: Bouncing Object 2005-10-22 09:59:07


At 10/22/05 09:43 AM, Glaiel_Gamer wrote: english please

its just a little formula to find a reflection vector
http://img478.images..lectionvector3uv.jpg


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to AS: Bouncing Object 2005-10-22 10:02:33


At 10/22/05 05:50 AM, dELta_Luca wrote: reflection math: can be used in bouncing too for arbitary ground (ie not just flat and horizontal)
the Vr (reflected vector) is V + (2N * - N . V);
-> (N . V is the dot product of N and V)

What's a dot product?


- - Flash - Music - Images - -

BBS Signature

Response to AS: Bouncing Object 2005-10-22 10:03:55


At 10/22/05 09:59 AM, dELta_Luca wrote:
At 10/22/05 09:43 AM, Glaiel_Gamer wrote: english please
its just a little formula to find a reflection vector
http://img478.images..lectionvector3uv.jpg

christ someones gunna A* there Maths GCSE's...


WEBSITE

BLOG ~ Dont fuck around with my dog. All that I can see I steal. ~

NG FFR ~ Automatic for the people.

BBS Signature

Response to AS: Bouncing Object 2005-10-22 10:17:59


At 10/22/05 10:02 AM, Denvish wrote:
At 10/22/05 05:50 AM, dELta_Luca wrote: reflection math: can be used in bouncing too for arbitary ground (ie not just flat and horizontal)
the Vr (reflected vector) is V + (2N * - N . V);
-> (N . V is the dot product of N and V)
What's a dot product?

dot product also called scalar product

a . b = ax*bx+ay*by+az*bz = cosine of the angle betweent the two vectors


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to AS: Bouncing Object 2005-11-23 18:52:17


so what could i use for AS for a game like kitty cannon or penguin baseball would this help casue i really dont know!?

Response to AS: Bouncing Object 2006-01-21 22:30:02


is there something wrong with me if i didn't get the action script............ cuz i'm trying my first game mabye i'm just a noob


i play guitar...... what else do you want from me

BBS Signature

Response to AS: Bouncing Object 2006-01-21 23:36:49


At 1/21/06 10:30 PM, ZIM106 wrote: is there something wrong with me if i didn't get the action script............ cuz i'm trying my first game mabye i'm just a noob

no, im accually just a pretty bad teacher. but basically, the variable gravity is the vertical acceloration of the object, ySpeed is the vertical velocity, and _y is the vertical postion on the stage.

just like in real life, gravity is (virtually) the same. so it remains constant. velocity is changed by acceloration (ySpeed-= gravity), and position is changed by velocity (_y+=ySpeed)

and in case your wandering
a+=b
is like adding "b" to the variable "a"

when you jump, at first you have a positive velocity. the velocity decreases gradually until its zero then, eventually, negitive. but your speed decreases at a constant rate. the difference in velocity is you acceloration(or gravity)


This blog I made | This game sucks | I, twit

Just trying to get enough Twitter followers to quit Facebook forever

Response to AS: Bouncing Object 2006-01-22 08:52:27


alright i kind of get it


i play guitar...... what else do you want from me

BBS Signature

Response to AS: Bouncing Object 2006-01-22 09:10:19


Whoever would like a rotating ball, put this script inside your enterFrame event handler.

this._rotation = this._x;
//assign the rotation property to the _x position property

As you can guess from my little comment there, this script adjusts the rotation of the ball depending on the current horizontal position. This makes a nice little "speed" effect.

Response to AS: Bouncing Object 2006-07-31 14:26:41


can you give me the code in full please... this is what I have but i always get errors

onClipEvent (load) {
ySpeed = 0;
//starting fall speed of object
gravity =3;
//how fast the ySpeed rises, should be no higher then 3
bounce = .8;
//how much the speed decreases when for each bounce
ground = 350;
//where to bounce back
}
onClipEvent (enterFrame) {
if (_y<ground) {
ySpeed += gravity;
} else if (ySpeed>gravity*4) {
//change 4 to a higher number if it doesnt work
_y = ground;
ySpeed *= -bounce;
} else {
ySpeed = 0;
_y = ground;
}
_y += ySpeed;
}
if (_x<0 || _x>550) {
xSpeed *= -1;
}
_x += xSpeed;
xSpeed *= .95;
onClipEvent (mouseDown) {
//when mouse is clicked
if (hitTest(_root._xmouse, _root._ymouse, true)) {
// if touching the mouse
xSpeed = (_x-_root._xmouse)/2;
_y--;
ySpeed = (_y-_root._ymouse-_height)/1.5;
}
}

Response to AS: Bouncing Object 2006-07-31 15:16:39


You should really try to work out what goes where you know John. I couldn't get it to work at first but i kept trying. Anyway, enter this code in your movieclip; this should work.

onClipEvent (load) {
ySpeed = 0;
//starting fall speed of object
gravity =3;
//how fast the ySpeed rises, should be no higher then 3
bounce = .8;
//how much the speed decreases when for each bounce
ground = 350;
//where to bounce back
}
onClipEvent (enterFrame) {
if (_y<ground) {
ySpeed += gravity;
} else if (ySpeed>gravity*7) {
//change 4 to a higher number if it doesnt work
_y = ground;
ySpeed *= -bounce;
} else {
ySpeed = 0;
_y = ground;
}
_y += ySpeed;if (_x<0 || _x>550) {
xSpeed *= -1;
}
_x += xSpeed;
xSpeed *= .95;
}onClipEvent (mouseDown) {
//when mouse is clicked
if (hitTest(_root._xmouse, _root._ymouse, true)) {
// if touching the mouse
xSpeed = (_x-_root._xmouse)/2;
_y--;
ySpeed = (_y-_root._ymouse-_height)/1.5;
}
}

Response to AS: Bouncing Object 2006-08-09 02:21:52


kk ty

Response to AS: Bouncing Object 2006-08-18 21:27:29


Wow , That was awesome I made a little gravity thing out of the knowledge I gained from that
<Gravity>

Response to AS: Bouncing Object 2006-10-10 13:31:59


404 error.

Response to AS: Bouncing Object 2007-04-06 10:20:27


can you just post the action script cos im sick of having to spend hours tryihng to find out wherewhatever goes, and if this bit of script is part of it, and then it never works, just post the whole action script, (the game part too)


Click This Link Courtesy Of Me.

Response to AS: Bouncing Object 2007-04-06 11:01:15


At 4/6/07 10:20 AM, Devils-Dog wrote: can you just post the action script cos im sick of having to spend hours tryihng to find out wherewhatever goes, and if this bit of script is part of it, and then it never works, just post the whole action script, (the game part too)

hahaha,lmao
tutorials are suppose to teach you
not let you cheat


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Response to AS: Bouncing Object 2007-04-06 17:24:10


At 4/6/07 11:01 AM, sk8more wrote:
At 4/6/07 10:20 AM, Devils-Dog wrote: can you just post the action script cos im sick of having to spend hours tryihng to find out wherewhatever goes, and if this bit of script is part of it, and then it never works, just post the whole action script, (the game part too)
hahaha,lmao
tutorials are suppose to teach you
not let you cheat

thats what i use tutorials for, i have a crappy memory for codes, so i have just copy the codes


Click This Link Courtesy Of Me.

Response to AS: Bouncing Object 2007-04-06 17:26:10


lol,you dont memorize codes either,or you will never become a true scripter
if you just memorize codes you cant write engines,games,or anything
you can only use scripts from tutorials, so you cant call it your own


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Response to AS: Bouncing Object 2007-04-07 09:55:54


...so... i dont give a damn


Click This Link Courtesy Of Me.

Response to AS: Bouncing Object 2007-04-07 10:00:14


Then gtfo.


BBS Signature

Response to AS: Bouncing Object 2007-04-07 10:09:08


At 4/7/07 10:00 AM, GustTheASGuy wrote: Then gtfo.

my exact thoughts >:(


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.

Response to AS: Bouncing Object 2007-04-07 11:36:13


Thank you very much!

Response to AS: Bouncing Object 2007-04-07 13:00:39


kids these days.


This blog I made | This game sucks | I, twit

Just trying to get enough Twitter followers to quit Facebook forever

Response to AS: Bouncing Object 2007-04-07 13:16:35


At 4/7/07 01:00 PM, ImpotentBoy2 wrote: kids these days.

by the waymnice tutorial
bsic,but well explained


Grah i feel so unknown, SK8MORE god damn :/ EvanHayes seems like a much more serious name than sk8more,so i changed it.