【问题标题】:AS3 localToGlobal returning seemingly random x valuesAS3 localToGlobal 返回看似随机的 x 值
【发布时间】:2015-05-18 17:22:58
【问题描述】:

我有一个带有滚动条的主时间线。子时间线包含一个带有按钮的长字符串。我正在使用 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");
    }

}

【问题讨论】:

  • 根据研究调查 hitTestObject,但仍然没有回答为什么 localToGlobal 在处理滚动时不喜欢工作

标签: actionscript-3 flash


【解决方案1】:

你应该可以通过stageXstageY获取鼠标事件的全局坐标:

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

【讨论】:

  • 有些东西告诉我这会起作用,我喜欢看鼠标的想法,它是恒定的,而不是按钮不是。但我不确定如何或将它放在我的代码中的位置。必要的编辑超出了我的能力范围,您介意告诉我如何实现吗?
【解决方案2】:

最终奏效的一件事,虽然它很混乱而且过于庞大,但它是复制每个按钮的脚本部分,以便能够为每个按钮提供不同的“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");
        }

    }

【讨论】:

    【解决方案3】:

    更好的是,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");
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2014-12-10
      • 1970-01-01
      • 2019-09-26
      相关资源
      最近更新 更多