Question

This was my code to make my movie clip follow my mouse.

function yoursub_onEnterFrame(e:Event) {
    if (yoursub.x < mouseX) {
        yoursub.x -= (yoursub.x-mouseX)/speed;
    } else {
        yoursub.x += (mouseX-yoursub.x)/speed;
    }

    if (yoursub.y<mouseY) {
        yoursub.y -= (yoursub.y-mouseY)/speed;
    } else {
        yoursub.y += (mouseY-yoursub.y)/speed;
    }

    if (0 < mouseY && mouseY < 100) {
        yoursub.y = 160;
    }
}
stage.addEventListener(Event.ENTER_FRAME,yoursub_onEnterFrame);

This is the code that is added to change my mouse controls to keyboard controls. I've deleted everything above.

var isRight = false;

function keyPressed(event:KeyboardEvent) {
    if (event.keyCode == Keyboard.RIGHT) {
        isRight = true;
    }
}

function keyNotPressed(event:KeyboardEvent) {
    if (event.keyCode == Keyboard.RIGHT) {
        isRight = false;
    }
}

var spd = 5;

function smoothMove(event:KeyboardEvent) {
    if (isRight == true) {
            yoursub.x += spd;
    }
}
addEventListener(Event.ENTER_FRAME, smoothMove);
stage.addEventListener(KeyboardEvent.KEY_UP, keyNotPressed);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

but upon running the game, my movie clip disappears and I get this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@26871921 to flash.events.KeyboardEvent.
Was it helpful?

Solution

Change your smoothMove method' declaration like this:

function smoothMove(event:Event) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top