在论坛中有几个问题,但我无法得到我需要的好答案.
我正在进入Canvas和Javascript,我想在游戏打开后立即预加载一些图像.我做了一个我想要构建的方法的例子(并没有工作)
我有3个文件:"main.html",其中画布(在这个例子中只有一个)被声明,我尝试加载图像,"ImagePreloader.js"我预加载所有图像和"Variables.js"在哪里我有我的变数.
任何人都可以请这个图像预加载metod,或建议我一个可行的?我认为图像没有加载,因为我没有使用onLoad()函数,我在目前为止阅读的教程中无法理解(我知道如何将其应用于图像,但不是图像数组)
main.html中:
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script type="text/javascript" src="Variables.js"></script>
<script type="text/javascript" src="ImagePreloader.js"></script>
<script>
// Basic canvas info
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// Draw one of the images. Somehow it doesn't work :(
context.drawImage(imageArray[3], x, y, width, height);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
ImagePreloader.js
// This should preload the images in imageArray[i] with 0 <= i <= 5 ... right?
function preloader()
{
for(var i=0; i<5; i++)
{
imageArray[i].src=images[i];
}
}
Run Code Online (Sandbox Code Playgroud)
Variables.js …