无法访问我的类的公共属性

时间:2012-10-18 14:07:44

标签: actionscript-3 class custom-component

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

我制作了自己的简单按钮类来做水平按钮列表。我工作得很好,但是......

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(将其显示为已点击)时,它返回undefined。我只能设置或阅读继承的礼仪,如名称,alpha等。我的概念错误是什么?

1 个答案:

答案 0 :(得分:0)

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

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

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