PixelWarrior 40x30 pixel EGA Action Adventure

Hi,

One thing that could be improved in the movement of the player is that when you press two keys at once, the player should do what the last key pressed did, rather than continuing to honour the old key press.

Here’s a class I used to take that into account, maybe it will be useful (This may help with your other problem too?):

import java.io.;
import java.util.
;
import java.awt.event.*;

public class PlayersCurrentDirection{

// If empty, the player will not be trying to move
// The last pressed keys are first.
ArrayListSS<Integer> directionsPressed;	
int direction;

public PlayersCurrentDirection(){
	direction = DOWN;
	directionsPressed = new ArrayListSS<Integer>();
}

public void stopMoving(){
	directionsPressed.clear();
}

// this should really be called isTryingToMove, since it doesn't return whether or not the player is actually moving.
public boolean isMoving(){
	return (directionsPressed.size() > 0 ? true : false);
}
public int getDirection(){
	return direction;
}

public void applyKeyEvent(PlayerKeyEvent e){
	if (e.getKeyEventType() == e.KEY_PRESS){
		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A){
			addDirection(LEFT);
		}else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D){
			addDirection(RIGHT);
		}else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W){
			addDirection(UP);
		}else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S){
			addDirection(DOWN);
		}
	}else if (e.getKeyEventType() == e.KEY_RELEASE){
		// Here we remove the key from directionsPressed if it is present.
		// Note that we must remove all occurences, since there may be more 
		// than one of the same key press in the list if something weird happened.
		if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A){
			removeDirection(LEFT);
		}else if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D){
			removeDirection(RIGHT);
		}else if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W){
			removeDirection(UP);
		}else if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S){
			removeDirection(DOWN);
		}
	}
}
protected void addDirection(int dir){
	if (directionsPressed.size() == 0 || directionsPressed.get(0).intValue() != dir){
		directionsPressed.add(dir);
		direction = dir;
	}
}
protected void removeDirection(int dir){
	for (int i = 0; i < directionsPressed.size(); i++){
		if (directionsPressed.get(i).intValue() == dir){
			directionsPressed.remove(i);
			i--;
		}
	}
	if (directionsPressed.size() != 0){
		direction = directionsPressed.get(0);
	}
}

}

PS: here’s Bleb’s cool sound synthesizer which seems similar to yours:

http://www.java-gaming.org/index.php/topic,18087.0.html

Ok, got the key thing sorted, new version up. This is probably the ugliest hack I have ever done but basically after any keyPressed/keyReleased events I keep track of the key state according to AWT and start a Timer with a 5ms delay. The timer actionEvent then compares the AWT key states with it’s own set of key states and issues it’s own events based on the differences. This seams to fix the Linux press/release key repeat issue as the press/release events appear to go into the event queue in pairs and when the timer goes off I have never had a key that was held down in the up state. Oddly I tried an SwingUtilities.invokeLater() to do a similar thing but I was getting keys up occasionally so I am not sure what’s up with that.

Yeah, I saw that a while back. Quite nice but could really use a demo applet or at least a webstart link.

I don’t think that class is quite necessary, all you need to do is set the direction to the most current key pressed and only stop if the released key matches the current player direction.

I don’t think that class is quite necessary, all you need to do is set the direction to the most current key
pressed and only stop if the released key matches the current player direction.

The usual way to do that is simpler than that.

dx=right-left
dy=down-up

Ye… that’s all. (Directions are int flags where 0=not pressed and 1=pressed.)

Opposing directions cancel each other out and everything else just works.

My little bro gave me the thumbs up when I implemented this method, since it meant that he could press down while pressing up, and the player would change direction by going down. Then when he released down, the player would head back up since he never released the up key.

It does feel nice when the game remembers what keys you’ve got down, but obviously the added complexity might not be worth it. each to his own :wink:

Also, often key presses are streamed continuously even when multiple keys are down, so doing whatever the latest key press was isn’t necessarily the best option.