Javascript onkeyup / onkeydown输入错误

时间:2012-03-16 22:48:09

标签: javascript javascript-events keyboard-events easeljs

我正在玩一个非常简单的小射手来学习画架,tweenjs和画布编码。我遇到了一个困扰我的问题。

我在Chrome中收到unexpected end of imput错误,表示line 1。那是什么?

请注意,在下面的代码中,我已经注释掉了所有键盘输入代码。错误不再出现。当我取消注释文档。addEventListenerwindow.addEventListener输入代码时,会再次抛出错误。进一步的实验让我相信它与事件对象有关,但除此之外我不知道。

希望有人可以提供帮助!

window.addEventListener('load', eventWindowLoaded, false);

function eventWindowLoaded() {
    init();
}

function init() {
    console.log("init hit");
    canvas = document.getElementById("canvas");
    stage = new Stage(canvas);

    createStarField();

    shipImg = new Image();
    shipImg.onload = onShipLoaded;
    shipImg.src = "ship1.png";

    Ticker.setFPS(30);

    Ticker.addListener(window);

 /*   document.addEventListener('keydown', function(event) {
        switch(event.keyCode)  {
            case 37: // left
                moveLeft = true; moveRight = false;
                break;

            case 38: moveUp = true; moveLeft = false;
                break;

            case 39: moveRight = true; moveLeft = false;
                break;

            case 40: moveDown = true; moveUp = false;
                break;
        }
    }, false);

    document.addEventListener('keyup', function() {
        switch(e.keyCode) {
            // left
            case 37: moveLeft = false;
                break;
            // up
            case 38: moveUp = false;
                break;
            // right
            case 39: moveRight = false;
                break;
            // down
            case 40: moveDown = false;
                break;
    }
}, false);
*/

/*function onKeyDown(e) {
    //if(!e) { var e = window.event; }

    switch(e.keyCode) {
        // left
            case 37: moveLeft = true; moveRight = false;
            break;

            case 38: moveUp = true; moveLeft = false;
            break;

            case 39: moveRight = true; moveLeft = false;
            break;

            case 40: moveDown = true; moveUp = false;
            break;
    }
}

function onKeyUp(e) {
   // if(!e) { var e = window.event; }
    switch(e.keyCode) {
        // left
        case 37: moveLeft = false;
            break;
        // up
        case 38: moveUp = false;
            break;
        // right
        case 39: moveRight = false;
            break;
        // down
        case 40: moveDown = false;
            break;
    }
*/
    function checkMovement() {
        if(moveLeft)
        {
            ship.x -= speed;
            if(ship.x < 0)
                ship.x = 640;
        }
        else if(moveRight)
        {
            ship.x += speed;
            if(ship.x > 640)
                ship.x = 0;
        }

        if(moveUp)
        {
            if(ship.y - speed > 24)
                ship.y -= speed;
        }
        else if(moveDown)
        {
            if(ship.y + speed < 460)
                ship.y += speed;
        }
    }

    function onShipLoaded() {
    ship = new Bitmap(shipImg);
    ship.regX = ship.image.width * .05;
    ship.regY = ship.image.height * 0.5;
    ship.x = 320;
    ship.y = 450;

    stage.addChild(ship);
}

function createStarField() {
    console.log("create star field");
    stars = new Array();

    g = new Graphics();
    g.setStrokeStyle(1);
    g.beginStroke(Graphics.getRGB(255,255,255));
    g.beginFill(Graphics.getRGB(255,255,255));
    g.drawCircle(0,0,1);

    for(var i = 0; i < 100; ++i) {
        var s = new Shape(g);
        stars.push(s);
        s.x = randRange(10,630);
        s.y = randRange(-250, 470);
        s.scaleX = randRange(0.5, 2);
        s.scaleY = s.scaleX;
        s.alpha = Math.random() + 0.2;

        stage.addChild(s);
    }
}

function randRange(min, max) {
    return Math.floor(Math.random()*(max - min)) + min;
}

function tick() {
    console.log("tick hit");
    updateStarField();
    checkMovement();
    stage.update();
}

function updateStarField() {
    console.log("updateStarField()");
    var curStar;
    var limit = stars.length;
    for(var i = 0; i < limit; ++i) {
        curStar = stars[i];
        curStar.y += 4
            if(curStar.y > 480) {
                curStar.x = randRange(10,630);
                curStar.y = -randRange(20, 450);
            }
    }
}

1 个答案:

答案 0 :(得分:1)

最后一个注释掉的onKeyUp(e)函数没有结束大括号(}