单击复选框后出现Movieclip错误?

时间:2015-08-07 10:17:29

标签: actionscript-3 flash checkbox keyboard-events movieclip

所以我在Adobe Flash CS6中创建这个测验游戏,其中化身必须捕获掉落的字母,化身通过键盘事件向右和向左移动。 我想在游戏开头添加一个复选框,以选择是否要重新调整问题。所以我为复选框添加了这段代码:

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 = "";
    }
}

然后我添加了这段代码来改变问题变量:

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

我选中了复选框,问题已成功洗牌。但是化身不会移动。这是头像代码:

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

我真的很困惑,如果我没有点击复选框,一切都会正常工作,但如果我点击复选框并开始游戏,问题就会被洗牌,但是头像不会移动。 它与键盘事件或鼠标事件有关吗?还是EventListener?你能帮助我吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

正如我所想,这是一个焦点问题,因为在选择复选框时,您的用户应该点击舞台上的任意位置以调度键盘事件。

为避免这种情况,您可以将stage.focus设置为null

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

希望可以提供帮助。