【问题标题】:ENTER_FRAME Event not working correctly with MouseEvent As3ENTER_FRAME 事件无法与 MouseEvent As3 一起正常工作
【发布时间】:2015-06-30 22:27:37
【问题描述】:

我知道我遗漏了一些非常简单的东西,但我似乎无法弄清楚。所以我有我的按钮来控制舞台上的日志,如下所示:

//buttons for main screen left and right
        mainScreen.leftBtn.addEventListener(MouseEvent.CLICK, leftButtonClicked);
        mainScreen.rightBtn.addEventListener(MouseEvent.CLICK, rightButtonClicked);

private function leftButtonClicked(e:MouseEvent):void 
    {
        if (e.type == MouseEvent.CLICK)
        {
            clickLeft = true;
            trace("LEFT_TRUE");
        }
    }

    private function rightButtonClicked(e:MouseEvent):void 
    {
        if (e.type == MouseEvent.CLICK)
        {
            clickRight = true;
            trace("RIGHT_TRUE");
        }
    }

现在这些控制我在名为 logControls(); 的 ENTER_FRAME 事件侦听器函数中设置的日志轮换,如下所示:

    private function logControls():void 
    {


        if (clickRight)
        {

            log.rotation += 20;

        }else
        if (clickLeft)
        {
            log.rotation -= 20;

        }
    }

我想要做的是当用户向左或向右按​​下时,日志会向左或向右旋转每一帧。但是发生的事情是它只旋转一种方式并且不响应其他鼠标事件。我可能做错了什么?

【问题讨论】:

  • 无需检查 (e.type == MouseEvent.CLICK) - 如果不是,您将收到运行时错误。你可能只需要设置你的 clickLeftclickRight 变量为 false 时设置相反的或鼠标。

标签: actionscript-3 mouseevent enterframeevent


【解决方案1】:

您可能只需在设置旋转时将相反的 var 设置为 false。因此,如果您向左旋转,则需要将 clickRight var 设置为 false。

mainScreen.leftBtn.addEventListener(MouseEvent.CLICK, rotationBtnClicked);
mainScreen.rightBtn.addEventListener(MouseEvent.CLICK, rotationBtnClicked);

private function rotationBtnClicked(e:MouseEvent):void {
    clickLeft = e.currentTarget == mainScreen.leftBtn; //if it was left button clicked, this will true, otherwise false
    clickRight = e.currentTarge == mainScreen.rightBtn; //if it wasn't right button, this will now be false
}

private function logControls():void {
    if (clickRight){
        log.rotation += 20;
    }

    if (clickLeft){
        log.rotation -= 20;
    }
}

【讨论】:

  • 感谢 LDMS,这很完美。我尝试做同样的事情,但在 logControl 函数内部,所以它仍然无法正常工作。没想到添加到鼠标事件会修复它哈哈。再次感谢!
  • 所以那是相同的方法,但更干净?
猜你喜欢
  • 2018-02-26
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多