Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 7 - the packages

This is a discussion on Tutorial 7 - the packages within the Object Oriented Programming - tutorials forums, part of the Tutorials category; In this seventh article about Object Oriented Programming with Actionscript 3.0 , we will see how to use packages. The ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Object Oriented Programming - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 26-10-07, 06:52
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 7 - the packages

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.

La Document Class

La Document Class

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

La Document Class

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 .

La Document Class

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 correct
ly

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 correctl
y

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 correct
ly

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:
Attached Files
File Type: zip tutorial_7.zip (15.3 KB, 50 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 13:49..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 12-11-07, 02:38
Member
 
Join Date: Nov 2007
Posts: 59
Rep Power: 2
gwulfwud is on a distinguished road
Re: Tutorial 7 - the packages

Thank you for this! now i know how to deal with those freaking packages...by the way...what if you're not going to use the "Document class" from the Properties tab to call the main AS? do you use the import inside the flash file??


Thanks man!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 12-11-07, 07:01
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: Tutorial 7 - the packages

Hi gwulfwud,
yes i use the import.
Example:
i have test.fla and Test.as ( DC ) :
Code:
import Test;
var my_test:Test=new Test();
addChild(my_test);
Code:
package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	public class Test extends MovieClip
	{
		public function Test()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			trace(this.root);
		}
	}
}
__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 13-11-07, 02:42
Member
 
Join Date: Nov 2007
Posts: 59
Rep Power: 2
gwulfwud is on a distinguished road
Re: Tutorial 7 - the packages

thanks for the reply flep! really helped

oh btw, i really wanted to make an mp3player, i downloaded your basic audio player so i can have an idea on how to create my own php xml player. do you have any source files that has english comments on it? a tutorial would be great...well not really a tutorial, but just an explanation on how some of the codes works =D thanks again!!

g.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 4 - le funzioni Flep Actioscript 3.0 base - tutorials 2 11-11-08 11:18
Object Oriented Programming - lezione 7 - packages Flep Programmazione Orientata agli Oggetti - tutorials 2 28-10-08 14:12
Tutorial 1 the variables Flep Actionscript for beginners - tutorials 0 01-11-07 06:47
Tutorial 8 the constants Flep Object Oriented Programming - tutorials 0 30-10-07 06:47
Tutorial Cercasi joyz Actionscript 3.0 base 22 30-07-07 10:52


All times are GMT. The time now is 05:08.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406