我有這個類:的DisplayObject不會顯示在AS3
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);
}
}
但是,當我這樣做:
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
}));
}
}
孩子IskwabolText沒有得到顯示。發生了什麼?
再次檢查您的重畫功能。可能有什麼不對 – chchrist 2010-05-19 12:46:05
我首先使用getters和setter編寫了IskwabolText類,但它變得冗長了。然後我使用了一個使用param對象來設置類屬性的集合函數。我沒有觸及重繪功能,所以我不認爲這是原因。 – MarkSteve 2010-05-19 13:05:57