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?
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;
}
}