Actionscript 3 Can';无法访问我的类的公共属性

Actionscript 3 Can';无法访问我的类的公共属性,actionscript-3,class,custom-component,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; impor

我还在学习面向对象编程

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

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(以单击方式显示)时,它返回未定义。我只能设置或读取继承的属性,如名称、字母等。我的概念错误是什么?

是的!我找到了一种避免这个问题的方法:因为(因为它)我无法通过显示对象列表访问“simpleButton”的内部公共对象,所以我在主类的根中创建了一个私有对象(btns:object)(可以在任何地方看到),并在显示时添加simpleButton。名称相同,因此我可以通过BTN更改realce.alpha,它将反映在显示屏上

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


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

请显示您尝试访问公共属性的代码。您是否在尝试访问这些属性的类中导入“as3class.*”?我不认为我需要提及这一点,但是您也需要实例化这个类。您不能通过simpleButton.titulo访问这些属性。事后,我注意到您在这个类中做了两件不正确的事情。所有类名都应大写。包、变量和函数名称应遵循传统的camelcase,但类名应始终大写,然后遵循camelcase。您还应该避免在全局空间中实例化对象(
sub=new Sprite()
)。对于public/private/protected变量,在构造函数中实例化它们。我正在创建一个新实例(var btn:simpleButton=newsimplebutton(categ.subcategorid,categ.subcategoriname);)addChild和一个侦听器。然后,在事件处理程序中,e.target.name起作用,但e.target.titulo不起作用,e.target.realce.alpha也不起作用。尝试在构造函数中跟踪nomeId和texto,以确保这些值不是空的。