Page 1 of 1

JS, keydown addEventListener not working?

PostPosted: Fri Oct 10, 2014 9:32 am
by Sim
So I converted my little block game code to a class and now I can't seem to get the keyDown to work with my method in the eventListener.

http://orpgcreator.com/3blocks/newBlock.html

I have this in my class
HTML
Code: Select all
  <body>
    <canvas id="myCanvas1" width="320" height="320"></canvas>
    <canvas id="myCanvas2" width="320" height="320"></canvas>
    <script>
      var canvas1 = document.getElementById('myCanvas1');
      var context1 = canvas1.getContext('2d');
      var canvas2 = document.getElementById('myCanvas2');
      var context2 = canvas2.getContext('2d');

    </script>
<script src="3blocks_class.js"></script>
<script src="3blocks_game.js"></script>

  </body>



3blocks_game.js
Code: Select all
var Player1 = new BlockGame("", canvas1, context1);
var Player2 = new BlockGame("", canvas2, context2);


setTimeout(function() {
game_loop();
}, 1000);


window.requestAnimFrame = (function(callback)
{
   return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
   function(callback)
   {
      window.setTimeout(callback, 1000 / 60);
   };
})();


function game_loop()
{

   //RUN GAME for players
   Player1.execu();
   Player2.execu();

   // request new frame
   requestAnimFrame(function()
   {
     game_loop();
   });
}


//I have this in my 3blocks_class.js constructor
Code: Select all
window.addEventListener("keydown", this.moveBlock);


I also moved the eventListener into 3blocks_game.js to see if it would work that way and still no luck
Code: Select all
window.addEventListener("keydown", Player1.moveBlock);

Re: JS, keydown addEventListener not working?

PostPosted: Sun Oct 12, 2014 2:12 pm
by Jackolantern
Can you post the code that includes the listeners? We really need to see where they are and how they are used.

Re: JS, keydown addEventListener not working?

PostPosted: Sun Oct 12, 2014 8:16 pm
by Sim
Jackolantern wrote:Can you post the code that includes the listeners? We really need to see where they are and how they are used.



This is now resolved. I had to bind it.