24 August 2009

Flash AS3 Star System Simulation Tutorial

Download Tutorial as PDF file.

As I promised, earlier this step-by-step tutorial will show you how you can create your own working star system simulation using Flash and ActionScript3.0 code. I like to think this is tutorial for beginners because it is written by one and code I'll explain here is very simple just around 50 lines stored in single class. This star system simulation is not finished work, but rather just something to start from building further.

Let's start with planets. I will not explain here how you can create images of planets, just search the web, there are many good tutorials out there about it. You can use some dummy images for start, just different size circles. Import them with File . Import . Import to library. Create movie clips for your planets, center images and name them as you like it doesn't really matter.

For every planet movie clip in your library you will have to set Linkage parameters like image shows. For class parameter use different names. I have only two planets in this example, but you can use more if you like. My planets classes are PlanetSmall and PlanetBig.

We are almost done with Flash, we only need to declare document class. Within properties panel set StarSystem as Document class. You can also make full folders structure if you like and use com.blogspot.flanture.animations.StarSystem as Document class name, but I want to keep this simple.

Create new ActionScript file in same folder as your StarSystem.fla, name it StarSystem.as and open it for editing. Now the code.

First we import some libraries:

import flash.display.Sprite;
import flash.events.Event;
import flash.display.Stage;

We define our class:

public class StarSystem extends Sprite {

private var p1:PlanetSmall;
private var p2:PlanetBig;

Variables p1 and p2 are types of PlanetSmall and PlanetBig. You will need one variable for every planet you have.

Next we define two variables to represent center of the stage:

private var xCenter:Number = stage.stageWidth/2;
private var yCenter:Number = stage.stageHeight/2;

Now, for every planet we need different parameters. First parameter is angle and here we define starting animation angle, two more parameters are xRadius and yRadius needed to define elliptic movement of planets and one more parameter is planet moving speed. So, for my two planets I have:

private var angle:Number = 20;
private var xRadius:Number = 250;
private var yRadius:Number = 150;
private var speed:Number = .008;

private var _angle:Number = -45;
private var _xRadius:Number = 100;
private var _yRadius:Number = 65;
private var _speed:Number = .01;

All these variables are something you need to test for yourself to find right numbers.

Then we define main method,

public function StarSystem() {

note how main method must be declared as public not private. Init function goes like this:

private function init():void {
p1 = new PlanetSmall();

p2 = new PlanetBig();

addEventListener(Event.ENTER_FRAME, onEnterFrame);

Here we simple set p1 as new PlanetSmall instance, and p2 as PlanetBig instance. Then we add those instances to the stage using standard addChild method. Finally we have event listener which file will fire on every enter frame event and this is where we define planets movement:

public function onEnterFrame(event:Event):void {
p1.x = xCenter + Math.sin(angle) * xRadius;
p1.y = yCenter + Math.cos(angle) * yRadius;
p1.scaleX = p1.scaleY = p1.y/400;
p1.alpha = p1.scaleX*2;
angle += speed;

p2.x = xCenter + Math.sin(_angle) * _xRadius;
p2.y = yCenter + Math.cos(_angle) * _yRadius;
p2.scaleX = p2.scaleY = p2.y/300;
p2.alpha = p2.scaleX*1.2;
_angle += _speed;

Last part of the code defines elliptic movement of the planets and it consists of two parts. If you have more planets you will need one block of code for every planet. Position x of the planet is calculated using sin function and y position is calculated using cos function. We use scale functions to make our planets smaller when they are away from viewing point (means they have smaller y value) and bigger when they are closer (bigger y value). Remember, Flash has different coordinate system, x axis has higher values from left to right and y axis has higher values from top to down.

We also set alpha as function of scale which is nice trick to use. Feel free to play with these numbers and see what suits your needs. Last line of the code changes angle of the planets as function of defined speed and this happens every new frame. Test your star system.

I hope you will make better looking rotating planets , some supernovas, stars and galaxies on background, animated Sun, maybe some starship or even planets with moons orbiting around them! When you do that, send me the link, I would like to see your creation.

Thanks for your time.



Anonymous said...

Thanks for the tutorial! Is there a working example to view?

flanture said...

yes, check previous post or live example here

Erik Nelson said...

This is helpful. It's a lot like what I am trying to do.

What I need help figuring out though is putting planet-type objects into an array, so I can loop through the array to calculate mutual gravitational attractions.

The hard part then is getting a grasp on what kind of objects it should be. Movie clips? Sprites? How do I declare them in multiples?

flanture said...

hello Erik and thank you for visiting my blog.

if your planet object don't change and they are only graphical representation you use Sprite, but if you can't help yourself and you must include some animation like rotation or whatever you will be better off with Movie Clips.

What is your project about?

Erik Nelson said...

I have just succeeded in doing an array of movie clips with mutual forces between them.

It seems I can't use Sprite because I can't tack on custom attribute variables, but the Movie Clip class lets me do that.

(I can't define custom classes without creating a module, it seems, and I have no experience doing that yet.)

Anonymous said...

hi there, I like the tutorial. I'm trying to make game, like a planet simulation. I want to code like this example here "http://dan-ball.jp/en/javagame/planet/" can you give me the idea, how it works. thanks

flanture said...

you should check Flint http://slekx.com/2009/12/flint-visual-introduction/


template by blogger templates