HTML5 Canvas图像功能触发,但有时未绘制图像

时间:2019-04-25 05:16:48

标签: javascript jquery html5 canvas game-development

我正在为telnet游戏创建界面。当我获得telnet更新时,图像功能启动,并且所有内容都被解析。

在大多数情况下,我的功能正常运行。有时,当图像更改时,第一个图像功能 floorLoad 和第四个图像功能 southWall 会绘制在画布上。我正在使用函数加载器,并且在控制台中可以看到所有图像函数均已启动并且顺序正确。如果我离开后回到房间重新加载房间,则按预期运行。这是我在说的视觉示例。

这是正常时的样子

enter image description here

这是发生的错误

enter image description here

请注意,地板覆盖了整个区域 floorLoad (地板负荷),并且指向下方的箭头是 southWall

任何有关故障排除的帮助或指导将不胜感激。我没有404,所有图像都从缓存中加载,当您第一次加载到页面时,我使用了图像预加载器:

    // Preload Room Art
    $('#preload').load('art_preload.php', function() {   
        $('#preload').remove();   
    });

以下是我的房间美术功能:

function room_update (str) {

var north_wall_neg;
var east_wall_neg;
var south_wall_neg;
var west_wall_neg;

//////////////////////////////////////////////////////////////////
// Room Exits
//////////////////////////////////////////////////////////////////
var exits;
var room_exits = str.match( /<prompt\ .*~(.*)~.*>/ );
if ( room_exits !== null && room_exits.length !== 0 ) {
    exits = room_exits[1];
} else {
    exits = "";
}

//////////////////////////////////////////////////////////////////
// Room Art
//////////////////////////////////////////////////////////////////
var room_art = str.match( /<RA\ (.*)\ (.*)\ (.*)\ (.*)\ (.*)>/ );
if ( room_art !== null && room_art.length !== 0 ) {

    function roomArt () {
        var defer = $.Deferred();
        //////////////////////////////////////////////////////////////////
        // Vars
        //////////////////////////////////////////////////////////////////
        var imgPath = "./images/room_art/";

        //////////////////////////////////////////////////////////////////
        // Floor
        //////////////////////////////////////////////////////////////////
        var floor_tile_num = room_art[5];
        function floorLoad (floor_tile_num) {
            var defer = $.Deferred();
            var images = ['tile' + floor_tile_num + '.png'];
                var loadedImages = {};
                var promiseArray = images.map(function(imgurl){
                   var prom = new Promise(function(resolve,reject){
                       var img = new Image();
                       img.onload = function(){
                           loadedImages[imgurl] = img;
                           resolve();
                       };
                       img.src = imgPath + imgurl;
                   });
                   return prom;
                });
                Promise.all(promiseArray).then(function(){
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 2, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 52, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 102, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 152, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 202, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 252, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 302, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 352, 345);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 45);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 95);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 145);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 195);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 245);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 295);
                    ctx.drawImage(loadedImages['tile' + floor_tile_num + '.png'], 402, 345);
                });
                console.log("<-- FLOOR -->");
                return defer;
        }

        //////////////////////////////////////////////////////////////////
        // North Wall
        //////////////////////////////////////////////////////////////////
        var north_wall_num  = room_art[1];
        if ( north_wall_num < 0 ) {
            north_wall_num = north_wall_num.replace("-", "");
            north_wall_neg = "1";
        }
        function northWall (pos, north_wall_num) {
            var defer = $.Deferred();
            if ( pos.indexOf(" N ") != -1 ) {
                var arrowExit = new Image();
                arrowExit.src = './images/room_art/arrowExit.png';
                arrowExit.onload = function(){
                    ctx.drawImage(arrowExit, 201, 45);
                };
            } else {
                var images = ['Cwall' + north_wall_num + '.png', 'wall' + north_wall_num + '.png', 'Door1.png', 'DoorN1.png'];
                var loadedImages = {};
                var promiseArray = images.map(function(imgurl){
                   var prom = new Promise(function(resolve,reject){
                       var img = new Image();
                       img.onload = function(){
                           loadedImages[imgurl] = img;
                           resolve();
                       };
                       img.src = imgPath + imgurl;
                   });
                   return prom;
                });
                Promise.all(promiseArray).then(function(){
                    // Corners
                    if ( pos.indexOf(" W ") != -1 ) {
                        ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 2, 45);
                    } else if ( north_wall_neg == "1" ) {
                        ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 2, 45);
                    } else {
                        drawRotatedImage(loadedImages['Cwall' + north_wall_num + '.png'], 27, 70, 270);
                    } 
                    if ( pos.indexOf(" E ") != -1 ) {
                        ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 402, 45);
                    } else if ( north_wall_neg == "1" ) {
                        ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 402, 45);
                    } else {
                        ctx.drawImage(loadedImages['Cwall' + north_wall_num + '.png'], 402, 45);
                    }
                    // Walls
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 52, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 102, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 152, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 202, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 252, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 302, 45);
                    ctx.drawImage(loadedImages['wall' + north_wall_num + '.png'], 352, 45);
                    // Door
                    if ( pos.indexOf(" -N ") != -1 ) {
                        ctx.drawImage(loadedImages['Door1.png'], 217, 65);
                    }
                    if ( pos.indexOf(" oN ") != -1 ) {
                        ctx.drawImage(loadedImages['DoorN1.png'], 217, 65);
                    }
                });
            }
            console.log("<-- N WALL -->");
            return defer;
        }

        //////////////////////////////////////////////////////////////////
        // East Wall
        //////////////////////////////////////////////////////////////////
        var east_wall_num   = room_art[2];
        function eastWall (pos, east_wall_num) {
            var defer = $.Deferred();
            if ( pos.indexOf(" E ") != -1 ) {
                var arrowExit = new Image();
                arrowExit.src = './images/room_art/arrowExit.png';
                arrowExit.onload = function(){
                    drawRotatedImage(arrowExit, 427, 221, 90);
                };
            } else {
                if ( pos.indexOf(" -E ") != -1 ) {
                    var images = ['Cwall' + east_wall_num + '.png', 'wall' + east_wall_num + '.png', 'Door1.png']; 
                } else if ( pos.indexOf(" oE ") != -1 ) {
                    var images = ['Cwall' + east_wall_num + '.png', 'wall' + east_wall_num + '.png', 'DoorN1.png']; 
                } else {
                    var images = ['Cwall' + east_wall_num + '.png', 'wall' + east_wall_num + '.png'];
                }
                var loadedImages = {};
                var promiseArray = images.map(function(imgurl){
                   var prom = new Promise(function(resolve,reject){
                       var img = new Image();
                       img.onload = function(){
                           loadedImages[imgurl] = img;
                           resolve();
                       };
                       img.src = imgPath + imgurl;
                   });
                   return prom;
                });
                Promise.all(promiseArray).then(function(){
                    // Walls
                    if ( pos.indexOf(" N ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 70, 90);
                    }
                    drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 120, 90);
                    drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 170, 90);
                    drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 220, 90);
                    drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 270, 90);
                    drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 320, 90);
                    if ( pos.indexOf(" S ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + east_wall_num + '.png'], 427, 370, 90);
                    }
                    // Door
                    if ( pos.indexOf(" -E ") != -1 ) {
                        drawRotatedImage(loadedImages['Door1.png'], 417, 221, 90);
                    }
                    if ( pos.indexOf(" oE ") != -1 ) {
                        drawRotatedImage(loadedImages['DoorN1.png'], 417, 221, 90);
                    }
                });
            }
            console.log("<-- E WALL -->");
            return defer;
        }

        //////////////////////////////////////////////////////////////////
        // South Wall
        //////////////////////////////////////////////////////////////////
        var south_wall_num  = room_art[3];
        if ( south_wall_num < 0 ) {
            south_wall_num = south_wall_num.replace("-", "");
            south_wall_neg = "1";
        }
        function southWall (pos, south_wall_num) {
            var defer = $.Deferred();
            if ( pos.indexOf(" S ") != -1 ) {
                var arrowExit = new Image();
                arrowExit.src = './images/room_art/arrowExit.png';
                arrowExit.onload = function(){
                    drawRotatedImage(arrowExit, 228, 370, 180);
                };
            } else {
                if ( pos.indexOf(" -S ") != -1 ) { 
                    var images = ['Cwall' + south_wall_num + '.png', 'wall' + south_wall_num + '.png', 'Door1.png'];
                } else if ( pos.indexOf(" oS ") != -1 ) { 
                    var images = ['Cwall' + south_wall_num + '.png', 'wall' + south_wall_num + '.png', 'DoorN1.png']; 
                } else { 
                    var images = ['Cwall' + south_wall_num + '.png', 'wall' + south_wall_num + '.png'];
                }
                var loadedImages = {};
                var promiseArray = images.map(function(imgurl){
                   var prom = new Promise(function(resolve,reject){
                       var img = new Image();
                       img.onload = function(){
                           loadedImages[imgurl] = img;
                           resolve();
                       };
                       img.src = imgPath + imgurl;
                   });
                   return prom;
                });
                Promise.all(promiseArray).then(function(){
                    // Corners
                    var south_wall_num_neg;
                    if ( pos.indexOf(" W ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 27, 370, 180);
                    } else if ( south_wall_neg == "1" ) {
                        drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 27, 370, 180);
                    } else {
                        drawRotatedImage(loadedImages['Cwall' + south_wall_num + '.png'], 27, 370, 180);
                    } 
                    if ( pos.indexOf(" E ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 427, 370, 180);
                    } else if ( south_wall_neg == "1" ) {
                        drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 427, 370, 180);
                    } else {
                        drawRotatedImage(loadedImages['Cwall' + south_wall_num + '.png'], 427, 370, 90);
                    }
                    // Walls
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 77, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 127, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 177, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 227, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 277, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 327, 370, 180);
                    drawRotatedImage(loadedImages['wall' + south_wall_num + '.png'], 377, 370, 180);
                    // Door
                    if ( pos.indexOf(" -S ") != -1 ) {
                        drawRotatedImage(loadedImages['Door1.png'], 227, 360, 180);
                    }
                    if ( pos.indexOf(" oS ") != -1 ) {
                        drawRotatedImage(loadedImages['DoorN1.png'], 227, 360, 180);
                    }
                });
            }
            console.log("<-- S WALL -->");
            return defer;
        }

        //////////////////////////////////////////////////////////////////
        // West Wall
        //////////////////////////////////////////////////////////////////
        var west_wall_num   = room_art[4];
        function westWall (pos, west_wall_num) {
            var defer = $.Deferred();
            if ( pos.indexOf(" W ") != -1 ) {
                var arrowExit = new Image();
                arrowExit.src = './images/room_art/arrowExit.png';
                arrowExit.onload = function(){
                    drawRotatedImage(arrowExit, 27, 217, 270);
                };
            } else {
                if ( pos.indexOf(" -W ") != -1 ) {
                    var images = ['Cwall' + west_wall_num + '.png', 'wall' + west_wall_num + '.png', 'Door1.png']; 
                } else if ( pos.indexOf(" oW ") != -1 ) {
                    var images = ['Cwall' + west_wall_num + '.png', 'wall' + west_wall_num + '.png', 'DoorN1.png']; 
                } else {
                    var images = ['Cwall' + west_wall_num + '.png', 'wall' + west_wall_num + '.png'];
                }
                var loadedImages = {};
                var promiseArray = images.map(function(imgurl){
                   var prom = new Promise(function(resolve,reject){
                       var img = new Image();
                       img.onload = function(){
                           loadedImages[imgurl] = img;
                           resolve();
                       };
                       img.src = imgPath + imgurl;
                   });
                   return prom;
                });
                Promise.all(promiseArray).then(function(){
                    // Walls
                    if ( pos.indexOf(" N ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 70, 270);
                    }
                    drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 120, 270);
                    drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 170, 270);
                    drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 220, 270);
                    drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 270, 270);
                    drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 320, 270);
                    if ( pos.indexOf(" S ") != -1 ) {
                        drawRotatedImage(loadedImages['wall' + west_wall_num + '.png'], 27, 370, 270);
                    }
                    // Door
                    if ( pos.indexOf(" -W ") != -1 ) {
                        drawRotatedImage(loadedImages['Door1.png'], 36, 217, 270);
                    }
                    if ( pos.indexOf(" oW ") != -1 ) {
                        drawRotatedImage(loadedImages['DoorN1.png'], 36, 217, 270);
                    }
                });
            }
            console.log("<-- W WALL -->");
            return defer;
        }

        //////////////////////////////////////////////////////////////////
        // Room Art Function Calls
        //////////////////////////////////////////////////////////////////
        floorLoad(floor_tile_num)
        .then(northWall(exits, north_wall_num))
        .then(eastWall(exits, east_wall_num))
        .then(southWall(exits, south_wall_num))
        .then(westWall(exits, west_wall_num));
        console.log("<-- SEQUENCE -->");
        return defer;
    }

    roomArt();
}

//////////////////////////////////////////////////////////////////
// On <prompt
//////////////////////////////////////////////////////////////////
if ( room_exits !== null && room_exits.length !== 0 ) {
    console.log("Room exists...");
    function darkRoom () {
        var defer = $.Deferred();
        var room_dark = str.match( /<room\ .*><(.*)>.*<room end>/ );
        if ( room_dark !== null && room_dark.length !== 0 ) {
            if ( room_dark[1] == "dark" ) {
                ctx.fillStyle = "#000000";
                ctx.fillRect(2, 45, 450, 350);
                console.log("it's dark!");
            }
        }
        return defer;
    }
    darkRoom();
    // Door Artwork
    // North Wall
    switch (true) {
        case room_exits[1].indexOf(" N ") != -1:
            var arrowExit_n = new Image();
            arrowExit_n.src = './images/room_art/arrowExit.png';
            arrowExit_n.onload = function(){
                ctx.drawImage(arrowExit_n, 201, 45);
            };
            break;
        case room_exits[1].indexOf(" -N ") != -1:
            var doorClosed_n = new Image();
            doorClosed_n.src = './images/room_art/Door1.png';
            doorClosed_n.onload = function(){
                ctx.drawImage(doorClosed_n, 217, 65);
            };
            break;
        case room_exits[1].indexOf(" oN ") != -1:
            var doorOpen_n = new Image();
            doorOpen_n.src = './images/room_art/DoorN1.png';
            doorOpen_n.onload = function(){
                ctx.drawImage(doorOpen_n, 217, 65);
            };
            break;
    }

    // West Wall
    switch (true) {
        case room_exits[1].indexOf(" W ") != -1:
            var arrowExit_w = new Image();
            arrowExit_w.src = './images/room_art/arrowExit.png';
            arrowExit_w.onload = function(){
                drawRotatedImage(arrowExit_w, 27, 217, 270);
            };
            break;
        case room_exits[1].indexOf(" -W ") != -1:
            var doorClosed_w = new Image();
            doorClosed_w.src = './images/room_art/Door1.png';
            doorClosed_w.onload = function(){
                drawRotatedImage(doorClosed_w, 36, 217, 270);
            };
            break;
        case room_exits[1].indexOf(" oW ") != -1:
            var doorOpen_w = new Image();
            doorOpen_w.src = './images/room_art/DoorN1.png';
            doorOpen_w.onload = function(){
                drawRotatedImage(doorOpen_w, 36, 217, 270);
            };
            break;
    }
    // South Wall
    switch (true) {
        case room_exits[1].indexOf(" S ") != -1:
            var arrowExit_s = new Image();
            arrowExit_s.src = './images/room_art/arrowExit.png';
            arrowExit_s.onload = function(){
                drawRotatedImage(arrowExit_s, 228, 370, 180);
            };
            break;
        case room_exits[1].indexOf(" -S ") != -1:
            var doorClosed_s = new Image();
            doorClosed_s.src = './images/room_art/Door1.png';
            doorClosed_s.onload = function(){
                drawRotatedImage(doorClosed_s, 227, 360, 180);
            };
            break;
        case room_exits[1].indexOf(" oS ") != -1:
            var doorOpen_s = new Image();
            doorOpen_s.src = './images/room_art/DoorN1.png';
            doorOpen_s.onload = function(){
                drawRotatedImage(doorOpen_s, 227, 360, 180);
            };
            break;
    }
    // East Wall
    switch (true) {
        case room_exits[1].indexOf(" E ") != -1:
            var arrowExit_e = new Image();
            arrowExit_e.src = './images/room_art/arrowExit.png';
            arrowExit_e.onload = function(){
                drawRotatedImage(arrowExit_e, 427, 221, 90);
            };
            break;
        case room_exits[1].indexOf(" -E ") != -1:
            var doorClosed_e = new Image();
            doorClosed_e.src = './images/room_art/Door1.png';
            doorClosed_e.onload = function(){
                drawRotatedImage(doorClosed_e, 417, 221, 90);
            };
            break;
        case room_exits[1].indexOf(" oE ") != -1:
            var doorOpen_e = new Image();
            doorOpen_e.src = './images/room_art/DoorN1.png';
            doorOpen_e.onload = function(){
                drawRotatedImage(doorOpen_e, 417, 221, 90);
            };
            break;
    }
    // Stairs
    switch (true) {
        case room_exits[1].indexOf(" U ") != -1:
            var stairsUp = new Image();
            stairsUp.src = './images/room_art/stairsUP.png';
            stairsUp.onload = function(){
                ctx.drawImage(stairsUp, 205, 221);
            };
            break;
        case room_exits[1].indexOf(" -U ") != -1:
            var stairsUp = new Image();
            stairsUp.src = './images/room_art/stairsUP.png';
            stairsUp.onload = function(){
                ctx.drawImage(stairsUp, 205, 221);
            };
            break;
        case room_exits[1].indexOf(" oU ") != -1:
            var stairsUp = new Image();
            stairsUp.src = './images/room_art/stairsUP.png';
            stairsUp.onload = function(){
                ctx.drawImage(stairsUp, 205, 221);
            };
            break;
        case room_exits[1].indexOf(" D ") != -1:
            var stairsDown = new Image();
            stairsDown.src = './images/room_art/stairsDOWN.png';
            stairsDown.onload = function(){
                ctx.drawImage(stairsDown, 205, 221);
            };
            break;
        case room_exits[1].indexOf(" -D ") != -1:
            var stairsDown = new Image();
            stairsDown.src = './images/room_art/stairsDOWN.png';
            stairsDown.onload = function(){
                ctx.drawImage(stairsDown, 205, 221);
            };
            break;
        case room_exits[1].indexOf(" oD ") != -1:
            var stairsDown = new Image();
            stairsDown.src = './images/room_art/stairsDOWN.png';
            stairsDown.onload = function(){
                ctx.drawImage(stairsDown, 205, 221);
            };
            break;
    }

}
room_art = "";
north_wall_neg = "";
east_wall_neg  = "";
south_wall_neg = "";
west_wall_neg  = "";
}  

控制台输出:

<-- FLOOR -->
<-- N WALL -->
<-- E WALL -->
<-- S WALL -->
<-- W WALL -->
<-- SEQUENCE -->

1 个答案:

答案 0 :(得分:0)

控制台输出不能证明代码正在按照您认为的方式运行。使用诺言的方式存在一些问题。我建议添加更多调试语句,以确保所有代码都在实际执行中。

首先,我相信您会混淆两个不同的系统来实现承​​诺(并且没有正确使用它们):

  • $.Deferred()是jQuery的实现
  • new Promise()是原生JS实现

因此floorLoad.then()promiseArray.之间绝对没有任何关系,也许这是有意为之,但仅凭一种承诺就不会造成混淆。

如果您有defer = $.Deferred(),则该defer应该在某个地方解决。您的代码缺少defer.resolve()

通常,缺少defer.resolve()意味着传递给.then()的代码将永远不会执行,但是还有第二个问题:这些.then()调用中的代码未包装在函数中:

floorLoad(floor_tile_num)
.then(northWall(exits, north_wall_num))
.then(eastWall(exits, east_wall_num))
.then(southWall(exits, south_wall_num))
.then(westWall(exits, west_wall_num))

没有用于包装它们的函数,代码将立即执行并将结果传递到.then(),因此代码基本上与以下代码相同:

floorLoad(floor_tile_num);
northWall(exits, north_wall_num);
eastWall(exits, east_wall_num);
southWall(exits, south_wall_num);
westWall(exits, west_wall_num);

所以我建议你:

  • 选择一种诺言类型,并在任何地方坚持下去。
  • .then()中使用函数包装代码,因此实际上延迟了执行。
  • 添加更多调试日志记录。特别:
    • 确保每个.onload()都按预期被调用。
    • 确保每个.all()都按预期被调用。