0

我把整个东西都放进去了,以防我进入 chrome 并寻找错误,但它也没有工作,所以我检查了一下,显然第一行有问题。我保留了前 6 行并删除了所有其他内容,然后它工作正常,所以我去了 SublimeText2 并在代码中搜索了每个 ) 和 }。

var canvasBg = document.getElementById('canvasBg');
var ctxBg = canvasBg.getContext('2d');


var canvasJet = document.getElementById('canvasJet');
var ctxJet = canvasJet.getContext('2d');

var jet1;
var fps = 17;
var drawInterval;

var imgSprite = new Image();
imgSprite.src = 'SpriteSheet.png'
imgSprite.addEventListener('load',init,false);


function init() {
    drawBg();
    startDrawing();
    jet1 = new Jet();
    document.addEventListener('keydown',checkKeyDown,false);
    document.addEventListener('keyup',checKeyUp,false);
}

function draw() {
    jet1.draw();
}


function startDrawing() {
    stopDrawing();
    drawInterval = setInterval(draw,fps);
}


function stopDrawing() {
    clearInterval(setInterval);
}



Jet.prototype.draw = function() {
    clearCtxJet();
    ctxJet.drawImage(imgSprite,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height);

};




function Jet() {
    this.srcX = 0;
    this.srcY = 0;
    this.drawX = 200;
    this.drawY = 200;
    this.width = 96;
    this.height = 30;
    }



function drawJet() {
}





function drawBg() {

    ctxBg.drawImage(imgSprite,96,0,800,500,0,0,800,500)

}

function clearCtxBg() {
    ctxBg.clearRect(0,0,800,500);
}

function clearCtxJet() {
    ctxJet.clearRect(0,0,800,500);
}



function checkKeyDown(e) {

    var keyID = (e.keyCode) ? e.keyCode : e.which;
    if (keyID === 38) { // 38 is up key
        alert('up arrow was pressed');
        e.preventDeafault();
    }

    if (keyID === 39) { // 39 is right key
        e.preventDeafault();
    }

    if (keyID === 40) { // 40 is down key
        e.preventDeafault();
    }

    if (keyID === 37) { // 37 is left key
        e.preventDeafault();
    }


function checkKeyup(e) {

    var keyID = (e.keyCode) ? e.keyCode : e.which;
    if (keyID === 38) { // 38 is up key
        alert('up arrow was pressed');
        e.preventDeafault();
    }

    if (keyID === 39) { // 39 is right key
        e.preventDeafault();
    }

    if (keyID === 40) { // 40 is down key
        e.preventDeafault();
    }

    if (keyID === 37) { // 37 is left key
        e.preventDeafault();
    }

}

4

2 回答 2

0

很高兴在http://jsfiddle.net中看到完整的代码

于 2013-03-03T09:18:25.683 回答
0

我假设您只显示部分代码,因此无法知道这是否是实际错误,但 checkKeyup 和 checkKeydown 都缺少右括号。

我建议为 Sublime Text 2 安装Package Control,然后使用它来安装SublimeLinter,它将能够为您检查您的代码并指出缺少的分号和大括号。

于 2013-03-03T09:16:19.290 回答