0

I know I am missing something very simple but I just can't seem to figure it out. So I have my buttons that control a log on the stage like so:

//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");
        }
    }

Now these control the logs rotation that I have setup in an ENTER_FRAME event listener function called logControls(); like so:

    private function logControls():void 
    {


        if (clickRight)
        {

            log.rotation += 20;

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

        }
    }

What I want to do is when the user presses left or right the log rotates each frame left or right. But what is happening is it only rotates one way and doesnt respond to the other mouse events. What could I be doing wrong?

Nathan
  • 536
  • 4
  • 21
  • no need to check for (`e.type == MouseEvent.CLICK`) - you will get a runtime error if it were otherwise. You probably just need to set your `clickLeft` and `clickRight` vars to false when the opposite one is set or on mouse up. – BadFeelingAboutThis Apr 23 '15 at 00:44

1 Answers1

1

Likely you just need to set opposite var to false when you set a rotation. So if you're rotating left, you want to set the clickRight var to 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;
    }
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Thanks LDMS this worked perfect. I tried doing this exact same thing but inside the logControl Function so it still wasnt working correctly. Didn't think added to the mouse event would fix it haha. Thanks again! – Nathan Apr 23 '15 at 00:51
  • So thats the same method but just cleaner? – Nathan Apr 23 '15 at 01:18