11 years ago

Prevent page scroll for HTML5 games


There is quite a common and irritating problem when playing HTML5/Javascript games on Gamejolt. If you control a character with the arrow keys or use the SPACE key, the game page will scroll up or down as soon as you use any of those keys. There are some game engines that already prevent this issue, but in most of the cases this is still a problem that should not be overlooked, because it can make your game unplayable. :(

I faced the same issues while working on Strider. Strider is a javascript game and to fix that I’ve used the following routines:

(function ItsATrap() {

		
			window.addEventListener('keydown', function(e){

    switch(e.keyCode){

    case 32: 

    case 37: 

    case 38: 

    case 39: 

    case 40:

        e.preventDefault(); 

        break;

    default: 

        break;

    }

}, false);
		
	

}());

This should prevent the arrow keys and space bar key press events to be transmitted to your page when a KeyboardEvent occurs.



0 comments

Loading...

Next up

Did you know that today is #InternationalJazzDay? 🎷😎

No? 😯 Well, we couldn't pass up this date!

And what better to celebrate this day than #Pecaminosa #OST? 🎷

Let us know what you think about it and have a great weekend!

Smile! Here, take some happy pills! ✨💊 #Blender #3DModelling #3DArt Buy me a Ko-fi: https://ko-fi.com/barbarafb_

Here's a peaceful little forest scene for this #screenshotsaturday, from the beginning of the game.

What you all think

Shuiro Haname. #Commission

Commission for @ShuHaname

Regular exercise is key for healthy wings.

Try it out!

https://gamejolt.com/games/AeonNightmares/605184

Coming Soon...

Colorful Fox

Happy Late Easter!

Aston Martin Vantage 2019

Day 4, Achieved

#AstonMartin #FastChallenge #speedmodeling

I remade my main character, what do you think? :)