Reputation: 1
I need some help. In Flash, i have designed a sinmple button from the components panel. I have given it the instance name of btn_one. What i am attempting to do is everytime users click on the button i want the button to change text from send to next. But, how do you do this?
Unfortunately my knowledge of as3 is limited. They are many tutorials of as3 button but i have not found any in regards to this. I would really appreciate it if you guys could show me some examples of code or provide me some tutorials links.
Thanks
Upvotes: 0
Views: 223
Reputation: 18757
I've tried to do something about SimpleButtons' text fields, and came up with the following code. Hope it'll help.
public class GenericTextButton extends flash.display.SimpleButton {
public var buttonText:String;
private var tfs:Vector.<TextField>;
public function GenericTextButton():void
{
tfs=new Vector.<TextField>();
var tc:DisplayObjectContainer;
var i:int;
// now let's init embedded TFs
// trace((upState as DisplayObjectContainer).numChildren); // wow there's property available! YES
if (upState is DisplayObjectContainer) {
tc=(upState as DisplayObjectContainer);
for (i=0;i<tc.numChildren;i++)
if (tc.getChildAt(i) is TextField) tfs.push(tc.getChildAt(i) as TextField);
} // now same with overState and downState
if (overState is DisplayObjectContainer) {
tc=(overState as DisplayObjectContainer);
for (i=0;i<tc.numChildren;i++)
if (tc.getChildAt(i) is TextField) tfs.push(tc.getChildAt(i) as TextField);
}
if (downState is DisplayObjectContainer) {
tc=(downState as DisplayObjectContainer);
for (i=0;i<tc.numChildren;i++)
if (tc.getChildAt(i) is TextField) tfs.push(tc.getChildAt(i) as TextField);
}
// trace(name+' has '+tfs.length+' textfields'); // 3 !!!! Unbelieveable, but true
}
public function setText(the_text:String):void {
for (var i:int=0;i<tfs.length;i++) tfs[i].text=the_text; // and NOW we can do as simple as this
}
}
What does it do: You declare your button instance as GenericTextButton descendant, instead of SimpleButton, then you'll have setText()
method available to instantly change all text in that button to whatever you supply. It enumerates internal structure of SimpleButton, grabs any TextFields there are, and stores it in a Vector for easy reference.
Upvotes: 1