AS3 localToGlobal返回看似随机的x值

时间:2015-05-18 17:22:59

标签: actionscript-3 flash

我有一个带滚动条的主时间轴。子时间轴包含一个带按钮的长字符串。我使用localToGlobal来确定按钮在舞台上的位置,这样我就可以在按钮的左侧或右侧弹出相关信息,具体取决于弹出窗口可能落在舞台上的位置。一切似乎都工作,直到滚动条移动时间轴太多,然后跟踪的x值似乎返回随机值。 关于如何获得x值的一致结果的任何想法,只显示我在舞台上点击的位置,这样我可以告诉弹出窗口是否向右移动,如果它太靠近左边缘,如果它太靠近右边缘则离开?我有结构布局,它只是主舞台上的x值似乎很不稳定。

以下是时间轴上的AS。

stop();

import flash.events.MouseEvent;
import flash.geom.Point;

//attach listeners to all your buttons
btn19980930.addEventListener(MouseEvent.CLICK, showPopup);
btn20110928.addEventListener(MouseEvent.CLICK, showPopup);
btn20111214.addEventListener(MouseEvent.CLICK, showPopup);
btn20120110.addEventListener(MouseEvent.CLICK, showPopup);

//define space in the timeline movie clip
var leftEdge:Number=360;


function showPopup(evt:MouseEvent){
    //figure out which button got clicked
    var buttonClicked = evt.currentTarget;

    //first get the local position of the button
    var localPoint:Point = new Point(buttonClicked.x, buttonClicked.y);


    //then calculate the buttons position in the global space
    var buttonGlobalPoint:Point = buttonClicked.localToGlobal(localPoint);


    trace(localPoint);
    trace(buttonGlobalPoint);


    if (buttonGlobalPoint.x < leftEdge){
        gotoAndStop(buttonClicked.name + "_right");
    } else { 
        gotoAndStop(buttonClicked.name + "_left");
    }

}

3 个答案:

答案 0 :(得分:0)

您应该可以使用stageXstageY获取鼠标事件的全局坐标:

var stageMousePoint:Point = new Point(evt.stageX, evt.stageY);

答案 1 :(得分:0)

最终有效的一件事,虽然它很麻烦且过于庞大,但是为每个按钮复制脚本部分,以便能够为每个按钮提供不同的“leftEdge”参数,即使按钮的大小和锚定方式不同,他们对自定义数字做出反应。我知道可能有一种方法来压缩剧本,但我正在向前推进我所知道的。

   // button 01
   //attach listeners to all your buttons
btn19980930.addEventListener(MouseEvent.CLICK, showPopup);

var leftEdge:Number=200;

function showPopup(evt:MouseEvent){
    //figure out which button got clicked
    //figure out which button got clicked
    var buttonClicked = evt.currentTarget;


    //first get the local position of the button
    var stageMousePoint:Point = new Point(buttonClicked.stageX, buttonClicked.stageY);

    //then calculate the buttons position in the global space
    var buttonGlobalPoint:Point = buttonClicked.localToGlobal(localPoint);

    //trace(localPoint);
    trace(buttonGlobalPoint);

    if (stageMousePoint.x < leftEdge){
        gotoAndStop(buttonClicked.name + "_right");
    } else { 
        gotoAndStop(buttonClicked.name + "_left");
    }

}

   // button 02
    //attach listeners to all your buttons
    btn19990224.addEventListener(MouseEvent.CLICK, showPopup);

    var leftEdge2:Number=215;

    function showPopup2(evt:MouseEvent){
        //figure out which button got clicked
        var buttonClicked2 = evt.currentTarget;

        //first get the local position of the button
        var localPoint2:Point = new Point(buttonClicked2.x, buttonClicked2.y);

        //then calculate the buttons position in the global space
        var buttonGlobalPoint2:Point = buttonClicked2.localToGlobal(localPoint2);

        //trace(localPoint);
        trace(buttonGlobalPoint2);

        if (buttonGlobalPoint2.x < leftEdge2){
            gotoAndStop(buttonClicked2.name + "_right");
        } else { 
            gotoAndStop(buttonClicked2.name + "_left");
        }

    }

答案 2 :(得分:0)

更好的是,Cadin在使用鼠标的正确方向上暗示,而不是担心鼠标的确切位置我只需要知道鼠标是在屏幕的左侧还是右侧的某个阈值内。我可以决定弹出窗口会出现在哪一侧。

stop();

import flash.events.MouseEvent;
import flash.geom.Point;
import flash.display.MovieClip;
import flash.display.DisplayObject;

//attach listeners to all your buttons
btn19980930.addEventListener(MouseEvent.CLICK, showPopup);
btn19990224.addEventListener(MouseEvent.CLICK, showPopup);
btn20010105.addEventListener(MouseEvent.CLICK, showPopup);
btn20050702.addEventListener(MouseEvent.CLICK, showPopup);
btn20060000.addEventListener(MouseEvent.CLICK, showPopup);
btn20060629.addEventListener(MouseEvent.CLICK, showPopup);
btn200720080000.addEventListener(MouseEvent.CLICK, showPopup);
btn20080229.addEventListener(MouseEvent.CLICK, showPopup);
btn20090307.addEventListener(MouseEvent.CLICK, showPopup);
btn20091209.addEventListener(MouseEvent.CLICK, showPopup);
btn20101125.addEventListener(MouseEvent.CLICK, showPopup);
btn20110120.addEventListener(MouseEvent.CLICK, showPopup);
btn20110312.addEventListener(MouseEvent.CLICK, showPopup);
btn20110729.addEventListener(MouseEvent.CLICK, showPopup);
btn20110316.addEventListener(MouseEvent.CLICK, showPopup);
btn20110815.addEventListener(MouseEvent.CLICK, showPopup);
btn20110909.addEventListener(MouseEvent.CLICK, showPopup);
btn20110928.addEventListener(MouseEvent.CLICK, showPopup);
btn20111214.addEventListener(MouseEvent.CLICK, showPopup);
btn20120110.addEventListener(MouseEvent.CLICK, showPopup);

var initialX : Number = 496;

function showPopup(evt:MouseEvent){
    //figure out which button got clicked
    var buttonClicked = evt.currentTarget;

    // not finished, we need to calculate using timeline_mc x and width

    if (stage.mouseX <= 400){
        gotoAndStop(buttonClicked.name + "_right");
    } else { 
        gotoAndStop(buttonClicked.name + "_left");
    }

}