完整错误: TypeError:错误#1034:类型强制失败:无法将flash.events::KeyboardEvent@4ff2301转换为flash.events.MouseEvent。
我使用ActionScript 3进行了一个非常简单的简单测验教程。但是一旦完成,我收到上述错误。这个教程已经很老了,所以只是希望这里有人可以检查它,看看我是否犯了一个明显的错误。
Enter_btn.addEventListener(MouseEvent.CLICK, go);
var quizModel:Array = [{q:"2+2", a:"4"}, {q:"What colour is a banana = ?", a:"Yellow"}];
var user_ans:Array = new Array();
var newQuizModel:Array = shuffleArray(quizModel);
stage.addEventListener(KeyboardEvent.KEY_DOWN, go);
function showQuiz(index:int):void
{
question_txt.text = newQuizModel[index]["q"];
}
var index:int = 0;
question_txt.text = newQuizModel[index]["q"];
var userAnswer:String = "";
function go(e:MouseEvent)
{
userAnswer = answers_txt.text;
if (userAnswer == newQuizModel[index]["a"])
{
rightwrong_txt.text = "Your answer is correct!";
}
else
{
rightwrong_txt.text = "Your answer is wrong";
return;
}
index++;
index%= quizModel.length;
showQuiz(index);
}
function shuffleArray(arr:Array):Array
{
var l:Number = arr.length - 1;
for (var it:uint = 0; it<l; it++)
{
var r:int = Math.round(Math.random() * l); /// l is an L
var tmp:Object = arr[it];
arr[it] = arr[r];
arr[r] = tmp;
}
return arr;
}
showQuiz(index);
答案 0 :(得分:0)
将go(e:MouseEvent)
替换为go(e:Event)
。
您向MouseEvent和KeyboardEvent注册了一个事件处理程序。当MouseEvent.CLICK
被触发时,将调用函数go
。 MouseEvent
的实例将在函数go
内传递。没有错误。但是当KeyboardEvent.KEY_DOWN
被触发时,KeyboardEvent的实例将在go
函数内传递。并且发生错误。对不起我的英语不好。你明白了吗?