Main Content

You are here:

What are packages?

Download complete files for this tutorial (.fla and .as files)

Packages are a way of organizing and giving your classes you create uniqueness. All the classes in the Flash player API use packages. They are like file systems. For example, all the classes in the Flash.geom package and all its sub-packages are classes dealing with geometry. This allows to to easily reference the .as files inside, that store the classes. The circle class would therefore be referenced as "flash.geom.Circle" and would be stored at "flash\geom\Circle.as" in the Flash player API. This is the method you should use to store your classes. So let's say we're creating a project which is a car racing game. We'd need a RaceCar class and a Referee class for starters. So let's just start with that. You'd have to create a new folder hierarchy, probably something like "com\foundationflash\example\game\characters\". Then create a new ActionScript file. inside just write something simple (as we are only learning about packages here) like this:

package com.foundationflash.example.game.characters
{
        public class RaceCar
        {
                public function goRace():void
                {
                        trace("Zooooooooom!");
                }
        }
}

Then save it as RaceCar.as. We have now created a RaceCar class in our example folder hierarchy. Now create a new ActionScript file in the same folder again, called Referee.as:

package com.foundationflash.example.game.characters
{
        public class Referee
        {
                public function goRef():void
                {
                        trace("Whistle!");
                }
        }
}

Save the file and start a new ActionScript file. I called it GameGo.as. Save it in your root folder (i.e. the folder in which your "com" folder is located. In the file, type:

package
{
        import com.foundationflash.example.game.characters.RaceCar;
        import com.foundationflash.example.game.characters.Referee;
        import flash.display.Sprite;
 
        public class GameGo extends Sprite
        {
                public function GameGo()
                {
                        var ref:Referee = new Referee();
                        ref.goRef();
                        var car1:RaceCar = new RaceCar();
                        car1.goRace();
                        var car2:RaceCar = new RaceCar();
                        car2.goRace();
                        var car3:RaceCar = new RaceCar();
                        car3.goRace();
                }
        }
}

You don't need to define the package location because this is the default package. You then import the RaceCar and Referee classes and the sprite class, to make the GameGo class extend the sprite class, so it can use its properties and methods. Then create an instance of Referee and 3 instances of RaceCar and reference their functions goRef and goRace.

Now we have our fully working GameGo class, it's time to make it work. Open a new .fla and save it in the same folder and GameGo.as. Set the document class to GameGo, this will make it run immediately. Test Your movie, and you should see in the output window:

Whistle!
Zooooooooom!
Zooooooooom!
Zooooooooom!

Thanks for reading!

Leon

Comments