改进我的 OOP 结构,并尝试使代码的显示尽可能直观,使用基本的 OOP 原则,这肯定有帮助,但我的手艺只到此为止。我希望改进我的实践:
package
{
import flash.display.*;
import flash.text.*;
public class Button extends Sprite
{
protected var _spr:Sprite = new Sprite();
protected var _tf :TextField = new TextField();
protected var c :Number;
protected var xx :Number;
protected var yy :Number;
protected var w :Number;
protected var h :Number;
protected var t :String;
protected var a :Number;
protected var l :Number;
protected var lC :Number;
function Button
(
_col:Number, //beginFill
_x:Number, _y:Number, _w:Number, _h:Number, //drawRect
_t:String = "", //TextField (optional)
_a:Number = 1, //beginFill (optional)
_l:Number = 0, _lC:Number = 0xFFFFFF //lineStyle (optional)
)
{
c = _col;
xx = _x;
yy = _y;
w = _w;
h = _h;
t = _t;
a = _a;
l = _l;
lC = _lC;
_spr.addChild(_tf);
draw();
}
public function draw ():void
{
_spr.graphics.clear ();
_spr.graphics.lineStyle(l, lC);
_spr.graphics.beginFill(c);
_spr.graphics.drawRect (xx, yy, w, h);
var f:TextFormat = new TextFormat;
f.font = "Arial";
_tf.text = t;
_tf.autoSize = TextFieldAutoSize.LEFT;
_tf.x = xx + w/2 - _tf.textWidth / 2;
_tf.y = yy + h/2 - _tf.textHeight / 1.5;
_tf.width = w - 2;
_tf.height = h - 2;
_tf.alpha = 0.75;
_tf.setTextFormat(f);
_tf.selectable = false;
_tf.mouseEnabled = false;
_spr.graphics.endFill ();
}
/* ----------------------- *
* GETTERS *
* ----------------------- */
//OVERRIDE
override public function get x () :Number {return (xx)}
override public function get y () :Number {return (yy)}
override public function get width () :Number {return (w)}
override public function get height () :Number {return (h)}
//SUPPLEMENTARY
public function get col () :Number {return (c)}
public function get text () :String {return (t)}
public function get line () :Number {return (l)}
public function get lineC () :Number {return (lC)}
public function get getObj () :Sprite {return (_spr)}
/* ----------------------- *
* SETTERS *
* ----------------------- */
//OVERRIDE
override public function set x (_n:Number) :void
{ xx = getObj.x = Math.round(_n - xx) }
override public function set y (_n:Number) :void
{ yy = getObj.y = Math.round(_n - yy) }
override public function set width (_n:Number) :void
{ w = Math.round(_n) }
override public function set height (_n:Number) :void
{ h = Math.round(_n) }
//SUPPLEMENTARY
public function set col (_n:Number) :void
{
c = _n;
draw();
}
public function set text (_s:String) :void
{
t = _s;
draw();
}
public function set line (_n:Number) :void
{
l = _n;
draw();
}
public function set lineC(_n:Number) :void
{
lC = _n;
draw();
}
}
}
从上面的 Button 类中,你可以对我的结构和组织做出什么解释?我已经阅读并关注了许多关于如何在逻辑上执行 OOP 的文章,我认为我已经准备好进行一些调整或批评,而不是我对 AS3 中的类应该如何理解的理解(我知道的情况。)
一些具体问题:
当我从主类实例化“按钮”时,我应该将它添加到那里的舞台还是使用 Super 的按钮类本身中?
“1047”错误会被修复吗?除了这里已经提到的之外,还有什么可持续(或更有效)的解决方法?: http: //curtismorley.com/2008/10/15/actionscript-error-1047-parameter-initializer-unknown-or-is-不是编译时间常数/
为了更新宽度/高度的变化,我必须在从 Override 函数之外专门针对宽度和高度进行修改后调用我的绘图函数。有没有通过 Override 函数调用 draw() 的正确方法?没有提供错误输出,但这样做似乎违反了规则,所以我只是从 Main 调用 draw() 作为 hack 修复。可能在按钮内编写一个函数,绑定到更改属性的事件?
如果有人费心解析这堵文字墙,感谢您的阅读,我感谢您提出的任何批评,无论是严厉的还是其他的 :)