开发者

AS3 checking if click was on stage or an a movieclip

How can i check if a click was on the stage (not on a other object in front of the stage - just on a place where no other object is) or on a movieclip (name "mcSlideHolder开发者_C百科")?

function onMouse_Down(e:MouseEvent):void
{
    trace("MouseDown occured");
    trace("MouseDown target: " + e.target.name);
    trace("MouseDown target.tostring: " + e.target.toString);
    trace("MouseDown currentTarget: " + e.currentTarget);
    if(e.target.name == null || e.target.name == "mcSlideHolder")
    {   
        this._userInput();
    }
}

My Problem is, that e.target.name is null, when clicking on the stage. When clicking on the mcSlideHolder e.target.name is "instance235". How would you do this?

Thanks for any advice!


Normally, when you want to detect a click on a specific MovieClip, that is the one you add the listener to:

mcSlideHolder.addEventListener(MouseEvent.MOUSE_DOWN, onSlideHolderMouseDown);

rather than

stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouse_Down);

It sounds like you're doing the latter; adding a listener to the stage, and trying to figure out in the listener function what was clicked on. There is nothing wrong with this, and it can even be efficient if there are many possible things to click on. But in this case, the currentTarget is always going to be the stage, and the target is always going to be the thing you clicked on that generated the event. If that is indeed how you have things set up, you will get a different result--possibly the one you expected--if you do this:

mcSlideHolder.mouseChildren = false;

I'm assuming mcSlideHolder is the instance name. Setting mouseChildren to false will mean that mcSlideHolder will generate the MOUSE_DOWN event rather than instance235 that is its child.


Adam smith's answer already explains why you get "instance235" so I'll explain a work around for stage object's name property equaling null.

You could create a wrapper class for the stage object and give that class a property called name. The wrapper class would also have to be a subclass of EventDispatcher so that you can override it's addEventListener() method and alter its functionality so that you are implicitly adding an event listener to the stage object and also to the wrapper object. The best way to explain it is to show you an example I made:

package 
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Main extends Sprite 
    {
        private var _stageWrapper:StageWrapper;

        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 stageWrapper:StageWrapper = new StageWrapper(stage, "stage");
            stageWrapper.addEventListener(MouseEvent.CLICK, onStageWrapperClick);

        }// end function

        private function onStageWrapperClick(e:MouseEvent):void
        {
            trace(e.target.name) // output: stage

        }// end function

    }// end class

}// end package

import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;

internal class StageWrapper extends EventDispatcher
{
    private var _stage:Stage;
    private var _name:String;
    private var _listener:Function;

    public function get stage():Stage { return _stage }
    public function get name():String { return _name }

    public function StageWrapper(stage:Stage, name:String) 
    { 
        _stage = stage;
        _name = name;

    }// end function

    override public function addEventListener(type:String, 
                                              listener:Function,
                                              useCapture:Boolean = false,
                                              priority:int = 0,
                                              useWeakReference:Boolean = false):void
    {

        super.addEventListener(type, listener, useCapture, priority, useWeakReference);
        _stage.addEventListener(type, onStageEvent, useCapture, priority, useWeakReference);

    }// end function

    private function onStageEvent(e:Event):void 
    {
        dispatchEvent(e);

    }// end function

}// end class

Personally I wouldn't go with this approach but this is the best answer I can give you for the direction you seem to be going with.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜