0
createjs: implement keyboard input from external script
Contributor
,
/t5/animate-discussions/createjs-implement-keyboard-input-from-external-script/td-p/10648588
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/animate-discussions/createjs-implement-keyboard-input-from-external-script/m-p/10648622#M197004
Oct 03, 2019
Oct 03, 2019
Copy link to clipboard
Copied
Actions pane, left side, Global, Include Script.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
bhnh
AUTHOR
Contributor
,
/t5/animate-discussions/createjs-implement-keyboard-input-from-external-script/m-p/10650219#M197020
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.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
bhnh
AUTHOR
Contributor
,
LATEST
/t5/animate-discussions/createjs-implement-keyboard-input-from-external-script/m-p/10662177#M197186
Oct 10, 2019
Oct 10, 2019
Copy link to clipboard
Copied
Found the solution. In the "game.js" class file, use document.onkeydown and document.onkeyup.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

