Jeremy Bural
Jeremy Bural

Reputation: 7

AS3 multiple textfields made easy

I am working on a Results page for my game as well as upgrade page and looking for an easy way to do many textfields. I have a format for my text that takes care of font, colour, and size, but looking for an easy way to do the width and height of textfields to increase all at the same time.

I have been informed about a "with" keyword that may work but do not understand how to implement this within my program and essentially want to shorten my results class if possible.

Thank you,

Upvotes: 0

Views: 1551

Answers (2)

loxxy
loxxy

Reputation: 13151

The best way would be to create a custom function for generating textfield.

The example can be found in the livedocs itself.

So something like the following should suffice :

private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {

        var result:TextField = new TextField();

        result.x = x; 

        result.y = y;

        result.width = width; 

        result.height = height;

        return result;
    }

You may also set a default value to each attribute in the function.

private function createCustomTextField ( x:Number= <Default Value>,  ...

Use it to add a textfield inside the container form.

var container:Sprite = new Sprite();  // New form container

container.addChild(createCustomTextField (20,20,50,50)); // Text Filed 1

container.addChild(createCustomTextField (20,50,50,50)); // Text Filed 2

addChild(container);  // Add to current class

You may want to modify the function to accept a name so that each variable can be accessed later.

Upvotes: 1

meddlingwithfire
meddlingwithfire

Reputation: 1437

As far as I am aware, you can't use a "with" keyword to target multiple objects. Here's the documentation for it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#with

What I've done in the past is just make an array of all the targets, and then write a loop to apply properties to each:

var textFormat:TextFormat = generateMyCustomTextFormat();
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
//...
var textField3:TextField = new TextField();
var targets:Array = [textField1, textField2, textField3];
for(var i:int=0; i<targets.length; i++)
{
    targets[i].defaultTextFormat = textFormat;
    targets[i].width = 250;
    //...
}

Upvotes: 0

Related Questions