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

createjs: implement keyboard input from external script

Contributor ,
Oct 03, 2019 Oct 03, 2019

Copy link to clipboard

Copied

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.

Views

365

Translate

Translate

Report

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

Copy link to clipboard

Copied

Actions pane, left side, Global, Include Script.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST
Found the solution. In the "game.js" class file, use document.onkeydown and document.onkeyup.

Votes

Translate

Translate

Report

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