PDA

View Full Version : Help with Classes


GandalfPT
11-07-2003, 02:44 PM
hi there.

This is the first time i work with classes. the closest thing i have ever made where _global functions. and i never programed in anything else beside AS, so i dont have a background on understanding the Classes. anyway i read some tutorials on Classes and i think i got the general idea.

Now, for my experiment, i whanted to create a class that i could use to create windows with round corners and some text inside.

I managed to do that, but many of the pieces of code seams....like a real mess.

this is my Quad.as file.


class Quad {

// class properties
static var classInstances:Number = 0;
var xPos:Number;
var yPos:Number;
var xSize:Number;
var ySize:Number;
var corner:Number;
var color:String;
var name:String;
var myObjectReference:Object;


// class method to return the property values
function buildQuad() {
//trace(classInstances);
classInstances++
myObjectReference.createEmptyMovieClip(name, classInstances);
with (myObjectReference[name]){
//----- window ----------------------------------------------------------
lineStyle(2, 0x000000, 100);
beginFill("0x" + this.color, 100);
moveTo(this.xPos + this.corner, this.yPos);
lineTo(this.xPos + this.xSize - this.corner, this.yPos);
curveTo(this.xPos + this.xSize, this.yPos, this.xPos + this.xSize, this.yPos + this.corner);
lineTo(this.xPos + this.xSize, this.yPos + this.ySize - this.corner);
curveTo(this.xPos + this.xSize, this.yPos + this.ySize, this.xPos + this.xSize - this.corner, this.yPos + this.ySize);
lineTo(this.xPos + this.corner, this.yPos + this.ySize);
curveTo(this.xPos, this.yPos + this.ySize, this.xPos, this.yPos + this.ySize - this.corner);
lineTo(this.xPos, this.yPos + this.corner);
curveTo(this.xPos, this.yPos , this.xPos + this.corner, this.yPos);
//----- textbox ---------------------------------------------------------
createTextField ("TextBox", 10, this.xPos + 8, this.yPos + 8, this.xSize - 8, this.ySize - 8);
CaixaTexto.type = "dynamic";
CaixaTexto.multiline = true;
CaixaTexto.html = true;
CaixaTexto.selectable = false;
CaixaTexto.wordWrap = true;
CaixaTexto._highquality = 2;
CaixaTexto.htmlText = this.name;
//-----------------------------------------------------------------------
}
}

function dragOn() {
myObjectReference[name]._alpha = 50;
myObjectReference[name].swapDepths(myObjectReference[name].getNextHighestDepth());
startDrag(myObjectReference[name], false);
}

function dragOff() {
myObjectReference[name]._alpha = 100;
myObjectReference[name].stopDrag();
}

// constructor function
function Quad (my_xPos:Number, my_yPos:Number, my_xSize:Number, my_ySize:Number, my_corner:Number, my_color:String, my_name:String, my_object:Object){
xPos = my_xPos;
yPos = my_yPos;
xSize = my_xSize;
ySize = my_ySize;
corner = my_corner;
color = my_color;
name = my_name;
myObjectReference = my_object;
}
}



and this is my FLA file.


var quad_1:Quad = new Quad(0,0,100,100,10, "F74A33", "window_1", _root);
var quad_2:Quad = new Quad(200,0,100,100,10, "FFFF2B", "window_2", _root);
var quad_3:Quad = new Quad(0,200,200,200,40, "3DBDED", "window_3", _root);

quad_1.buildQuad();
quad_2.buildQuad();
quad_3.buildQuad();

quad_1.myObjectReference[quad_1.name].onPress = function(){
quad_1.dragOn();
}
quad_1.myObjectReference[quad_1.name].onRelease = function(){
quad_1.dragOff();
}

quad_2.myObjectReference[quad_2.name].onPress = function(){
quad_2.dragOn();
}
quad_2.myObjectReference[quad_2.name].onRelease = function(){
quad_2.dragOff();
}

quad_3.myObjectReference[quad_3.name].onPress = function(){
quad_3.dragOn();
}
quad_3.myObjectReference[quad_3.name].onRelease = function(){
quad_3.dragOff();
}


can you guys give me some hints on how to optimize or do things the correct way or a better way at least?.

for example, in the buildQuad function i have to keep repeating the "this." to access thos vars. thats stressing.

another thing can i make the onPress and the onRelease inside the class? so i dont have to put all those instructions in the FLA?

anyway, thanx in advance for your info on my work.

GandalfPT

ferdymania
11-07-2003, 07:10 PM
nah its not a mess..
very good in fact..
you dont have to use "this" at all..
in fact i think it is discouraged in as2
(but not real sure)
but it seems fine to me..
you could extend MovieClip and that would give you
all the movieclip methods if you wanted...
..
as for the event detection. i usually leave it in the fla..
my opinion is that is where event detection should be..
2 cents worth..
steve

GandalfPT
11-11-2003, 07:47 PM
Thanx for your opinion ferdymania.

Im going to try the extend MovieClip, maybe it will work out the "this" problem, the problem is that if i dont put the "this", i cant access those vars :P

maybe with that extend its resolved.... i have to try it out :P

still a newbie around here.....


:p :p