C
caleb
I'm making a Mario like side scroller game and I having some trouble
with input. I can only hit one key at once, if I try two it doesn't
register. I would like to be able to run and jump at the same time.
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.moveEast();
}
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.moveWest();
}
}
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.jump();
}
}
}
This is an example of the kind of thing I'm trying to do. Also I've
been trying to figure out how to have the camera center on the
player... so the player's pixels aren't actually moving, but rather the
background. I assume I should make the input keys move the background
while the character is only being animated but I haven't started coding
it yet. Any source code from similar games would be very helpful.
Thanks in advance.
with input. I can only hit one key at once, if I try two it doesn't
register. I would like to be able to run and jump at the same time.
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.moveEast();
}
}
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.moveWest();
}
}
if(e.getKeyCode() == KeyEvent.VK_SPACE) {
for(int i=0; i<player.size(); i++) {
Player p = (Player) player.elementAt(i);
p.jump();
}
}
}
This is an example of the kind of thing I'm trying to do. Also I've
been trying to figure out how to have the camera center on the
player... so the player's pixels aren't actually moving, but rather the
background. I assume I should make the input keys move the background
while the character is only being animated but I haven't started coding
it yet. Any source code from similar games would be very helpful.
Thanks in advance.