Here’s a simple little example of creating actionscript 3 text fields dynamically. It is also a great example of using functions to do all the dirty work for you.
In order for this to work you need to put a button component in the library.
This sample will create an input text field instance and a button instance. When the user click on the button, it will create a new text field and set its x,y to random.
Here is the code:
import fl.controls.Button;
import flash.text.TextField;
import flash.text.TextFieldType;
//create a button
var myButton:Button = new Button();
myButton.label = "set text random x,y";
myButton.emphasized = true;
myButton.width = 150;
myButton.move(50, 100);
addChild(myButton);
var tf1:TextField = createTextField(10, 10, 400, 22, true);
tf1.type = TextFieldType.INPUT;
//create an input text field function
function createTextField(x:Number, y:Number, width:Number, height:Number, border:Boolean):TextField {
var result:TextField = new TextField();
result.x = x;
result.y = y;
result.width = width;
result.height = height;
result.background = true;
result.border = border;
addChild(result);
return result;
}
//event that listens for the button to be clicked
myButton.addEventListener(MouseEvent.CLICK,setRandomXY);
//function that creates the new text field and sets the text
function setRandomXY(e:Event){
var randomX = Math.random()*stage.width;
var randomY = Math.random()*stage.height;
var tf2:TextField = createTextField(randomX,randomY,50,20,false)
tf2.type = TextFieldType.DYNAMIC;
tf2.text = tf1.text;
}
Related Posts:
Posted in: Flash Helps | Comments Off