0
votes

I cannot figured a simple problem with handling dynamic text field in flash. I do not have brain cells anymore and im giving up..

var g = ['text dang', 'text deng', 'dong', 'laah', 'maah'];

for(var q = 0; q < g.length; q++){
   var p = new grad_plate();
   p.name_field.multiline = false;
   p.name_field.wordWrap = false;
   p.name_field.autoSize = TextFieldAutoSize.LEFT;
   p.name_field.text = g[q];
   p.name_field.width = p.name_field.textWidth;
   p.plate_mc.width = p.name_field.width + 20;
   p.width = p.plate_mc.width;
   p.name = "name_" + q;
   stage.addChild(p);
   //p.x =  q * stage.getChildAt(q).width;
   //p.x = q * p.width;
   p.x =  q * 200;
}

my grad_plate() class contains name_field textfield and plate_mc for the background.

I want to dynamically set the x of each of the instance depending instance`s width.

giving 200 (fix) is working but I want to dynamically get the p.width which gives me an ugly horizontal align.

1
I am thinking that maybe your layouting code will work better in the next frame after the things are added to the stage(maybe then the textWidth or whatever thing you want to messure is properly computed, this is just a guess I hope it helps, what is mean is to call the layout /setup code using something like settimeOut after the things are added to the stage) - simion314

1 Answers

1
votes

Working example for you, I think some parts, will be helpful.

package {

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.text.TextFormat;

    public class StackOverflow extends Sprite {
        public function StackOverflow() {
            addEventListener(Event.ADDED_TO_STAGE, onAdded);
        }

        private function onAdded(e:Event):void {
            removeEventListener(Event.ADDED_TO_STAGE, onAdded);

            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;

            setup();
        }

        private function setup():void {
            var texts:Array = ["some", "text", "here", "dynamic"];
            var i:uint, len:uint = texts.length, posX:uint, plate:Plate;
            var textFormat: TextFormat = new TextFormat("Arial", 18, 0xFFFFFF, true);

            for (i; i < len; ++i) {
                plate = new Plate(texts[i], textFormat, Math.random() * 0xFFFFFF);
                addChild(plate);
                plate.x = posX;
                posX += plate.width;
            }
        }
    }
}

import flash.display.Graphics;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

internal class Plate extends Sprite {

    public function Plate(text:String, textFormat:TextFormat, color:uint) {
        const padding:int = 20;
        var textField:TextField = new TextField();
        var g:Graphics = this.graphics;
        textField.autoSize = TextFieldAutoSize.LEFT;
        textField.defaultTextFormat = textFormat;
        textField.multiline = false;
        textField.wordWrap = false;
        textField.text = text;

        addChild(textField);

        textField.x = textField.y = padding;
        g.beginFill(color);
        g.drawRect(0, 0, 2 * padding + textField.width, 2 * padding + textField.height);
    }
}

Result, will look something like this:

Result