Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

createjs: implement keyboard input from external script

Contributor ,
Oct 03, 2019 Oct 03, 2019

I'm building a createjs game in Animate.  On frame 0 I have (in part) the following...

var goLeft = false;
var goRight = false;
var KEYCODE_LEFT=37,KEYCODE_RIGHT=39

window.onkeyup = keyUpHandler;
window.onkeydown = keyDownHandler;

function keyDownHandler(e){
	if(!e){
		var e = window.event;
	}
	switch(e.keyCode){
		case KEYCODE_LEFT: goLeft = true;break;
		case KEYCODE_RIGHT: goRight = true; break;
		case KEYCODE_UP: stage.swapChildren(gate, boarder);
	}
}

function keyUpHandler(e){
	if(!e){
		var e = window.event;
	}
	switch(e.keyCode){
		case KEYCODE_LEFT: goLeft = false;break;
		case KEYCODE_RIGHT: goRight = false; break;
	}
}

createjs.Ticker.addEventListener("tick", loopclip);
function loopclip(){
    if(goLeft) // something happens;
    if(goRight)  // the opposite happens;
}

 

This works just fine.  My question is, how do I move this to an external "game" js file?  Thanks.

440
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 03, 2019 Oct 03, 2019

Actions pane, left side, Global, Include Script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 04, 2019 Oct 04, 2019

Thanks for responding.  My basic question is, how do I write a keyboard function in an external "game.js" file?  I tried "window.onkeydown" and I got an error.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 10, 2019 Oct 10, 2019
LATEST
Found the solution. In the "game.js" class file, use document.onkeydown and document.onkeyup.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines