In this seventh article about Object Oriented Programming with Actionscript 3.0 , we will see how to use packages.
The packages are nothing else than a group of classes put together to subdivide and organize our Flash project.
Clearly, the more an application is important and rammed, the more it is worthwhile to divide the logics in packages so that to create a well organized structure but above all modular.
Every package will have its own classes in base to the theme of the package.
For instantiate, if we wanted to implement a shopping cart in Flash to an e-commerce, we would have a package named buyer, cart, another payment etc etc...
This way, we will have everything under control and if something had to go wrong, we immediately know in what package to check without having to check the whole application.
Each package needs a unique name.
Each class inside the package needs a unique name, so in the package buyer, only one class can be named User. Another class of the name User could then exist inside the package cart.
Inside a package, we can have another package defined as subpackage.
By convention the principal package is always defined with the domino of first level of the developer or organization, for instantiate in my case (flepstudio.org) the main package would be org with a subpackage flepstudio. For instance, the project would be: org.flepstudio.projectName.category.className.
Let me explain myself better:
Let us say that I want to build an application that I call site.
In this case I would have: org.flepstudio.site and then I could for instantiate divide my classes by categories: org.flepstudio.site.utilities and org.flepstudio.site.services or still org.flepstudio.site.portfolio or also org.flepstudio.site.tutorials etc etc...
Let us see some concrete examples...
EXAMPLE 1
I create the FLA of the project and name it seventh.fla.
I create a folder (in this case we won't call it folder but package) named org. Into which, I create another folder named flepstudio.


Now, I create the projectís Document Class, an AS file saved as Seventh.as inside the package flepstudio:

implemented the following way:
Code:
package org.flepstudio
{
import flash.display.MovieClip;
public class Seventh extends MovieClip
{
public function Seventh()
{
trace('The Document Class was instantiated correctly');
}
}
}
To notice, the first line defines the package
package org.flepstudio
we are telling Flash that the class Seventh has the following path: org.flepstudio
Up till now, we assigned the Document Class of the project's FLA, in the most obvious way.
Alternatively, I can type in the Document Class filed org.flepstudio.Seventh .

Publishing the FLA, we obtain the following output:
Instance of the Document Class created correctly
I now create another package into flepstudio (where Seventh.as is already placed) and name it site.

Next, I create 4 packages (folders) into site and I name them:
- gallery
- portfolio
- services
- utilities

I discount the fact that in each of these packages, we will create some classes that will manage our application. This way, we have a modular structure and every package will have its own classes that will manage a specific part of the whole project.
We make an example creating the class Images.as in the package org.flepstudio.site.gallery, therefore we save the class Images.as in the folder gallery.
‘Images.as’ will be declared the following way:
Code:
package org.flepstudio.site.gallery
{
public class Images
{
public function Images()
{
trace(ìInstance of the class Images created correctlyî);
}
}
}
At this point, to instantiate the class Images from the Document Class, I must first import Images in Seventh.as and then instantiate Images.
So I go back to Seventh.as and I write:
Code:
package org.flepstudio
{
import flash.display.MovieClip;
import org.flepstudio.site.gallery.Images;
public class Seventh extends MovieClip
{
private var images:Images;
public function Seventh()
{
trace('The Document Class was instantiated correctly ');
images=new Images();
}
}
}
import org.flepstudio.site.gallery.Images: I import Images.as telling flash where to find it
private var images:Images;: I create a property of type Images (once the class Images is instantiate, we can refer to it without the need to write the full path)
images=new Images();: I instantiate the class Images
and I obtain the following output:
The Document Class was instantiated correctly
Instance of the class Images created correctly
EXAMPLE 2:
Continuing from the previous example, I create two classes ( Client1.as and Client2.as ) in the package portfolio:
Code:
package org.flepstudio.site.portfolio
{
public class Client1
{
public function Client1()
{
trace('The class Client1 has been instantiated correctly');
}
}
}
Code:
package org.flepstudio.site.portfolio
{
public class Client2
{
public function Client2()
{
trace('The class Client2 has been instantiated correctly');
}
}
}
I import them and I instantiate them from the Document Class (Seventh.as) the following way:
Code:
package org.flepstudio
{
import flash.display.MovieClip;
import org.flepstudio.site.gallery.Images;
import org.flepstudio.site.portfolio.*;
public class Seventh extends MovieClip
{
private var images:Images;
private var client1:Client1;
private var client2:Client2;
public function Seventh()
{
trace('The Document Class was instantiated correctly ');
images=new Images();
client1=new Client1();
client2=new Client2();
}
}
}
import org.flepstudio.site.portfolio.*; with the asterisk, I tell Flash to import all the classes present in the package portfolio
I declare the classes
private var client1:Client1;
private var client2:Client2;
I instantiate the classes
client1=new Client1();
client2=new Client2();
and I obtain the following output:
The Document Class was instantiated correctly
Instance of the class Images created correctly
The class Client1 has been instantiated correctly
The class Client2 has been instantiated correctly
EXAMPLE 3:
Continuing, again, from the previous example, I create a class Design.as in the package services:
Code:
package org.flepstudio.site.services
{
public class Design
{
public function Design()
{
trace(ëThe class Design has been instantiated correctly');
}
}
}
and another class Calculator.as in the package utilities:
Code:
package org.flepstudio.site.utilities
{
public class Calculator
{
public function Calculator()
{
trace(íThe class Design has been instantiated correctly');
}
}
}
I import, declare and instantiate Design.as in the Document Class:
Code:
package org.flepstudio
{
import flash.display.MovieClip;
import org.flepstudio.site.gallery.Images;
import org.flepstudio.site.portfolio.*;
import org.flepstudio.site.services.Design;
public class Seventh extends MovieClip
{
private var images:Images;
private var client1:Client1;
private var client2:Client2;
private var design:Design;
public function Seventh()
{
trace('The Document Class was instantiated correctly');
images=new Images();
client1=new Client1();
client2=new Client2();
design=new Design();
}
}
}
nothing new up till now. But if I wanted to instantiate Calcolatore.as from Design.as?
I should import, declare and instantiate Calcolatore in Design the following way:
Code:
package org.flepstudio.site.services
{
import org.flepstudio.site.utilities.Calcolatore;
public class Design
{
private var calculator:Calculator;
public function Design()
{
trace(íThe class Design has been instantiated correctly');
calculator=new Calculator();
}
}
}
import org.flepstudio.site.utilities.Calculator; I import
private var calculator:Calculator; I declare
calculator=new Calculator(); I instantiate
and I obtain the following output:
The Document Class was instantiated correctly
Instance of the class Images created correctly
The class Client1 has been instantiated correctly
The class Client2 has been instantiated correctly
The class Design has been instantiated correctly
The class Calculator has been instantiated correctly
Clearly, the instance of Calculator named calculator will not be created from the Document Class but from Design.as as it is imported and instantiated there.
Source files:
Bookmarks