【问题标题】:Can't access public properties of my Class无法访问我的班级的公共属性
【发布时间】:2012-10-18 14:07:44
【问题描述】:

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

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

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

【问题讨论】:

  • 请显示您尝试访问公共属性的代码。
  • 您是否在尝试访问这些属性的类中导入“as3Classes.*”?我认为我不需要提及这一点,但您也需要实例化该类。您无法通过 simpleButton.titulo 访问这些属性。
  • 事后我注意到你在这堂课上做错了两件事。所有类名都应该大写。包名、变量名和函数名应遵循传统的驼峰式命名,但类名应始终大写,然后遵循驼峰式。您还应该避免在全局空间中实例化对象 (sub = new Sprite())。对于公共/私有/受保护的变量,请在构造函数中实例化它们。
  • 我正在创建一个新实例 (var btn:simpleButton = new simpleButton(categ.SubcategoryID, categ.SubcategoryName);) addChild 和一个监听器。比,在事件处理程序中,e.target.name 有效,但 e.target.titulo 无效,e.target.realce.alpha 也无效。
  • 从概念上讲没有错误。尝试在构造函数中跟踪 nomeId 和 texto 以确保这些值不为空。

标签: actionscript-3 class custom-component


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多