DisplayObject not being displayed in AS3

Posted by MarkSteve on Stack Overflow See other posts from Stack Overflow or by MarkSteve
Published on 2010-05-19T12:40:50Z Indexed on 2010/05/19 18:00 UTC
Read the original article Hit count: 169

Filed under:

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?

© Stack Overflow or respective owner

Related posts about actionscript-3