Search code examples
actionscript-3flashcheckboxkeyboard-eventsmovieclip

Movieclip error after clicking a checkbox?


So I'm creating this quiz game in Adobe Flash CS6 where the avatar have to catch falling letters, the avatar moves to the right and left with keyboard event. I want to add a checkbox at the beginning of the game to choose whether I want to shuffle the questions or not. So I added this code for the checkbox:

var shuffleOn:Boolean = false;
shufflecheckbox.addEventListener(MouseEvent.CLICK, ShuffleOn);

function ShuffleOn(event:MouseEvent):void
{
    if (shufflecheckbox.selected == true){
    shuffleOn = true;
    ShuffleText.text = "Questions Will Be Shuffled";
    }
    else{
    shuffleOn = false;
    ShuffleText.text = "";
    }
}

then I added this code to shuffle the questions variable:

function ShuffleArray(input:Array)
{
    for (var i:int=input.length-1; i>=0; i--)
    {
        var randomIndex:int = Math.floor(Math.random() * (i+1));
        var itemAtIndex:int = input[randomIndex];
        input[randomIndex] = input[i];
        input[i] = itemAtIndex;
    }
}

if (shuffleOn == true){
    ShuffleArray(questions);
    }

I checked the checkbox, and the questions are successfully shuffled. But the avatar won't move. Here is the avatar code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, moveavatar);
stage.addEventListener(KeyboardEvent.KEY_UP, stopavatar);
function moveavatar(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.RIGHT)
    {
        rightPressed = true;
    }
    else if (e.keyCode == Keyboard.LEFT)
    {
        leftPressed = true;
    }
    else if (e.keyCode == Keyboard.UP)
    {
        if (onFloor)
        {
            jumpsnd.play();
            speedY =  -  impulsion;
            onFloor = false;
        }
    }
}
function stopavatar(e:KeyboardEvent):void
{
    if (e.keyCode == Keyboard.RIGHT)
    {
        rightPressed = false;
    }
    else if (e.keyCode == Keyboard.LEFT)
    {
        leftPressed = false;
    }
}

if (avatarmove.y > floor)
        {
            speedY = 0;
            avatarmove.y = floor;
            onFloor = true;
            avatarmove.avatar.play();
            shade.gotoAndStop(1);
        }
        else
        {
            avatarmove.avatar.stop();
            shade.gotoAndStop(2);
        }
        if (rightPressed)
        {
            avatarmove.gotoAndStop(3);
            if (avatarmove.x < 540)
            {
                avatarmove.x +=  speedX;
            }
        }
        else if (leftPressed)
        {
            avatarmove.gotoAndStop(2);
            if (avatarmove.x > 10)
            {
                avatarmove.x -=  speedX;
            }
        }
        else
        {
            avatarmove.gotoAndStop(1);
        }

I'm just really confused, if I don't click the checkbox, everything would work fine, but if I click the checkbox and start the game, the questions are shuffled but the avatar won't move. Does it have something to do with the keyboard events or mouse events? or the EventListener? Can you guys help me?

Thank you.


Solution

  • As I thought, it's a focus problem, because when selecting the checkbox, your user should click anywhere on your stage to have the keyboard events dispatched.

    To avoid that, you can set the stage.focus to null :

    shufflecheckbox.addEventListener(MouseEvent.CLICK, ShuffleOn);
    function ShuffleOn(event:MouseEvent):void
    {
        shuffleOn = shufflecheckbox.selected;
        ShuffleText.text = shuffleOn ? 'Questions Will Be Shuffled' : '';
        stage.focus = null; 
    }
    

    Hope that can help.