8bitrocket.com
8Jan/080

Moving From Flash AS2 To Flash AS3: Packages

(Note: I'm converting my Flash game "Spin City" from AS2 to AS3 so it can be reskinned for another project. I've decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)

I've never been a huge fan of packages for my code. I'm not sure why, because I have pretty much grasped onto every other piece of Object Oriented methodology. For Some reason though, the use of packages has eluded me. Of course, I have referenced them to include other people's code and our own common libraries, but I've never felt the need to use them myself.

To work in Flash AS3 wth classes, packages are required. So let't take a quick look at packages in Flash AS2 and see how they relate to Flash AS3.

Flash AS2

In Flash AS2, a package is directly related to the directory structure that the classes are saved into. If you felt like creating a package for your game named fashiondash , you would create a directory named /fashiondash and save the main game file as /fashiondash/Game.as.

You would then make sure to define your class like the:

[cc lang="javascript" width="550"]
import TimerClock;
import ScoreBoard;
class fashiondash.Game {
public function Game() {
}
}
[/cc]

By saving your FashionDash.fla in the root directory that also contains the /fashiondash directory, your .fla would have access to call the necessary classes to compile correctly. Similarily, you could add /fashiondash directory to your ActionScript 2 classpath (through the Preferences option on the File menu) to achieve the same result.

By the way, the common naming convention for packages is something like this:

com.8bitrocket.fashiondash : [company]/[url]/[project]

Where the directory structure would relate like this: /com/8bitrocket/fashiondash . This is done so that classes barrowed or reused from other sources or projects don't step on each other. I personally am not a fan of doing it this way, but of course, by all means, use this naming convention if it suits you.

There are many more facets to packages in Flash AS2, but these are the basics.

Flash AS3

Packages are not an option in Flash AS3 as you are forced to use Packages if you want to use any kind of class or object structure. If you decide you do not want to create a package structure, you can simple use the following syntax to create a class:

[cc lang="javascript" width="550"] package {
import TimerClock;
import ScoreBoard;
public class Game {
public function Game()
}
}
}
[/cc]

In Flash AS2, the public attribute of the class defintion was optional. In Flash AS3 it is not . This is because classes can be internal (with the internal attribute or none at all), and this means that they can only be accessed by other members of the same package. We need the Game class to be accessed from outside of the package, so we much designate it as public.

For our purposes, we are going to save all of our code in a directory named /fashiondash and access it as a package. To do this, we much use the following syntax to create our class:

[cc lang="javascript" width="550"] package fashiondash {
import TimerClock;
public class Game {
public function Game() {
}
}
}

package fashiondash {
public class ScoreBoard {
public function ScoreBoard() {
}
}
}

[/cc]

Notice that we no longer need to import the Scoreboard class. If we define ScoreBoard to be to be in the fashiondash package, we don't have import it into our class. Of course the FashionDash class would be saved in it's own file named FashionDash.as, and ScoreBoard would be saved in ScoreBoard.as. The code above is just a demonstration of what both classes might look like.

Classes inside the fashiondash package don't need to be accessed from outside the package. Because of this, we can define them without the public attribute like this: [cc lang="javascript" width="550"] package fashiondash {
import TimerClock;
class TitleScreen {
public function TitleScreen() {
}
}
}
[/cc]

Now, to further reorganize our code we will create a gameutilities package that will hold classes that we can resue for other projects. We are going to put the TimerClock class in the new package. First we create a /gameutilities directory, and copy TimerClock.as into it. Then we alter the code to look something like this: [cc lang="javascript" width="550"] package gameutilities{
public class TimerClock {
public function TimerClock() {
}
}
}
[/cc]Finally, to reference this in Game as an import, we now need to reference the package as well: [cc lang="javascript" width="550"] package fashiondash {
import gameutilities.TimerClock;
public class Game {
public function Game() {
}
}
}

[/cc]

OK, that it for lesson #1. Very simple I suppose, but also just different enough to cause problems. I assume that many aspects of AS3 fall into this same category.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
Filed under: Tutorials Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.

This site is protected by Comment SPAM Wiper.