Dynamically creating an input text field in ActionScript 3.0

by Peter deHaan on December 2, 2008

in TextField, migration

The following example shows how you can dynamically create an input TextField object in ActionScript 3.0 by setting the text field object’s type property to the TextFieldType.INPUT constant.

Full code after the jump.

// ActionScript 3.0
var theTextField:TextField = new TextField();
theTextField.type = TextFieldType.INPUT;
theTextField.border = true;
theTextField.x = 10;
theTextField.y = 10;
theTextField.multiline = true;
theTextField.wordWrap = true;
addChild(theTextField);

To create the same thing in ActionScript 2.0, you would do something like the following:

// ActionScript 2.0
createTextField("theTextField", 0, 10, 10, 100, 100);
theTextField.type = "input";
theTextField.border = true;
theTextField.multiline = true;
theTextField.wordWrap = true;

{ 7 comments… read them below or add one }

1 Andy Hadel 05.18.09 at 1:26 am

Okay so I’m cutting teeth on AS3 but I can’t get the textFormat var to work at all on the textField var. Just keeps ignoring it.

2 Thomas Davis 06.04.09 at 7:14 pm

Have you imported flash.text.TextField? Pretty sure it needs to happen, with or without packaging.

3 Peter deHaan 06.05.09 at 5:57 am
4 Jorgesys 07.02.09 at 1:48 pm

Andy you can define your textFormat like…

var mytextFormat:TextFormat = new TextFormat(“verdana”, 11,0xFF0000)
theTextField.defaultTextFormat = mytextFormat;

;)

5 Jorgesys 07.02.09 at 1:51 pm

Thomas, when you aren’t using action script file (.as) you don’t need to package or import the class flash.text.TextField
;)

6 Hassan 07.14.09 at 9:47 pm

Hi there Peter,

Im working on a Actionscript 3 project and i have come across some issues. Like u have shown in your post, it is quite easy to create a dynamic textField in AS3. My problem is that i want to create a class that creates a textfield (and a lot of other movieclips). So basically the class is a movieclip that creates a textfield and some movieclips. Ive actually written some AS3 code but it seems like i cant get Flash to show the result. Ive used AddChild etc to try to put the content onto the stage. Is there some basic issues im missing that are new to Flash?

Thanks for your time

Regards

7 Gerald 12.12.09 at 9:12 pm

Hi Peter,

I’m creating input text boxes in a for loop based on a number someone puts into a input text box on the Stage. This works great but I also need to store what is typed into the dynamically generated input text boxes. This is where I’m getting stuck so any insights would be great.

Gerald

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: