开发者

DisplayObject not being displayed in AS3

I have this class:

public class IskwabolText extends Sprite {

    private var _tf:TextField;
    private var _tfmt:TextFormat;

    private var _size:Number;
    private var _text:String;

    public function IskwabolText(params:Object) {
        var defaultParams:Object = {
            color: 0x000000,
            background: false,
            backgroundColor: 0xFFFFFF,
            width: 0,
            height: 0,
            multiline: false,
            wordWrap: false
        };
        // textfield
        _tf = new TextField();
        _tf.antiAliasType = 'advanced';
        _tf.embedFonts = true;
        _tf.type = 'dynamic';
        _tf.selectable = false;
        // textformat
        _tfmt = new TextFormat();
        set(defaultParams);
        set(params);
    }

    public function get(param:String):Object {
        switch (param) {
            case 'size': return _tfmt.size;
            case 'text': return _tf.text;
            case 'font': return _tfmt.font;
            case 'color': return _tfmt.color;
            case 'background': return _tf.background;
            case 'backgroundColor': return _tf.backgroundColor;
            case 'width': return _tf.width;
            case 开发者_运维问答'height': return _tf.height;
            case 'multiline': return _tf.multiline;
            case 'wordWrap': return _tf.multiline;
            default: return this[param];
        }
        return null;
    }

    public function set(params:Object):Object {
        for (var i:String in params) {
            setParam(i, params[i]);
        }
        redraw();
        return this;
    }

    private function setParam(param:String, value:Object):Object {
        switch (param) {
            case 'size': _tfmt.size = new String(value); break;
            case 'text': _tf.text = new String(value); break;
            case 'font': _tfmt.font = new String(value); break;
            case 'color': _tfmt.color = new uint(value); break;
            case 'background': _tf.background = new Boolean(value); break;
            case 'backgroundColor': _tf.backgroundColor = new uint(value); break;
            case 'width': _tf.width = new Number(value); break;
            case 'height': _tf.height = new Number(value); break;
            case 'multiline': _tf.multiline = new Boolean(value); break;
            case 'wordWrap': _tf.multiline = new Boolean(value); break;
            default: this[param] = value; break;
        }
        return this;
    }

    private function redraw():void {
        _tf.setTextFormat(_tfmt);
        if (contains(_tf))
            removeChild(_tf);
        if (_tf.width == 0)
            _tf.width= _tf.textWidth+5;
        _tf.height = _tf.textHeight;
        addChild(_tf);
    }

}

But when I do this:

public class Main extends Sprite {
   public function Main() {
      addChild(new IskwabolText({
         size: 100,
         text: 'iskwabol',
         font: 'Default', // this is properly embedded
         color: 0x000000,
         x: stage.stageWidth / 2 - this.width / 2,
         y: 140
      }));
   }
}

The child IskwabolText doesn't get displayed. What happening?


You have embedFonts set to TRUE, but I don't see where/how you are setting the font type to your TextFormat - set embedFonts to FALSE and you'll see you text added to your display list.

UPDATE: I overlooked you do have the ffont property set to the embedded font name, just be sure that you are referring to it correctly as the problem it is most likely to be due to the embedded font.


The text is set in the constructor. I just figured out what was going wrong. Apparently the width and height were getting a value of 5 for some reason. I fixed the redraw function to adjust the width and height correctly. Seems like chchrist's comment was the write answer :) Thanks for the help guys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜