JS, keydown addEventListener not working?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.

JS, keydown addEventListener not working?

Postby Sim » Fri Oct 10, 2014 9:32 am

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);
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
 
Posts: 412
Joined: Sat Dec 26, 2009 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)

Re: JS, keydown addEventListener not working?

Postby Jackolantern » Sun Oct 12, 2014 2:12 pm

Can you post the code that includes the listeners? We really need to see where they are and how they are used.
The indelible lord of tl;dr
User avatar
Jackolantern
 
Posts: 10893
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: JS, keydown addEventListener not working?

Postby Sim » Sun Oct 12, 2014 8:16 pm

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.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Sim
 
Posts: 412
Joined: Sat Dec 26, 2009 12:37 pm
Location: Baltimore
Has thanked: 39 times
Been thanked: 13 times
Blog: View Blog (1)


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x