[Flash] gravity actionscript help

Started by: DarkCloud | Replies: 10 | Views: 592

DarkCloud

Posts: 164
Joined: Dec 2009
Rep: 10

View Profile
Dec 24, 2009 5:38 AM #527925
ok so i'm trying to create gravity for a ball heres the code
onClipEvent(load){
gravity=2;
}
onClipEvent(enterFrame){
vy += gravity;
}
but when I load it the ball just stays in midair can you guys help.:Nooooooo:
CGIllusion
2

Posts: 617
Joined: Aug 2005
Rep: 21

View Profile
Dec 24, 2009 6:54 AM #527974
Aren't you supposed to do something like ball.y += gravity? Whats vy?
DarkCloud

Posts: 164
Joined: Dec 2009
Rep: 10

View Profile
Dec 24, 2009 7:21 AM #527993
V is for velocity :heh: anyways anyone else help!
CGIllusion
2

Posts: 617
Joined: Aug 2005
Rep: 21

View Profile
Dec 24, 2009 11:37 AM #528033
So you're just going to disregard the first part of my post. Incrementing some arbitrary variable in your frame event obviously accomplishes nothing, you have to modify the objects y axis (this._y += gravity) is the proper syntax I believe.
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Dec 24, 2009 1:21 PM #528041
I scanned through the tutorial he used, vy is the variable for the change in the y axis, like you said he just hasn't applied it to anything.

Try something along these lines, however, you'll have to fill in the blanks/replace the italic bold text:

onClipEvent(load){
gravity = 2;
vy = 0
}
onClipEvent(enterFrame){
if (vy < max gravity strength) {
vy += gravity;
}
_root.instance name of ball.y -= vy
}


My AS2 might be a bit rusty since I've been learning AS3 and might have used some of that in place of AS2 but you should be able to clean it out by googling it. This includes a way to give the dropping speed of the ball a maximum, eg, 10.

_root.instance name of ball.y could be replaced with this.y but that will be a bitch if you every advance to AS3 as it was removed, kind of anyway.
Aycaramba
2

Posts: 766
Joined: Apr 2007
Rep: 10

View Profile
Dec 24, 2009 2:59 PM #528064
It's ._y in AS2.
DarkCloud, try learning more basic stuff, and don't move on to more advanced stuff until you understand the code.
DarkCloud

Posts: 164
Joined: Dec 2009
Rep: 10

View Profile
Dec 24, 2009 10:53 PM #528223
I feel stupid I still don't understand it...
Aycaramba
2

Posts: 766
Joined: Apr 2007
Rep: 10

View Profile
Dec 24, 2009 11:07 PM #528232
Did you read the tutorial Flood gave you?
If you haven't, read it.
The Dude
2

Posts: 1,526
Joined: Dec 2007
Rep: 10

View Profile
Dec 24, 2009 11:27 PM #528243
You might want to learn about hitTests also.
DarkCloud

Posts: 164
Joined: Dec 2009
Rep: 10

View Profile
Dec 24, 2009 11:30 PM #528244
yea I did learn heres my code idk if it works since the damn ball wont let gravity affect it!heres the hittest can you guys tell if its right
onClipEvent(enterFrame){
if (_root.ball.hitTest(_root.ground)){
vy *= 0;
vx *= 0;
}
}
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Dec 26, 2009 1:21 AM #528660
If the movieclip ball hits the movieclip ground, vy and vx are * by 0, making them 0. You then do nothing with these variables so nothing will happen. To try and explain it, you have vaiables, a value or setting. You have to tell flash what to do with these, for exmaple, telling it that the ball's x co-ordinate value should equal vx.