Log in

View Full Version : creating and importing classes



evan
07-07-2009, 04:06 AM
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 :

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:


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)


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.:confused:

punstc
07-07-2009, 07:30 PM
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



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

box = new CreateBox();

so your full document class would look like this then;



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.

evan
07-07-2009, 10:30 PM
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:

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?

punstc
07-08-2009, 02:16 PM
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.


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

evan
07-08-2009, 09:06 PM
thanks again -this is a leap forward for a recovering timeline coder lol.