单击复选框后出现 Movieclip 错误?
Movieclip error after clicking a checkbox?
所以我在 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);
}
我真的很困惑,如果我不点击复选框,一切都会正常,但如果我点击复选框并开始游戏,问题会随机播放,但头像不会移动。
它与键盘事件或鼠标事件有关吗?还是事件监听器?你们能帮帮我吗?
谢谢。
正如我所想,这是一个焦点问题,因为在选择复选框时,您的用户应该单击您舞台上的任意位置以调度键盘事件。
为避免这种情况,您可以将 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;
}
希望能帮到你。
所以我在 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);
}
我真的很困惑,如果我不点击复选框,一切都会正常,但如果我点击复选框并开始游戏,问题会随机播放,但头像不会移动。 它与键盘事件或鼠标事件有关吗?还是事件监听器?你们能帮帮我吗?
谢谢。
正如我所想,这是一个焦点问题,因为在选择复选框时,您的用户应该单击您舞台上的任意位置以调度键盘事件。
为避免这种情况,您可以将 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;
}
希望能帮到你。