6 Replies Latest reply on Apr 6, 2013 12:44 PM by sinious

# Curved Path with actionscript

Hi I am building my first game. I'm aming to keep all code in seperate actionscript files (one for each class and a main constructor .as) linked to my fla.

So far I have added the backgound movieclip and one moving object (mc). i.e. 2 class files) It all looks great so far.

In my game I have a projectile (first of many) falling out of a specific x;y point. I have a timer to change the x;y positions. This works well but it is going in a straight line.From parabola formulas I know y = x^2 or y = x*x, but these don't work.

1) Please tell me what code to use to make a projectile's path curved or do I need a physics engine to handle this? Any ointers to tutorials will be much appreciated.

2) I'm going to have many of these items fall out from the same x;y coordinates but i'd like the curve to vary. So I suppose I must add a random number to the formula - i. How do I do that? In normal life I would do y = x^2 + i.

3) The "falling projectiles" are all going to match letters of the alphabet, so I suppose I will not have different instances of the same movieclip but rather different classes for each letter. Am I on the right path? How taxing will this be on my swf file size?

Here is the very basic, but perfectly working code so far:

import flash.display.MovieClip;
public class A extends MovieClip
{
public function A()
{
x = 300;
y = 30;
alpha = 0.1;

}
{
alpha = alpha + 0.06;
}

public function moveWithCurve():void
{
x = x + 3;
y = y + 4;
}
}
}

Message was edited by: buggerschool

• ###### 1. Re: Curved Path with actionscript

You should calculate a curve (e.g. Bezier) ahead of time and every frame update the position based upon your premade calculations. You want to calculate this ahead of time to reduce overall overhead frame to frame. If you just fill an array with a bunch of x/y positions and run through them frame to frame you're not really calculating anything, just assigning the x and y position frame to frame.

Learning how to calculate curves is a subject itself. Here's a pretty simple intro into calculating a multi-point curve which you can simply yourself into a single point if you need, but it's nice to have multi-point flexibility:

Just convert the short sample source into AS3, e.g. Double can be a Number, the precision should be good enough.

• ###### 2. Re: Curved Path with actionscript

Thank you. I'll study the link and post my resulting code

• ###### 3. Re: Curved Path with actionscript

actionscript 3 has a beziersegment class that does all the work for you.

• ###### 4. Re: Curved Path with actionscript

Greensock's implementation is far more robust, especially for multiple path followers and automatic animation.

https://www.greensock.com/as/docs/tween/com/greensock/motionPaths/MotionPath.html

• ###### 5. Re: Curved Path with actionscript

Thanks Sinious,

This looks very easy to implement. Love the clearly explained comments!

• ###### 6. Re: Curved Path with actionscript

You're welcome and good luck!