Sign in to follow this  
Brash Benson

[web] Flash: keyDown issues with multiple keys

Recommended Posts

I am currently writing an Asteroids type game, but I am having issues with what I think are the keyDown events. In short, if you press and release single keys at a time (up to move forward, left to rotate left, etc), everything works great. But when you press more than one key at a time (up and left), when it comes time to do the keyUp event, it only registers one keyUp. So your ship is either stuck rotating or thrusting until you press and release the corresponding key one more time. Here is some of the code which I hope helps:

shipInterval = setInterval(shipExecute, playerTick);

function shipKeyDown(){
		else if(k==Key.LEFT||k==72)ship.left=true;
		else if(k==Key.RIGHT||k==74)ship.right=true;
		else if(k==Key.UP||k==85)ship.up=true;
	else if(k==Key.SHIFT)toggleShield();

function shipKeyUp(){
		else if(k==Key.LEFT||k==72)ship.left=false;
		else if(k==Key.RIGHT||k==74)ship.right=false;
		else if(k==Key.UP||k==85)ship.up=false;

function shipExecute(){
	if(!ship.down && ship.up) {
		ship._x += shipSpeed * Math.sin(d2r(ship._rotation));
		ship._y -= shipSpeed * Math.cos(d2r(ship._rotation));
	else if (ship.down && !ship.up) {
		ship._x -= (shipSpeed/4) * Math.sin(d2r(ship._rotation));
		ship._y += (shipSpeed/4) * Math.cos(d2r(ship._rotation));

function d2r(d) {
	return d*Math.PI/180;

I set up an interval to update the ship's movement every 40 milliseconds, if that helps clear anything up. What's frustrating is that I'm doing this project out of a book, but the book fails to mention that the controls are borked. I guess the author only wants you to press one key at a time. Any help would be greatly appreciated!

Share this post

Link to post
Share on other sites
Well, I ended up simply converting the whole thing into a bunch of these:

ship._rotation += shipRotationSpeed;

using individual Key.isDown conditionals solved the problem, but if anybody knows whether or not the original method could work I would like to know. Also, if you know of any benefits to using either method, please let me know. Thanks! :)

Share this post

Link to post
Share on other sites
The main problem is with how your code is set up to begin with. If you haven't done so already I would recomend a general programming book that works with general programming terms before you dive into any language programming or scripting.

The way a if statement works visually is like this


Then, else, another else

next process

(crappy picture didn't quite work out when formating the post but you get the idea)

As you can see from that crappy little picture I made you have your If statement going off, from there it can only take one path, it should goto your Then if possible, if that is not possible goto your first else that is true, and if that isn't possible goto your next else that is true, and so on. After it goes to one of these the process does whatever the statement says and the next process in the program is done.

The problem is you are trying to make it take multipule paths from a single If statement. There are a few solutions to your problem, the best one being just to break it down into multipule If Then statements like you said you did, just thought I would point out why the other method you were trying wasn't working.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this