Be a Supporter!

AS: Bouncing Object

  • 8,702 Views
  • 37 Replies
New Topic Respond to this Topic
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
AS: Bouncing Object Aug. 26th, 2005 @ 07:59 PM Reply

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
Mogly
Mogly
  • Member since: Sep. 5, 2004
  • Offline.
Forum Stats
Member
Level 25
Animator
Response to AS: Bouncing Object Aug. 26th, 2005 @ 08:00 PM Reply

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
PiePie
PiePie
  • Member since: Jul. 21, 2004
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: Bouncing Object Aug. 26th, 2005 @ 08:34 PM Reply

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

If yes how will I go about doing this.

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 05:30 AM Reply

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
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 05:50 AM Reply

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
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 09:43 AM Reply

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

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 09:59 AM Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 10:02 AM Reply

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
Mogly
Mogly
  • Member since: Sep. 5, 2004
  • Offline.
Forum Stats
Member
Level 25
Animator
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 10:03 AM Reply

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
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Bouncing Object Oct. 22nd, 2005 @ 10:17 AM Reply

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
MetroidX51
MetroidX51
  • Member since: Aug. 29, 2005
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to AS: Bouncing Object Nov. 23rd, 2005 @ 06:52 PM Reply

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

ZIM106
ZIM106
  • Member since: Dec. 23, 2004
  • Offline.
Forum Stats
Member
Level 13
Artist
Response to AS: Bouncing Object Jan. 21st, 2006 @ 10:30 PM Reply

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
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS: Bouncing Object Jan. 21st, 2006 @ 11:36 PM Reply

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)

ZIM106
ZIM106
  • Member since: Dec. 23, 2004
  • Offline.
Forum Stats
Member
Level 13
Artist
Response to AS: Bouncing Object Jan. 22nd, 2006 @ 08:52 AM Reply

alright i kind of get it


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

BBS Signature
Siggy0501
Siggy0501
  • Member since: Jan. 8, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: Bouncing Object Jan. 22nd, 2006 @ 09:10 AM Reply

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.

johnab333
johnab333
  • Member since: Jun. 27, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Bouncing Object Jul. 31st, 2006 @ 02:26 PM Reply

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

Andy-Smithy
Andy-Smithy
  • Member since: Jul. 27, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Bouncing Object Jul. 31st, 2006 @ 03:16 PM Reply

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

johnab333
johnab333
  • Member since: Jun. 27, 2006
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS: Bouncing Object Aug. 9th, 2006 @ 02:21 AM Reply

kk ty

Rock-Dido-God
Rock-Dido-God
  • Member since: Mar. 15, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: Bouncing Object Aug. 18th, 2006 @ 09:27 PM Reply

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

Gylfi
Gylfi
  • Member since: Aug. 20, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Bouncing Object Oct. 10th, 2006 @ 01:31 PM Reply

404 error.

Devils-Dog
Devils-Dog
  • Member since: Feb. 10, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Bouncing Object Apr. 6th, 2007 @ 10:20 AM Reply

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.

EvanHayes
EvanHayes
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Bouncing Object Apr. 6th, 2007 @ 11:01 AM Reply

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.

Devils-Dog
Devils-Dog
  • Member since: Feb. 10, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Bouncing Object Apr. 6th, 2007 @ 05:24 PM Reply

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.

EvanHayes
EvanHayes
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Bouncing Object Apr. 6th, 2007 @ 05:26 PM Reply

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.

Devils-Dog
Devils-Dog
  • Member since: Feb. 10, 2007
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Bouncing Object Apr. 7th, 2007 @ 09:55 AM Reply

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


Click This Link Courtesy Of Me.

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Bouncing Object Apr. 7th, 2007 @ 10:00 AM Reply

Then gtfo.


BBS Signature
EvanHayes
EvanHayes
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Bouncing Object Apr. 7th, 2007 @ 10:09 AM Reply

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.

thenadlernator
thenadlernator
  • Member since: May. 7, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: Bouncing Object Apr. 7th, 2007 @ 11:36 AM Reply

Thank you very much!

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS: Bouncing Object Apr. 7th, 2007 @ 01:00 PM Reply

kids these days.

EvanHayes
EvanHayes
  • Member since: Jan. 13, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to AS: Bouncing Object Apr. 7th, 2007 @ 01:16 PM Reply

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.