I am in a pinch for time and know very little about Flash and even less about Actionscript, but I need to know how to make a ball fall according to gravity!
Email me!
Thomas
<a href=“mailto:ttorrey@slc.edu”>ttorrey@slc.edu</a>
I am in a pinch for time and know very little about Flash and even less about Actionscript, but I need to know how to make a ball fall according to gravity!
Email me!
Thomas
<a href=“mailto:ttorrey@slc.edu”>ttorrey@slc.edu</a>
Here you go man…
Make a ball using the Flash drawing tools… Select the entire ball and right click and convert this to a symbol… Make it a movieclip.
Go back to the main movie (Scene1) and click on this new movieclip symbol… You should see in your properties an instance name field… Put in this field the name : ball
Right click on the movieclick and go to Actions… An actionscript window should appear… Now… Copy and Paste the following code…
[AS]
onClipEvent (load)
{
this.xspeed = 0;
this.yspeed = 0;
}
onClipEvent (enterFrame)
{
this.xspeed += _root.wind;
this.yspeed += _root.gravity;
this._x += this.xspeed;
this._y += this.yspeed;
}
[/AS]
Now… Click back into the main scene… Click on the first frame of this movie (should have the ball loctaed in it)… Then right click in this and click on Actions…
Copy and Paste the following code in there…
[AS]
_root.onLoad = function()
{
wind = 0;
gravity = 1.5;
}
[/AS]
I also included a wind possibility… Incase you wanna add a “wind” variable in along with this… If you need more… Bounce and the ability to grab the ball and move it… Please drop me a line and I’ll get back to you on that…,
playamarz :player:
marz: You don’t need to add an onLoad on the main timeline, once the frame is hit, that info is loaded anyway, and onLoad is for things like loadVars and XML, etc, etc.
www.bit-101.com has an excellent tutorial on the concept of gravity as well.
And Marz: I just read over your whole thing, I noticed you don’t change the _x position at all, so technically your code does nothing
There you go… I forgot to place in those two lines of code… So sue me…
And I use onLoad in the main timeline because it’s faster than Movieclip loading… Look into it buddy
Thanks for pointing out my mistake though… I tend to **** up ever so often like that… lol :beam:
You don’t need to do onLoad though, you can just add
[AS]wind = 0;
gravity = 1.5;[/AS]
To the main timeline
And also… I just realized another thing, that doesn’t follow gravity, it just movies it down at the speed you set for the gravity variable… wouldn’t you also have to do a gravity++ in the enterFrame as well?
I don’t see what the ball is meant to do once the actions have been added.???
It just sits on the stage when you test the movie:)
great! now what do i do?
Well I wrote out a huge explanation about everything but accidently hit Reset Form thinking it was the preview option… KIRUPA! Why do they have reset form anyways…
Dammit dammit dammit… Here is what I was going to show you though…
[SWF=“http://www.fantom-stranger.com/~mentalconcepts/experiments/balltest.swf width=650 height=350”][/SWF]
It’s an old method I use to use… But it works out pretty good… You can download the .fla here…
http://www.fantom-stranger.com/~mentalconcepts/experiments/balltest.fla
Take care and see ya around.
playamarz :player:
www.bit-101.com has a tutorial on it, it’s a great tutorial, it is where I learned gravity.
been doing ball play for a while. here’s a tidy way to wrap up movement.
create a circle, make it a movie clip, name it ball.
right click on it in the library, and choose linkage
set linkage to “export”, and tag it the default “ball”
layer one, frame one
_global.ballClass = function(){
}
ballClass.prototype = new MovieClip();
ballClass.prototype.onLoad = function(){
yVelocity=0;
xVelocity=20;
gravity=2;
lossOfPower=.94;
bottem=400;
top=0;
left=0;
right=600;
}
ballClass.prototype.onEnterFrame = function(){
xVelocity*=lossOfPower;
yVelocity*=lossOfPower;
yVelocity+=gravity;
this._x+=xVelocity;
this._y+=yVelocity;
if(this._y+this._height>=bottem||this._y<=top){
yVelocity=-yVelocity;
}else if(this._x+this._width>=right||this._x<=left){
xVelocity=-xVelocity;
}
}
Object.registerClass("ball",ballClass);
_root.attachMovie("ball","ball",1);
Should give an interesting bounce anyway. I hadn’t thought of including wind speed in when I was doing these so my code is missing that… but a nice random breeze alteration would be nice.
That code does nothing for me david :-\
I don’t really know how to edit it as OOP is like my archenemy (as well as server-side flash)
oops… let me try to fix that.
arg… left the attach movie code out.
I fixed it… I think
Yep, its fixed, but that is one freaky lookin’ bounce there…lol :beam:
Man, I am super tired, I think i’m off to bed now
lol… I dont’ even know what it looks like. I did that from memory.
I think I wrote a tutorial in here too, didn’t I? I’m not sure it is better than Bit’s though, the old man the very fluent
Thats right Ilyas… you did!!! Man, I haven’t used that tutorial in ages, I forgot about it…
I can see now why my example is a little screwy. I need a stoper for the movement.
going to go look at some older work to see what I left out.
:: Copyright KIRUPA 2024 //--