简体   繁体   English

我的Javascript瓷砖地图不会画?

[英]My Javascript tile map will not draw?

Here is my script, I can't seem to figure out what's wrong, I want to draw a tile map 12*12 and the tiles are 32px - 32px. 这是我的脚本,我似乎无法弄清楚什么是错的,我想绘制一张12 * 12的瓷砖地图,瓷砖是32px - 32px。 The tiles dont draw when I run the page, Ive tried using parse int as shown below but still, it didn't work. 当我运行页面时,瓷砖不会绘制,我尝试使用parse int,如下所示,但仍然无法正常工作。

if(parseInt(mapArray[x][y]) == 0){
    ctx.drawImage(rockTile, posX, posY, tileSize, tileSize);
}

Here is the script I have created. 这是我创建的脚本。

var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = (32 * 12);
canvas.height = (32 * 12);
document.body.appendChild(canvas);

var rockTile = new Image();
rockTile.src = "../Images/dc-dngn/floor/rect_gray0.png";
var tileSize = 32;
var posX = 0;
var posY = 0;

var mapArray = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];

$(document).ready(function(){
    drawMap();
});

function drawMap(){
    for(var x = 0; x < mapArray.length; x++){
        for(var y = 0; y < mapArray[x].length; y++){
            if(mapArray[x][y] == 0){
                ctx.drawImage(rockTile, posX, posY, tileSize, tileSize);
            }
            posX += 32;
        }
        posX = 0;
        posY += 32;
    }
}

If anyone can help me to get my tiles to draw that would greatly appreciated, Thank you! 如果有人可以帮我把我的瓷砖画得非常感谢,谢谢!

You must wait for the image to load. 您必须等待图像加载。 That image is not part of the DOM and therefore waiting for the loading of the document won't help. 该图像不是DOM的一部分,因此等待加载文档将无济于事。

You need to place an handler for the image.onload slot and trigger a redraw when that code is called. 您需要为image.onload插槽放置一个处理程序,并在调用该代码时触发重绘。

The normal procedure is 正常的程序是

  1. create the image object 创建图像对象
  2. register the onload event for it 为它注册onload事件
  3. set up the image src value 设置图像src

only when the registered event is called you can use the image object. 只有在调用已注册的事件时,您才能使用图像对象。

The tricky part is that, depending on the browser, the image may become instantly valid after setting src if it's already in the cache, so when you don't follow the correct procedure things may apparently work anyway (but they will not work in real cases when loading requires internet access). 棘手的部分是,根据浏览器的不同,图像可能会在设置src之后立即生效,如果它已经在缓存中,那么当你不遵循正确的程序时,事情显然可能会有效(但它们不会在真实中工作)加载时需要上网的情况)。

You have two main problems that I can see: 你有两个主要问题我可以看到:

  1. You are accessing document.body before it is defined. 您在定义之前访问document.body
  2. You are using the image possibly before it is loaded. 您在加载之前可能正在使用该图像。

Here is code that solves both these problems: 这是解决这两个问题的代码:

// variables are defined as global variables
var posX = 0;
var posY = 0;
var tileSize = 32;
var mapArray;
var canvas;
var ctx;
var rockTile;

$(function() {
    // document should be defined now
    canvas = document.createElement("canvas");
    ctx = canvas.getContext("2d");
    canvas.width = (32 * 12);
    canvas.height = (32 * 12);
    document.body.appendChild(canvas);

    mapArray = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ];

    // wait until the image is loaded to draw
    rockTile = new Image();
    rockTile.onload = drawMap;
    rockTile.src = "../Images/dc-dngn/floor/rect_gray0.png";        
});

function drawMap(){
    posX = 0;
    posY = 0;
    for(var x = 0; x < mapArray.length; x++){
        for(var y = 0; y < mapArray[x].length; y++){
            if(mapArray[x][y] == 0){
                ctx.drawImage(rockTile, posX, posY, tileSize, tileSize);
            }
            posX += 32;
        }
        posX = 0;
        posY += 32;
    }
}

Also be sure to double check your image path. 另外一定要仔细检查你的图像路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM