0

我还在学习面向对象的编程......

我制作了自己的简单按钮类来执行水平按钮列表。我工作正常,但是...

package as3Classes {

import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;
import flash.text.TextFormat;

public class simpleButton extends MovieClip {

    //var nome:String = new String();
    var sub:Sprite = new Sprite();
    public var realce:Sprite = new Sprite();
    public var titulo:String;

    public function simpleButton(nomeId:String, texto:String) {
        this.name = nomeId;
        this.titulo = texto;
        this.useHandCursor = true;
        this.buttonMode = true;
        this.mouseChildren = false;
        var txtf:TextFormat = new TextFormat();
        txtf.font = "Arial";
        var txt:TextField = new TextField();
        txt.wordWrap = false;
        txt.multiline = false;
        txt.text = texto;
        txt.setTextFormat(txtf);
        txt.width = txt.textWidth+4;
        txt.height = 22;
        txt.x = 4;
        txt.y = 2;
        txt.cacheAsBitmap = true;
        var fundo:Sprite = new Sprite();
        fundo.graphics.beginFill(0xCCCCCC);
        fundo.graphics.drawRect(0, 0, txt.width+8, 22);
        fundo.graphics.endFill();
        //var realce:Sprite = new Sprite();
        this.realce.graphics.beginFill(0x00CC00);
        this.realce.graphics.drawRect(0, 0, txt.width+8, 22);
        this.realce.graphics.endFill();
        this.sub.graphics.beginFill(0xFF0000);
        this.sub.graphics.drawRect(0, 0, txt.width+8, 2);
        this.sub.graphics.endFill();
        this.addChild(fundo);
        this.addChild(this.realce);
        this.addChild(this.sub);
        this.addChild(txt);
        this.sub.alpha = 0;
        this.sub.y = 22;
        this.addEventListener(MouseEvent.MOUSE_OVER, mouseover);
        this.addEventListener(MouseEvent.MOUSE_OUT, mouseout);
    }

    private function mouseover(e:MouseEvent){
        sub.alpha = 1;
    }
    private function mouseout(e:MouseEvent){
        sub.alpha = 0;
    }

}

}

...当我尝试访问“titulo”或将“realce”设置为 alpha=1(以将其显示为单击时)它返回未定义。我只能设置或读取继承的属性,如名称、字母等。我的概念错误是什么?

4

1 回答 1

0

是的!我找到了避免这个问题的方法:因为(因为它接缝)我无法通过显示对象列表访问“simpleButton”的内部公共对象,所以我在主类的根目录中创建了一个私有对象(btns:Object) (随处可见)并在添加时同时添加 simpleButtons。名称相同,所以我可以通过 btns 更改 realce.alpha,它会反映在显示中。

这很奇怪,因为 e.target 指向孩子而不是对象本身!

如果有人知道更好的方法,请告诉我。

于 2012-10-19T18:52:28.050 回答