我正在尝试研究如何将一些图像加载到javascript中以使用画布。我想首先我会采用JSON对象并处理数组的所有成员......
canvasAssets = [{name:"purple_guy", src:"sprites/purpleguy.png", type: "image"}];
function loadAssets(){
alert(canvasAssets); // THIS WORKS
//parse all assets
for(i=0;i<canvasAssets.length;i++){
alert(canvasAssets); //THIS WORKS
//image assets
if(cavasAssets[i].type == "image"){ //ERRORS HERE saying undefined
alert(canvasAssets);
name = cavasAssets[i].name;
imageAssets[name] = new Image();
imageAssets[name].onload = function(){
completedAssets++;
if(completedAssets = canvasAssets.length){
drawCanvas();
}
};
imageAssets[name].src = cavasAssets[i].src;
}
}
}
我目前遇到的问题是突然出现在if()块if(cavasAssets[i].type == "image")
中,它说我的变量未定义,我不知道为什么。
答案 0 :(得分:4)
您有一个错字cavasAssets
应为canvasAssets
。我会再次查看代码,我会看到更多代码name = cavasAssets[i].name
和imageAssets[name].src = cavasAssets[i].src;
只是做一个查找和替换,因为你一直在拼错它。