First look inside the .fla and then inside the mc_button_primary and notice there is a little code inside there. Won’t work like you want to without it. You can add it to each instance via the myMethod() but it’s meant to be a button so let’s make this as simple as we can and put it here.
stop();
this.buttonMode = true;
this.mouseChildren = false;
StarterClass.as
Here is where something actually happens follow the comments.
package {
// here is the Adobe provided code that you need to import to make this happen
// get used to it. You'll be doing this alot.
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.*;
// import our fancy button from the library
import mc_button_primary;
public class StarterClass extends MovieClip {
// here's where our new button is born
private var buttonPrimary:mc_button_primary;
// this is the constructor, it is the function/method that is automatically called
function StarterClass() {
trace(“Starter class…..√ check”);
// call myMethod and create a button
myMethod();
}
function myMethod() {
trace(” myMethod……….√ check”);
trace(” StarterClass called myMethod()”);
trace(” myMethod() creates a button”);
// create a new button from the exported library item
buttonPrimary = new mc_button_primary();
// name the new button
buttonPrimary.name = “Linus”;
// put Linus on the stage
addChild( buttonPrimary );
// create a handle to reference Linus with
var bttn_handle = getChildByName( buttonPrimary.name );
// Put the name in the text field then center Linus on the stage
bttn_handle.button_Text.text = buttonPrimary.name;
bttn_handle.x = ( stage.stageWidth - bttn_handle.width ) / 2;
bttn_handle.y = ( stage.stageHeight - bttn_handle.height ) / 2;
// add an event listener to Linus so we know when he was clicked
bttn_handle.addEventListener( MouseEvent.CLICK, myButton_CLICK );
}
// here is the listener
function myButton_CLICK(e:MouseEvent):void {
trace(” myButton_CLICK is called when the button is clicked”);
}
}
}
StarterClass_wButton.zip
Actionscript 3 add a button to the starter class
Apr, 13 -- Categories: Actionscript