我想提出一個麻將遊戲在JS,我有在畫布上的一個問題加載圖像無法加載在畫布上的圖像爲我的js遊戲
帆布
// Create the canvas var canvas = document.createElement("canvas"); var ctx = canvas.getContext("2d"); canvas.width = 512; canvas.height = 512; //draw canvas document.body.appendChild(canvas);
陣列
//Array with Tiles & set length to 144 which are the requested tiles var tiles = new Array(2);//will be 144 //2D map array for showing the images coordinates var map = [[69,50],[100,150]];//will be 144 coordinates
我的功能更新()
function update(){ for(var i=0;i<tiles.length;i++){ //make the tile object tiles[i] = new Object(); tiles[i].id = i ; tiles[i].selected = false; //set the coordinates from map Array tiles[i].x=map[i][0]; tiles[i].y=map[i][1]; //tiles[i].ready=false; //These are for the image location works fine //convert i to String var sourceNumber = i.toString(); //add .png to String var source = sourceNumber.concat(png); //add /image var source = dest.concat(source); tiles[i].img = new Image(); tiles[i].img.onload = function(){ //tiles[i].ready=true; ctx.drawImage(tiles[i].img,tiles[i].x,tiles[i].y,xdimension,ydimension); }; tiles[i].img.src = source ; } }
我拼命地跑它在每個我的瀏覽器就不會加載圖像,我調試對鉻和它說ctx.drawImage(...); - >遺漏的類型錯誤:無法讀取的不確定(反覆多次)屬性「IMG」,所以我嘗試了磚[I]。就緒和負載的圖像後,但仍然有上error.Any建議我應該如何實現平鋪圖像的加載