ENTER_FRAME事件与MouseEvent As3无法正常工作

时间:2015-04-23 00:41:07

标签: actionscript-3 mouseevent enterframeevent

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

//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;

        }
    }

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

1 个答案:

答案 0 :(得分: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;
    }
}