如何在actionscript 3.0中捕获鼠标单击

时间:2009-04-02 17:37:56

标签: actionscript-3

如何使用Actionscript 3.0捕获Flash窗口中用户的鼠标点击位置?

4 个答案:

答案 0 :(得分:3)

Ron DeVera很接近,但我不会使用内联函数,传递给函数的对象不是Event,而是MouseEvent。

stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);

function _onStageMouseDown(e:MouseEvent):void
{
    trace(e);
}

//traces
//[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=96 localY=96 stageX=96 stageY=96 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]

上面输出中的所有属性都可以通过传递给事件侦听器方法的对象_onStageMouseDown(e:MouseEvent)获得;因此以下

function _onStageMouseDown(e:MouseEvent):void
{
    trace(e.localX);
    trace(e.stageX);
    //Note that the above two traces are identical as we are listening to the stage for our MouseEvent.
}

答案 1 :(得分:1)

他们解释得很好,但是这里有完整的代码可以为你说明一点:

addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler(event: MouseEvent) : void
{
    // these are the x and y relative to the object
    var localMouseX: Number = event.localX;
    var localMouseY: Number = event.localY;

    // these are the x and y relative to the whole stage
    var stageMouseX: Number = event.stageX;
    var stageMouseY: Number = event.stageY;
}

答案 2 :(得分:0)

位置由什么上下文定义?整个页面?一个或多个特定的可点击控件?

答案 3 :(得分:0)

您可以随时查询任何DisplayObject的mouseX和mouseY。