2014-04-04 31 views
-1

我一直在簡單的畫布上顯示一個精靈表。爲了調試目的,我從主代碼中取出了代碼,這樣我就可以只處理一個小文件。精靈表加載在畫布旁邊,但畫布不顯示精靈本身。 任何幫助,非常感謝。由於Javascript SpriteSheet不工作

<canvas id="canvas" width="800" height="36" style="border:1px solid" onload="draw_hello()" ></canvas> 

<img id="helloSprites" src="Tux-Hello.png"> 

<script> 
//hello sprite 
var x=0; 
var y=0; 
var f=0; 
var w=32; 
var h=32; 
var mycanv=document.getElementById("canvas"); 
var ctx=mycanv.getContext("2d"); 

//helloImage.src = "images/Tux-Hello.png"; 
//var helloImage = new Image(); 


var hello=document.getElementById("helloSprites"); 
function draw_hello() { 
    setInterval(drawHello,200); 
} 
function drawHello() { 
     ctx.fillStyle="#292"; 
     ctx.fillRect(0,0,mycanv.width, mycanv.height); 
     ctx.fillStyle="#aaa"; 
     ctx.fillRect(0,25,mycanv.width, mycanv.height); 
    if (x>=(mycanv.width)) { 
     x=0; 
    } 
    x+=2; 
     f++; 
    // draw the car 
     if (f==2) { 
      f=4; 
     } 
     if (f==6) { 
      f=0; 
     } 

    ctx.drawImage(hello, w, f*h, w, h, x, y, w, h); 
} 

</script> 

回答

0

附上您的onloadbody元素,而不是canvas元素。

您可能正在調用onload - 因此在img元素完全加載之前調用draw_hello()。