as3 textfield bug还是什么?

时间:2014-03-04 15:37:33

标签: actionscript-3 flash actionscript flash-cs5

我无法想象在flash中处理动态文本字段的简单问题。我不再有脑细胞了,我放弃了......

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;
}

我的grad_plate()类包含name_field textfield和plate_mc作为背景。

我想根据实例的宽度动态设置每个实例的x。

给200(修复)工作,但我想动态获取p.width,这给我一个丑陋的水平对齐。

1 个答案:

答案 0 :(得分:1)

为你工作的例子,我认为有些部分会有所帮助。

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