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.
Post Reply
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

JS, keydown addEventListener not working?

Post 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);
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: JS, keydown addEventListener not working?

Post by Jackolantern »

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
Sim
Posts: 412
Joined: Sat Dec 26, 2009 5:37 pm

Re: JS, keydown addEventListener not working?

Post 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.
oRPG Creator - Make Your Own Browser Game
oRPG Creator on Facebook
Post Reply

Return to “Beginner Help and Support”