Highlighted

createjs: implement keyboard input from external script

Contributor ,
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

45

Likes

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

createjs: implement keyboard input from external script

Contributor ,
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

46

Likes

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
Oct 03, 2019 0
Adobe Community Professional ,
Oct 03, 2019

Copy link to clipboard

Copied

Actions pane, left side, Global, Include Script.

Likes

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
Reply
Loading...
Oct 03, 2019 0
Contributor ,
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.

Likes

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
Reply
Loading...
Oct 04, 2019 0
bhnh LATEST
Contributor ,
Oct 10, 2019

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Oct 10, 2019 0