开发者

AS3 - Get button instance name and path

How to trace that "section1.btnback" was clicked?

section1.btnback.addEventListener(MouseEvent.CLICK, getBack)

    function getBack(event:MouseEvent):void
    {
    // trace: "secti开发者_运维百科on1.btnback"
    }

Thanks. Uli


I'm not sure what actualy you are asking for. If my suggestion is not the right for you, please excuse me. If you write

 trace(event.target)

you will see the complete name of the instance of the button.


I am guessing that you want it to say something like: "Button was clicked!" to console each time the button is pressed?

trace("Section1.btnback was clicked!");

I believe. (Wasn't really sure what you were asking for)

-Expanded in comments below-


If you want the "complete path" like meddlingwithfire suggested in his comment on 3lionz's answer, then you can use a class I created that does it for you. The class is called DOPath(short for display object path). The following is an example of it being used to get the complete path of a display object:

Main.as(document class):

package 
{
    import com.example.display.DOPath;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {   
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var container1:Sprite = new Sprite();
            container1.name = "container1";

            var container2:Sprite = new Sprite();
            container2.name = "container2";

            var square:Square = new Square();
            square.name = "square";
            square.addEventListener(MouseEvent.CLICK, onSquareClick);

            addChild(container1);
            container1.addChild(container2);
            container2.addChild(square);

        }// end function

        private function onSquareClick(e:MouseEvent):void
        {
            var square:Square = e.target as Square;

            var doPath:DOPath = new DOPath(square);
            trace(doPath); // output: stage.root1.container1.container2.square

        }// end function

    }// end package

}// end package

import flash.display.Sprite;

internal class Square extends Sprite
{
    public function Square()
    {
        graphics.beginFill(0xFF0000);
        graphics.drawRect(0, 0, 100, 100);
        graphics.endFill();

    }// end function

}// end class

DOPath.as:

package com.example.display 
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Stage;

    public class DOPath
    {
        private var _parents:Vector.<DisplayObjectContainer>;
        private var _d:DisplayObject;

        public function DOPath(d:DisplayObject):void
        {
            _d = d;

            init();

        }// end function

        private function init():void
        {
            _parents = new Vector.<DisplayObjectContainer>;

            pushParent(_d);

        }// end function

        private function pushParent(d:DisplayObject):void
        {
            if(d.parent) 
            {
                _parents.push(d.parent);
                pushParent(d.parent);

            }// end if

        }// end function

        public function toString():String
        {
            var path:String = _d.name;

            for (var i:uint = 0; i < _parents.length; i++)
            {
                var name:String = (_parents[i] is Stage) ? "stage" : _parents[i].name;
                path = name + "." + path;

            }// end for

            return path;

        }// end function

    }// end class

}// end package


You can use a quite simple recursive function like this one:

private function getDOname(dob : DisplayObject) : String
{
    var result : String = dob.name;

    if (dob is UIComponent)
    {
        result = UIComponent(dob).id;
    }

    if(dob.parent && dob.parent is DisplayObjectContainer)
    {
        result = getDOname(dob.parent) + "." + result;
    }

    return result;
}

You can pass event.target to the function and acquire the button that was clicked. If you are using these techniques for debug purposes, you can simply add and event listener for click event to the stage or the SandboxRoot itself, if you are unsure which visual element you are actually clicking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜