Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 4 of 4

Thread: Tutorial 7 - the packages

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,454
    Rep Power
    8

    Tutorial 7 - the packages

    amazing Flash templates
    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

  2. #2
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    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!

  3. #3
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,454
    Rep Power
    8

    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);
    		}
    	}
    }

  4. #4
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    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.

+ Reply to Thread

Similar Threads

  1. Business & Personal Webhosting Packages.
    By salmank502 in forum Off Topics
    Replies: 0
    Last Post: 28-10-09, 05:38
  2. Tutorial 4 - le funzioni
    By Flep in forum Actioscript 3.0 base - tutorials
    Replies: 6
    Last Post: 13-09-09, 16:45
  3. Object Oriented Programming - lezione 7 - packages
    By Flep in forum Programmazione Orientata agli Oggetti - tutorials
    Replies: 10
    Last Post: 04-02-09, 20:47
  4. tutorial 9 - Inheritance
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 0
    Last Post: 10-03-08, 06:55
  5. Tutorial 8 the constants
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 0
    Last Post: 30-10-07, 06:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO