2011-01-23 12 views
1

申し訳ありません、私はいくつかのjsをロードする最後のページを取得しようとしています。私はそれを得たと思うが、今では私は、この行で番号が届かないと言っています:私のjavascriptサンプルにはいくつか問題があります。助けてください

parseInt(document.getElementById('canvas').style.width); 

それは「480PX」でなければなりませんが、それはNaNのです。

また、document.getElementById('ss')という文字列がnullであるとも言います。ここで

は完全なことだ:

<!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> 

答えて

3

あなたの代わりにスタイルプロパティの要素のプロパティを使用する必要があります幅を取得するには:

document.getElementById('canvas').offsetWidth 
+0

これはうまくいきました。どうもありがとうございました! – CyanPrime

2

あなたが定義した「幅」と「高さ」プロパティキャンバスのスタイルプロパティではありません。キャンバスオブジェクトの属性です。表示の高さと幅ではなく、キャンバスの座標系の幅と高さを定義します。

キャンバスは親コンテナ(480pxおよび320px)を塗りつぶします。

$("#canvas").width(); 
0

あなたのコードがありますmVChrが述べたように、あなたは、またはクロスブラウザの方法であなたのためにそれを計算しますjQueryのようなライブラリを使用して(クロスブラウザの警告付き)「offsetWidth」を介してこれを得ることができますいくつかの問題。まず、幅を取得しているこれらの線がNaN結果の原因になります。

  screenWidth = parseInt(document.getElementById('canvas').style.width); 
      screenHeight = parseInt(document.getElementById('canvas').style.height); 

widthおよびheightプロパティは、それぞれ文字列値480pxおよび320pxを返します。これらは文字列値なので、480pxは数値ではないため、parseInt呼び出しはNaNを返します。私はmVCrとuser85461推奨としてoffsetWidthに切り替えることをお勧めしたいと思います。

そして、document.getElementById( 'ss')行に問題はありません。私がそれを走らせると、クリックイベントがセットアップされ、正しく発射されました。