好的,所以我試圖讓頁面在加載結束時加載一些js。我想我得到了它,但現在它說我沒有收到一些與這一行:我的JavaScript示例有一些問題。請幫助
parseInt(document.getElementById('canvas').style.width);
它應該是「480像素」,但它是爲NaN。
它也表示線document.getElementById('ss')
爲空。
下面是完整的東西:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Cypri Designs</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/images/favicon1.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
<!--
//Variables to handle game parameters
var gameloopId;
var screenWidth;
var screenHeight;
var gameRunning = false;
var ctx;
var playerX = 0;
var playerY = 0;
//Create images
var playerImg = new Image();
//Wait for DOM to load and init game
window.onload = function(){
init();
};
function init(){
initSettings();
initImages();
//add event handler to surrounding DIV to monitor mouse move and update mushroom's x position
/*$("#container").mousemove(function(e){
mushroomX = e.pageX;
});*/
//add event handler for clicking on start/stop button and toggle the game play
document.getElementById('ss').onclick = function(){
toggleGameplay();
};
}
function initSettings()
{
//Get a handle to the 2d context of the canvas
ctx = document.getElementById('canvas').getContext('2d');
//Calulate screen height and width
screenWidth = parseInt(document.getElementById('canvas').style.width);
screenHeight = parseInt(document.getElementById('canvas').style.height);
playerX = 100;
playerY = 100;
}
function initImages(){
playerImg.src = "images/player.png";
}
//Main game loop, it all happens here!
function gameLoop(){
//Clear the screen (i.e. a draw a clear rectangle the size of the screen)
ctx.clearRect(0, 0, screenWidth, screenHeight);
ctx.save();
ctx.drawImage(playerImg, 0, 0);
ctx.restore();
}
//Start/stop the game loop (and more importantly that annoying boinging!)
function toggleGameplay()
{
gameRunning = !gameRunning;
if(gameRunning)
{
clearInterval(gameloopId);
gameloopId = setInterval(gameLoop, 10);
}
else
{
clearInterval(gameloopId);
}
}
//-->
</script>
</head>
<body>
<input id="ss" type="button" value="start/stop" />
<div id="container" style="border:1px solid; cursor:none; width:480px; height:320px;">
<canvas id="canvas" width="480px" height="320px" >
Canvas not displaying.
</canvas>
</div>
</body>
</html>
工作。非常感謝你! – CyanPrime 2011-01-23 06:18:38