日期:2014-05-16 浏览次数:20794 次


function itemLoaded(item){
if (g_context == undefined)
{
if ( !initCanvas() )//加载完第一张图片后初始化canvas
{
dbg("Critical error initializing canvas.", false);
return;
}
}
if (item.id == "splash_screen")//画背景
{
g_context.drawImage(item,0,0);
g_context.strokeRect(35,220, 500,40);
}
else if (item.id == "loading")//画loading..图片
{
g_context.drawImage(item,0,0);
g_context.fillStyle = "black";
g_context.fillRect(400,300, 300,30);
g_context.fillStyle = "green";
g_context.fillText("Loading game sounds", 400,320);
g_soundsLoaded= false;
// loadGameSounds();
}
g_itemsLoaded++;/当前加载完的图片数
alert(g_itemsLoaded);
g_context.fillStyle = "black";
g_context.fillRect(400,300, 300,30);
g_context.fillStyle = "green";
g_context.fillText(item.id, 400,320);
var percent = g_itemsLoaded / g_totalItems;
var width = Math.floor(percent * 500);
g_context.fillRect(35,220, width,40);//画进度条
}
仔细看一下下面这个HTML文件,就明白了。
<!DOCTYPE HTML>
<title>Canvas 游戏 </title>
<script type="text/javascript">
var g_canvas;
var g_context;
//var g_soundsLoaded;
// var g_onscreenControls;
var g_totalItems;
var g_itemsLoaded;
//这个函数在Loader.js中,这里把它提出来了。
function itemLoaded(item){
if (g_context == undefined)
{
if ( !initCanvas() )
{
dbg("Critical error initializing canvas.", false);
return;
}
}
if (item.id == "splash_screen")
{
g_context.drawImage(item,0,0);
g_context.strokeRect(35,220, 500,40);
}
else if (item.id == "loading")
{
g_context.drawImage(item,0,0);
g_context.fillStyle = "black";
g_context.fillRect(400,300, 300,30);
g_context.fillStyle = "green";
g_context.fillText("Loading game sounds", 400,320);
//g_soundsLoaded= false;
// loadGameSounds();
}
g_itemsLoaded++;
alert(g_itemsLoaded);
g_context.fillStyle = "black";
g_context.fillRect(400,300, 300,30);
g_context.fillStyle = "green";
g_context.fillText(item.id, 400,320);
var percent = g_itemsLoaded / g_totalItems;
var width = Math.floor(percent * 500);
g_context.fillRect(35,220, width,40);
}
//这个函数在Loader.js中,这里把它提出来了。
function initCanvas()
{
g_canvas = document.getElementById('theCanvas');
if (!g_canvas.getContext)
{
return false;
}
g_context = g_canvas.getContext('2d');
g_context.font = "14pt Helvetica";
g_context.lineWidth = 2;
g_context.strokeStyle = "green";
g_totalItems = 46;
g_itemsLoaded = 0;
// g_onscreenControls = false;
return true;
}
</script>
</head>
<body>
<table cellpadding="0" border="0" cellspacing="0" frame="void">
<tr>
<td valign="top">
<canvas align="left" id="theCanvas" width="600" height="400"></canvas>
</td>
<tr>
<table>
<div id="dbg"></div>
<div id="hidden" style="visibility:hidden; width:1px; height:1px; overflow:hidden">
<img id="splash_screen" src="splash_screen.jpg" onload="itemLo