PDA

View Full Version : My first class with attachmovie


Groges
07-29-2003, 05:53 AM
I have just attempted writing my first class for a button. The variables are all passed through correctly (verified with traces), I am pretty sure the problem is with my attachmovie syntax. My library contains topnavbutton_pb and topnavbutton_lbl, the class method is meant to attach both these movies place them via x and y and give the label some text via the LabelText variable. Any help would really be appreciated!! I have searched for an answer for this and could not find one, I would hate my first post to be a crossthread:p
here is the code:

TopNavButtonClass = function(ButtonName,ButtonInstanceName,ButtonDepth Number,ButtonxCoord,ButtonyCoord,LabelName,LabelIn stanceName,LabelDepthNumber,LabelxCoord,LabelyCoor d,LabelText ){
this.ButtonName = ButtonName;
this.ButtonInstanceName = ButtonInstanceName;
this.ButtonDepthNumber = ButtonDepthNumber;
this.ButtonxCoord = ButtonxCoord;
this.ButtonyCoord = ButtonyCoord;
this.LabelName = LabelName;
this.LabelInstanceName = LabelInstanceName;
this.LabelDepthNumber = LabelDepthNumber;
this.LabelxCoord = LabelxCoord;
this.LabelyCoord = LabelyCoord;
this.LabelText = LabelText;
trace("in class");
}
TopNavButtonClass.prototype.createbuttons = function(){
attachMovie(this.ButtonName,this.ButtonInstanceNam e);
[this.ButtonInstanceName]._x = [this.ButtonxCoord];
[this.ButtonInstanceName]._y = [this.ButtonyCoord];
attachMovie(this.LabelName,this.LabelInstanceName, this.LabelDepthNumber);
[this.LabelInstanceName]._x = [this.LabelxCoord];
[this.LabelInstanceName]._y = [this.LabelyCoord];
[this.LabelInstanceName].text_lbl.text=this.LabelText;
trace("in method");
trace(this.ButtonDepthNumber);
}

HomeButton = new TopNavButtonClass("topnavbutton_pb","topnavbutton_pb_Home",10,19,47,"topnavbutton_lbl","topnavbutton_lbl_Home",11,19,47,_Global.Top_Home_Button);
HomeButton.createbuttons();

Oh yeah if this code is bad please tell me better I learn the right way early,

Cheers

jaybee
07-29-2003, 10:46 AM
it's basically OK, but you have square brackets where you don't need them & you need to include the depth argument when you attach the first mc:

TopNavButtonClass.prototype.createbuttons = function(){
attachMovie(this.ButtonName,this.ButtonInstanceNam e,this.ButtonDepthNumber);
this[ButtonInstanceName]._x = this.ButtonxCoord;
this[ButtonInstanceName]._y = this.ButtonyCoord;
attachMovie(this.LabelName,this.LabelInstanceName, this.LabelDepthNumber);
this[LabelInstanceName]._x = this.LabelxCoord;
this[LabelInstanceName]._y = this.LabelyCoord;
this[LabelInstanceName].text_lbl.text=this.LabelText;
trace("in method");
trace(this.ButtonDepthNumber);
}


but as your class is not abstract, it's a series of objects on the stage, you should have a go at using components instead - that encapsulates the whole thing and is a neater way to do this.......so you would take your code and put it on the first frame of an empty clip and change it like this:


#initclip

TopNavButtonClass = function(ButtonName,ButtonInstanceName,ButtonDepth Number,ButtonxCoord,ButtonyCoord,LabelName,LabelIn stanceName,LabelDepthNumber,LabelxCoord,LabelyCoor d,LabelText ){
this.ButtonName = ButtonName;
this.ButtonInstanceName = ButtonInstanceName;
this.ButtonDepthNumber = ButtonDepthNumber;
this.ButtonxCoord = ButtonxCoord;
this.ButtonyCoord = ButtonyCoord;
this.LabelName = LabelName;
this.LabelInstanceName = LabelInstanceName;
this.LabelDepthNumber = LabelDepthNumber;
this.LabelxCoord = LabelxCoord;
this.LabelyCoord = LabelyCoord;
this.LabelText = LabelText;
trace("in class");
this.createButtons();
}
TopNavButtonClass.prototype = new MovieClip();

TopNavButtonClass.prototype.createbuttons = function(){
this.attachMovie(this.ButtonName,this.ButtonInstan ceName,this.ButtonDepthNumber);
this[ButtonInstanceName]._x = this.ButtonxCoord;
this[ButtonInstanceName]._y = this.ButtonyCoord;
this.attachMovie(this.LabelName,this.LabelInstance Name,this.LabelDepthNumber);
this[LabelInstanceName]._x = this.LabelxCoord;
this[LabelInstanceName]._y = this.LabelyCoord;
this[LabelInstanceName].text_lbl.text=this.LabelText;
trace("in method");
trace(this.ButtonDepthNumber);
}
Object.registerClass("TopNavButtonSymbol",TopNavButtonClass);
#endinitclip


(you can see there's only a few lines difference there) give it the linkage name in the library of TopNavButtonSymbol and attach it in your movie like this:


myInit = {ButtonName:"topnavnutton_pb",ButtonInstanceName:"button_name"};//and all your other arguments
attachMovie("TopNavButtonSymbol","nav_mc",1,myInit);

Groges
07-29-2003, 11:24 AM
Thanks Jaybee,

You just saved me a few hours of frustration!!!