简体   繁体   中英

Can't access public properties of my Class

I'm still learning about object oriented programming...

I made my own simple button class to do horizontal buttons lists. I works fine, but...

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;
    }

}

}

... when I try to access the "titulo" or set "realce" as alpha=1 (to display it as clicked) it returns undefined. I can only set or read proprieties inherited, as the name, alpha, etc. What is my conceptual mistake?

Yes! I found a way to avoid this issue: Since (as it seams) I can't access the internal public objects of "simpleButton" by display objects list, I create a private object (btns:Object) in the root of the main class (can be seen everywhere) and add the simpleButtons simultaneously as adding at display. The name is the same, so I can change the realce.alpha by btns and it will reflect in the display.

This is bizarre, because the e.target leads to the child instead of the object itself!

If somebody knows a better way, please let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM