0
votes

Full error: TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::KeyboardEvent@4ff2301 to flash.events.MouseEvent.

I followed a really simple tutorial for a simple quiz using ActionScript 3. However once complete I receive the above error. The tutorial is quite old so was just hoping if someone here could just check it and see if I have made an obvious mistake.

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

1 Answers

0
votes

Replace go(e:MouseEvent) with go(e:Event).

You registered one event handler to MouseEvent and KeyboardEvent. When MouseEvent.CLICK will be fired, function go will be called. An instance of MouseEvent will be passed inside function go. There is no error. But when KeyboardEvent.KEY_DOWN will be fired, instance of KeyboardEvent will be passed inside go function. And error happens. Sorry for my English. Do you understand?