views:

40

answers:

1

I'm really new to actionscript so I'm not even sure how to ask this. First off I'm not using any Adobe IDE just notepad with flex as a compiler. What I want to know is how to make a class but make it with arguments and then have that class use those arguments.

The only way I can clarify what I mean is through an example. So for example say I have my main class and a class called square. Now I think (and i could be wrong) I can 'make' a square class in the main class by simply saying new square(); in some function of the main class. But lets say I want this square class to have a x and y value. Would I establish this by saying new square(x,y); in the main class (where x and y are integer values)? If not how so? Also how would I get the square class to read these values? How would I go about getting the square class to draw a square with its center at the x,y given to it in the main class?

+2  A: 

You would specify those in the class constructor. So for example:

Square Class:

public class Square
{
    //Create two private variables that will hold the width and height of the square
    private var _width:Number;
    private var _height:Number;

    /*
    This is the class constructor, here we specify what parameters
    are needed to create an instance of this class
    */
    public function Square(width:Number, height:Number)
    {
        _width = width;
        _height = height;
    }

    //Calculate the are of this square
    public function area():Number
    {
        return width * height;
    }
}

Using the square class

var my_square:Square = new Square(50, 50);
trace(my_square.area());

Is this what you're talking about? If so, I'd suggest reading up on some introductory tutorials on classes in flash (preferably in AS3).

Like: http://www.kirupa.com/developer/as3/classes_as3_pg1.htm

Fox