// Now how to create a button. Because we will have multiple of these, create a function to generate buttons:
function createDummyButton(target_mc:MovieClip, name:String, text:String, x:Number, y:Number, width:Number, height:Number, key:String):Void
{
// 1. create an object holding a button structures
target_mc.createEmptyMovieClip(name, target_mc.getNextHighestDepth());
target_mc[name]._x = x; // set coordinates; indexing object by the name in function parameter
target_mc[name]._y = y;
// 2. create a clickable rectangle, otherwise button would work only when clicked text exactly, not a hole in a "O" for example.
// Make is a subobject so it's visibility can be controlled independently from text.
target_mc[name].createEmptyMovieClip("pad", 1); // using layer 1 (deepest for pad)
// draw an invisible (alpha 0) white rectangle in that object
with (target_mc[name].pad)
{
beginFill(0xffffff, 100); // white fill
lineTo(width,0);
lineTo(width,height);
lineTo(0,height);
lineTo(0,0);
endFill();
_alpha = 0; // invisible by default
}
// 3. create a button text object named "label"
target_mc[name].createTextField("label", 2, 0, 0, width, height); // using layer 2 (just above pad pad)
target_mc[name].label.text = text; // set actual text
// set a text format. Need a special object holding that data for TextField
var textformatdata:TextFormat = new TextFormat();
textformatdata.size = 18;
textformatdata.bold = true;
textformatdata.font = "Arial Black"; // System fonts are used, not fonts packed in SWF file.
textformatdata.color = 0xffffff; // white RGB code
textformatdata.leftMargin = 2;
// apply format on our label using it's setTextFormat() method
target_mc[name].label.setTextFormat(textformatdata);
// 4. make button pad rectangle visible when mouse is over
target_mc[name].onRollOver = function()
{
this.pad._alpha = 30; // make it 70% transparent
};
target_mc[name].onRollOut = function()
{
this.pad._alpha = 0; // make it invisible when mouse is out
};
// 5. create helper methods to change button text color
target_mc[name].turnGreen = function()
{
var textformatdata:TextFormat = new TextFormat();
textformatdata.color = 0x00ff00; // green
this.label.setTextFormat(textformatdata); // apply to button text text
};
target_mc[name].turnWhite = function()
{
var textformatdata:TextFormat = new TextFormat();
textformatdata.color = 0xffffff; // white
this.label.setTextFormat(textformatdata);
};
// 6. make the button selectable by Tab key & Enter
target_mc[name].focusEnabled = true;
target_mc[name].tabEnabled = true;
// 7. if key parameter has proper length (one character), make it keyboard active
if(key.length == 1)
{
target_mc[name].keycode = key.charCodeAt(0); // Store key parameter first (and the only) character Ascii code
target_mc[name].onKeyDown = function() // create a function for processing keyboard events
{
if(Key.getAscii() == this.keycode) // pressed keyboard code match button stored key code
{
this.onRelease(); // on matching key hit call the same function as for mouse click, which is not defined yet.
}
};
// register this button object among system keyboard listener object (Key is static object in Flash player)
Key.addListener(target_mc[name]);
}
// 5. leave on press/release script for later as it is button specific
}
// Now use that function for the first two buttons, for speed 1 at coordinates 80,5 (just after "SPEED" text)
createDummyButton(controller,"button1","1",80,5,20,25,"1");
controller.button1.onRelease = function() // define what that button is suposed to do:
{
this._parent._parent.focus.camera.area.char.kendra.gotoAndPlay("a"); // scene control call
this.turnGreen(); // turn button green
this._parent.button2.turnWhite(); // turn the other button (not defined yet) white
};
// The second button on coordinates just 20 pcx right of the first button
createDummyButton(controller,"button2","2",100,5,20,25,"2");
controller.button2.onRelease = function()
{
this._parent._parent.focus.camera.area.char.kendra.gotoAndPlay("b");
this.turnGreen();
this._parent.button1.turnWhite(); // turn the another button white
};
//Now we can copy-paste-modify to create as many buttons as we want easily. No need to create any flash structure (sprites etc), all is created by this script from nothing.
Note on code 1:
Unlike in C++, method (object associated function) in Flash AS2 doesn't have focus of the object which it belongs to but a random focus of a script in which the function body is defined. So it is not really a method, but a generic function with unlisted "this" pointer to the object which it is declared part of. So you have to use "this" pointer to access any data declared outside the function.
Note on code 2:
There is a bug in Flash player that ruins "this" pointer inside "with(){}" statement. So never use "this" pointer in "with" statement. It also ruins "this" pointer in every method/function called inside "with" statement. So use "with" statement cautiously and only for simple things.
Note on code 3:
In AS2/Flash we can use variables and call object methods not defined yet (neither declared as virtual functions) because in Flash (and also Java and similar languages) these things are not compiled into memory offsets but all data are searched by names during code execution. So entire Flash is a tree of associative arrays where data are searched by names in hash tables or something. I'm not a programmer, just average guy, so I find it freak and computationally inefficient.
Note on code 4:
Types in AS2 are most likely used only for some formal check by Adobe tool compiler. Types are thrown away in compilation. FFDec can't even compile code with types declared. Flash player simply ignores type, so it asserts string or pointer to a variable that was a originally number type. Variable type is changed by value assertion. So if you add two variables, the result may be a number or a string depending what was loaded to variables last time. It's good idea to use AS2 special functions line "isNaN()" to verify that input to math operation is actually a number.