7

我从一个等距游戏开始,当绘制地面的所有部分时,我的画布在闪烁(不在 IE 中)。当我将 fps 设置为 20 或更低时,闪烁停止。我该如何解决?有任何想法吗?

var camerax = 300, cameray = 100;
var fps = 60;

function draw() {
    clearCanvas();
    drawGround();
}

function drawGround() {
    var img = new Image();
    img.onload = function() {
    var width = img.width;
    var height = img.height;
    for (var x = 0; x < 3; x++) {
        for (var y = 3; y >= 0; y--) {
                mx = (x-y)*height + camerax;
                my = (x+y)*height/2 + cameray; 
                ctx.drawImage(img, mx, my);
             }
        }
    }
    img.src = "ground.png";
}

var loop = setInterval(function() {
    update();
    draw();
}, 1000/fps);
4

2 回答 2

10

现在,您每帧都重新加载图像,除非onload回调在帧的 16 毫秒内触发,否则您将看到一个空白画布。

您应该只需要调用new Image,img.onload序列一次来预加载您的图像。然后onload回调将启动您的第一帧,并且绘图调用可以自由使用内存中的图像。

就像是:

var camerax = 300, cameray = 100;
var fps = 60;
var img;
var loop;

function init() {
    img = new Image();
    img.onload = function() {
        loop = setInterval(function() {
            update();
            draw();
        }, 1000/fps);
    };
    img.src = "ground.png";
}

function draw() {
    clearCanvas();
    drawGround();
}

function drawGround() {
    var width = img.width;
    var height = img.height;
    for (var x = 0; x < 3; x++) {
        for (var y = 3; y >= 0; y--) {
                mx = (x-y)*height + camerax;
                my = (x+y)*height/2 + cameray; 
                ctx.drawImage(img, mx, my);
             }
        }
    }
}

当然,一旦您等待多个图像预加载,它就会变得更加复杂,因为您只需要在所有图像都完成后启动循环。

于 2013-06-23T13:31:28.150 回答
1

不错的提示,freejosh!谢谢!我的屏幕现在没有闪烁,代码结果是:

        var canvas = document.getElementById("game");
        var ctx = canvas.getContext("2d");

        var camerax = 300, cameray = 100;
        var fps = 60;
        var img;
        var loop;

        function update() {
        }

        function draw() {
            clearCanvas();
            drawGround();
        }

        function clearCanvas() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }

        function drawGround() {
            var width = img.width;
            var height = img.height;
            for (var x = 0; x < 3; x++) {
                for (var y = 3; y >= 0; y--) {
                    mx = (x-y)*height + camerax;
                    my = (x+y)*height/2 + cameray; 
                    ctx.drawImage(img, mx, my);
                }
            }
        }

        function init() {
            img = new Image();
            img.onload = function() {
                drawGround();
            };
            img.src = "ground.png";
        }

        function keyListener(e){
           e = e || window.event

           if(e.keyCode==37){
              camerax--;
           }
           else if(e.keyCode==39){
              camerax++;
           }
           else if(e.keyCode==38){
              cameray--;
           }
           else if(e.keyCode==40){
              cameray++;
           }                           
        }

        window.onkeypress = function(e) {
            keyListener(e);
        }

        init();
        var loop = setInterval(function() {
            update();
            draw();
        }, 1000/fps);
于 2013-06-23T13:44:33.323 回答