Forum Topic: AS: Bouncing Object

(7,171 views • 37 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 8/26/05 07:59 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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

Some times my "L" key decides not to work.


None

Mogly

Reply To Post Reply & Quote

Posted at: 8/26/05 08:00 PM

Mogly LIGHT LEVEL 25

Sign-Up: 09/05/04

Posts: 10,336

why thankyou

~ MogTom ~ Dont fuck around with my dog. All that I can see I steal. ~
NG FFR ~ Automatic for the people.

BBS Signature

None

PiePie

Reply To Post Reply & Quote

Posted at: 8/26/05 08:34 PM

PiePie NEUTRAL LEVEL 31

Sign-Up: 07/21/04

Posts: 3,092

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

If yes how will I go about doing this.


None

Toast

Reply To Post Reply & Quote

Posted at: 10/22/05 05:30 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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.


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 10/22/05 05:50 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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)

My social worker says im special!

BBS Signature

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 10/22/05 09:43 AM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 10/22/05 09:59 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 10/22/05 10:02 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Mogly

Reply To Post Reply & Quote

Posted at: 10/22/05 10:03 AM

Mogly LIGHT LEVEL 25

Sign-Up: 09/05/04

Posts: 10,336

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

~ MogTom ~ Dont fuck around with my dog. All that I can see I steal. ~
NG FFR ~ Automatic for the people.

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 10/22/05 10:17 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

MetroidX51

Reply To Post Reply & Quote

Posted at: 11/23/05 06:52 PM

MetroidX51 LIGHT LEVEL 14

Sign-Up: 08/29/05

Posts: 445

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


None

ZIM106

Reply To Post Reply & Quote

Posted at: 1/21/06 10:30 PM

ZIM106 EVIL LEVEL 13

Sign-Up: 12/23/04

Posts: 361

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

None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 1/21/06 11:36 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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)

Some times my "L" key decides not to work.


None

ZIM106

Reply To Post Reply & Quote

Posted at: 1/22/06 08:52 AM

ZIM106 EVIL LEVEL 13

Sign-Up: 12/23/04

Posts: 361

alright i kind of get it

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

BBS Signature

None

Siggy0501

Reply To Post Reply & Quote

Posted at: 1/22/06 09:10 AM

Siggy0501 LIGHT LEVEL 23

Sign-Up: 01/08/05

Posts: 1,563

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.


Questioning

johnab333

Reply To Post Reply & Quote

Posted at: 7/31/06 02:26 PM

johnab333 NEUTRAL LEVEL 03

Sign-Up: 06/27/06

Posts: 46

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


None

Andy-Smithy

Reply To Post Reply & Quote

Posted at: 7/31/06 03:16 PM

Andy-Smithy LIGHT LEVEL 09

Sign-Up: 07/27/06

Posts: 659

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


None

johnab333

Reply To Post Reply & Quote

Posted at: 8/9/06 02:21 AM

johnab333 NEUTRAL LEVEL 03

Sign-Up: 06/27/06

Posts: 46

kk ty


None

Rock-Dido-God

Reply To Post Reply & Quote

Posted at: 8/18/06 09:27 PM

Rock-Dido-God NEUTRAL LEVEL 07

Sign-Up: 03/15/06

Posts: 512

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


None

Gylfi

Reply To Post Reply & Quote

Posted at: 10/10/06 01:31 PM

Gylfi EVIL LEVEL 08

Sign-Up: 08/20/05

Posts: 345

404 error.


None

Devils-Dog

Reply To Post Reply & Quote

Posted at: 4/6/07 10:20 AM

Devils-Dog DARK LEVEL 09

Sign-Up: 02/10/07

Posts: 94

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)

If Your Internet Browser Can't Access This Link, Then It Sucks

Click This Link Courtesy Of Me.

BBS Signature

None

EvanHayes

Reply To Post Reply & Quote

Posted at: 4/6/07 11:01 AM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,838

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.


None

Devils-Dog

Reply To Post Reply & Quote

Posted at: 4/6/07 05:24 PM

Devils-Dog DARK LEVEL 09

Sign-Up: 02/10/07

Posts: 94

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

If Your Internet Browser Can't Access This Link, Then It Sucks

Click This Link Courtesy Of Me.

BBS Signature

None

EvanHayes

Reply To Post Reply & Quote

Posted at: 4/6/07 05:26 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,838

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.


None

Devils-Dog

Reply To Post Reply & Quote

Posted at: 4/7/07 09:55 AM

Devils-Dog DARK LEVEL 09

Sign-Up: 02/10/07

Posts: 94

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

If Your Internet Browser Can't Access This Link, Then It Sucks

Click This Link Courtesy Of Me.

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 4/7/07 10:00 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,370

Then gtfo.

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 4/7/07 10:09 AM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,838

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.


None

thenadlernator

Reply To Post Reply & Quote

Posted at: 4/7/07 11:36 AM

thenadlernator LIGHT LEVEL 06

Sign-Up: 05/07/06

Posts: 19

Thank you very much!


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 4/7/07 01:00 PM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

kids these days.

Some times my "L" key decides not to work.


None

EvanHayes

Reply To Post Reply & Quote

Posted at: 4/7/07 01:16 PM

EvanHayes DARK LEVEL 11

Sign-Up: 01/13/07

Posts: 1,838

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:15 AM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!