Results 1 to 5 of 5

Thread: creating and importing classes

  1. #1
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default creating and importing classes

    I have pretty much gotten the hang of coding in a document class but I would like to start breaking up my code into classes that are called up in the document class:

    lets say my document class is named "shootemup", it will be named shootemup.as to go along with shootemup.fla -which has shootemup in the document class field in the properties.
    step 1:

    here is my code :
    Code:
    package{
    //imports here
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.*
    import flash.display.*
    public class shootemup extends MovieClip{
    //begin vars here if any
    //begin constructor:
    public function shootemup()
    {
    
    trace("the constructor is running");
    
    
    }//end constructor
    
    //private functions go here
    }//end class
    
    
    }//end package
    step 2:
    But if I want to create a new class like this:

    Code:
    package importme{
    //imports here
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.*
    import flash.display.*
    public class importme {
    //begin vars here if any
    //begin constructor:
    public function importme()
    {
    
    trace("class importme is running too");
    
    
    }//end constructor
    
    //private functions go here
    }//end class
    
    
    }//end package
    step 3:
    import class and get it to run:(this is the part I am not sure about)
    I go back into my document class and add the lines in bold(I think)
    Code:
    package{
    //imports here
    
    import importme
    /*importme is not in a folder otherwise it would be importme.importme or com.importme*/
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.*
    import flash.display.*
    public class shootemup extends MovieClip{
    //begin vars here if any
    
    
    //not sure: var importme: new Imortme
    //begin constructor:
    public function shootemup()
    {
    
    trace("the constructor is running");
    //start importme function
    importme();
    
    //or is it
    
    //addchild(importme)
    
    
    }//end constructor
    
    //private functions go here
    }//end class
    
    
    }//end package
    -I am new at this so any help is appreciated.
    Last edited by evan; 07-07-2009 at 10:39 PM.

  2. #2
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    I like to think of packages as folders. When you are stating that the package is importme that means that that specific class is in the folder importme.

    so say you have a simple class called CreateBox...

    I always create an AS folder for my projects. so my package would be AS. this is what the code would loom like

    Code:
    package AS
    {
    	import flash.display.Sprite;
    
    	public class CreateBox extends Sprite
    	{
    		private var mc:Sprite;
    		public function CreateBox(width:Number, height:Number, color:uint, alpha:Number=1)
    		{
    			super();
    			mc = new Sprite();
    			mc.graphics.beginFill(color, 1);
    			mc.graphics.drawRect(0,0,width,height);
    			mc.graphics.endFill();
    			mc.alpha = alpha;
    			addChild(mc);
    		}
    		
    	}
    }
    now how to call that from your main document class. really depends on where your writing your code and how you have the project setup. I use eclipse with the flex sdk so it keeps track of all my folders in my project. And knows my AS folder is a resource for classes. If your using flash you can setup in the export settings where it should look for specific folders. Lee brimelow has a tutorial on this over at GotoandLearn

    for the sake of my example though its simple when you want to use that class then you would import AS.CreateBox;

    then you can create instances of that class by saying
    Code:
    box = new CreateBox();
    so your full document class would look like this then;

    Code:
    package
    {
            import AS.CreateBox;
    
    	import flash.display.Sprite;
    
    	public class Main extends Sprite
    	{
    		private var box:CreateBox;
    		public function Main()
    		{
    			super();
    			box = new CreateBox(10,15,0x000000,1);
    			addChild(box);
    		}
    		
    	}
    }
    hope this helps, and clears it up for you.

  3. The Following User Says Thank You to punstc For This Useful Post:

    evan (07-07-2009)

  4. #3
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default Perfect

    Thank you! for that short sweet simple example! Much needed, as I am just getting started with OOP. What does the "super();" call do here?
    with this line:
    Code:
    public function CreateBox(width:Number, height:Number, color:uint, alpha:Number=1)
    is this done to keep the variables within the method? otherwise I could declare the vars outside and just call them in the function, or I could just hard code the properties.

    What is the advantage of coding it this way?
    Last edited by evan; 07-07-2009 at 10:37 PM.

  5. #4
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    Too tell you the I can't give you so actual reason for the super, It has something to do with passing variables through the class which it extends.

    I believe how it works is that since we are extending sprite, the actual sprite class from adobe gets ran and then your code gets added onto it. Some one can corrected me if I'm wrong but thats the impression I got when I was researching.

    I was really told that its best practice to have it even if you don't need to use it, and eclipse always just throws it in there when you tell it to extend a class.

    There are times that is is necessary though like when extending the event class.

    heres an example of that just so you can see.
    Code:
    package AS
    {
    	import flash.events.*;
    	
    	public class ButtonClickedEvent extends Event
    	{
    		public static const LOAD_MOVIECLIP:String = "Load Movie Clip";
    		public static const CHANGE_LISTENERS:String = "Add and Remove Button MouseEvents";
    		public var buttonClicked:String;
    		
    		public function ButtonClickedEvent(type:String, button:String, bubbles:Boolean=true, cancelable:Boolean=false)
    		{
    			super(type, bubbles, cancelable);
    			buttonClicked = button;
    		}
    		
    		override public function clone():Event
            {
                return new ButtonClickedEvent(type, buttonClicked);
            }
     
            override public function toString():String
            {
                return formatToString("ButtonClickedEvent", "type", "buttonClicked");
            }
    
    	}
    }
    Like I said I can't tell you specifically why, Maybe Medyman or Blizzard would be able to give you a more direct reason. for everything I've written the only time i've really had to use it was when extending event class to create my own events.

    as for creating parameters. for me it's just easier to define the dimensions, color, and alpha of the box when i create it. Most of the time I use something like that to create a bar along the bottom of a site, or for a holder box for images/thumbnails so its nicer for me to define the properties of the box right away so it can automatically draw instead of me having to write extra getter setters to define the properties, and an extra function to tell it to draw the box.

    hope that helps a bit

  6. The Following User Says Thank You to punstc For This Useful Post:

    evan (07-08-2009)

  7. #5
    Join Date
    Jan 2008
    Location
    Near Chicago
    Posts
    247
    Thanks
    105
    Thanked 2 Times in 2 Posts

    Default

    thanks again -this is a leap forward for a recovering timeline coder lol.

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
  •