如何使这样的事情起作用?我真的是编程新手,不知道如何让我的库存可见?
收到此错误: 未捕获的类型错误:无法读取未定义图像的属性“img”。(匿名函数).onload
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var image = {};
function loadImage(name) {
image[name] = new Image();
image[name].onload = function () {
//resourceLoaded();
};
image[name].src = "images/" + name;
}
var items = {
knife: {
name: "Knife",
img: "knife.png"
},
sword: {
name: "Sword",
img: "sword.png"
}
};
var inventory = []; //empty inventory
inventory.push(items.knife); //pickup a knife
for (var i = 0, len = inventory.length; i < len; i++) {
loadImage(inventory[i].img);
image[inventory[i].img].onload = function() {
ctx.drawImage(image[inventory[i].img], 0, 0);
}
}
尝试了很多东西,但无法理解比这更好的东西:(因为我是编程新手