I'm trying to make 2 players Mario game for my school project but whenever Mario 1 jumping or go first, Mario 2 will stay still and move if i press multiple times. https://drive.google.com/file/d/1ATG3h_Om849_HBEaWiEIdADIC-SQ_dF7/view?usp=sharing - My Source if anyone wants to look for more specific details. this is my KeyBind class and my Main when they receive input
package manager;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.function.Supplier;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class KeyBindingManager {
private GameEngine engine;
private InputMap inputMap;
private ActionMap actionMap;
public KeyBindingManager(GameEngine engine, JFrame frame) {
this.engine = engine;
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
this.inputMap = frame.getRootPane().getInputMap(condition);
this.actionMap = frame.getRootPane().getActionMap();
init();
}
private void init() {
this.bindKey(KeyEvent.VK_W, "VK_W_Action", () -> {
GameStatus status = this.engine.getGameStatus();
if (status == GameStatus.START_SCREEN || status == GameStatus.MAP_SELECTION)
return ButtonAction.GO_UP;
else {
engine.receiveInputMario(ButtonAction.M_JUMP);
return null;
}
});
this.bindKey(KeyEvent.VK_S, "VK_S_Action", () -> {
GameStatus status = this.engine.getGameStatus();
if (status == GameStatus.START_SCREEN || status == GameStatus.MAP_SELECTION)
return ButtonAction.GO_DOWN;
return null;
});
this.bindKey(KeyEvent.VK_D, "VK_D_Action", () -> {
engine.receiveInputMario(ButtonAction.M_RIGHT);
return null;
});
this.bindKey(KeyEvent.VK_A, "VK_A_Action", () -> {
engine.receiveInputMario(ButtonAction.M_LEFT);
return null;
});
this.bindKey(KeyEvent.VK_ENTER, "VK_ENTER_Action", () -> {
return ButtonAction.SELECT;
});
this.bindKey(KeyEvent.VK_ESCAPE, "VK_ESCAPE_Action", () -> {
GameStatus status = this.engine.getGameStatus();
if (status == GameStatus.RUNNING || status == GameStatus.PAUSED)
return ButtonAction.PAUSE_RESUME;
else
return ButtonAction.GO_TO_START_SCREEN;
});
this.bindKey(KeyEvent.VK_SPACE, "VK_SPACE_Action", () -> {
engine.receiveInputMario(ButtonAction.M_FIRE);
return null;
});
this.bindKey(KeyEvent.VK_UP, "VK_UP_Action", () -> {
engine.receiveInputMario2(ButtonAction.M_JUMP);
return null;
});
this.bindKey(KeyEvent.VK_RIGHT, "VK_RIGHT_Action", () -> {
engine.receiveInputMario2(ButtonAction.M_RIGHT);
return null;
});
this.bindKey(KeyEvent.VK_LEFT, "VK_LEFT_Action", () -> {
engine.receiveInputMario2(ButtonAction.M_LEFT);
return null;
});
this.bindKey(KeyEvent.VK_0, "VK_P_Action", () -> {
engine.receiveInputMario2(ButtonAction.M_FIRE);
return null;
});
}
private void bindKey(int keyEvent, String actionName, Supplier<ButtonAction> action) {
this.inputMap.put(KeyStroke.getKeyStroke(keyEvent, 0, false), actionName + "_Press");
this.actionMap.put(actionName + "_Press", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
ButtonAction currentAction = ButtonAction.NO_ACTION;
currentAction = action.get();
if (currentAction != null) {
notifyInput(currentAction);
}
};
});
this.inputMap.put(KeyStroke.getKeyStroke(keyEvent, 0, true), actionName + "_Release");
this.actionMap.put(actionName + "_Release", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (keyEvent == KeyEvent.VK_D || keyEvent == KeyEvent.VK_A || keyEvent == KeyEvent.VK_W || keyEvent == KeyEvent.VK_SPACE) {
engine.receiveInputMario(ButtonAction.ACTION_COMPLETED);
}
if (keyEvent == KeyEvent.VK_RIGHT || keyEvent == KeyEvent.VK_LEFT || keyEvent == KeyEvent.VK_UP || keyEvent == KeyEvent.VK_0 ) {
engine.receiveInputMario2(ButtonAction.ACTION_COMPLETED);
}
};
});
}
private void notifyInput(ButtonAction action) {
if (action != ButtonAction.NO_ACTION)
engine.receiveInput(action);
}
}
public void receiveInputMario(ButtonAction input) {
if (gameStatus != GameStatus.RUNNING)
return;
Mario mario = mapManager.getMario("mario");
if (input == ButtonAction.M_JUMP) {
mario.jump(this);
} else if (input == ButtonAction.M_RIGHT) {
mario.move(true, camera);
} else if (input == ButtonAction.M_LEFT) {
mario.move(false, camera);
} else if (input == ButtonAction.ACTION_COMPLETED) {
mario.setVelX(0);
} else if (input == ButtonAction.M_FIRE) {
mapManager.fire(this);
}
}
public void receiveInputMario2(ButtonAction input) {
if (gameStatus != GameStatus.RUNNING)
return;
Mario mario2 = mapManager.getMario("mario2");
if (input == ButtonAction.M_JUMP) {
mario2.jump(this);
} else if (input == ButtonAction.M_RIGHT) {
mario2.move(true, camera);
} else if (input == ButtonAction.M_LEFT) {
mario2.move(false, camera);
} else if (input == ButtonAction.ACTION_COMPLETED) {
mario2.setVelX(0);
} else if (input == ButtonAction.M_FIRE) {
mapManager.fire(this);
}
}
public void receiveInput(ButtonAction input) {
if (gameStatus == GameStatus.START_SCREEN) {
if (input == ButtonAction.SELECT && startScreenSelection == StartScreenSelection.START_GAME) {
startGame();
} else if (input == ButtonAction.SELECT && startScreenSelection == StartScreenSelection.VIEW_ABOUT) {
setGameStatus(GameStatus.ABOUT_SCREEN);
} else if (input == ButtonAction.SELECT && startScreenSelection == StartScreenSelection.VIEW_HELP) {
setGameStatus(GameStatus.HELP_SCREEN);
} else if (input == ButtonAction.SELECT && startScreenSelection == StartScreenSelection.GAME_SCORE) {
setGameStatus(GameStatus.GAME_SCORE);
} else if (input == ButtonAction.GO_UP) {
selectOption(true);
} else if (input == ButtonAction.GO_DOWN) {
selectOption(false);
}
} else if (gameStatus == GameStatus.MAP_SELECTION) {
if (input == ButtonAction.SELECT) {
selectMapViaKeyboard();
} else if (input == ButtonAction.GO_UP) {
changeSelectedMap(true);
} else if (input == ButtonAction.GO_DOWN) {
changeSelectedMap(false);
}
} else if (gameStatus == GameStatus.RUNNING) {
if (input == ButtonAction.PAUSE_RESUME) {
pauseGame();
}
} else if (gameStatus == GameStatus.PAUSED) {
if (input == ButtonAction.PAUSE_RESUME) {
pauseGame();
}
} else if (gameStatus == GameStatus.GAME_OVER && input == ButtonAction.GO_TO_START_SCREEN) {
reset();
} else if (gameStatus == GameStatus.MISSION_PASSED && input == ButtonAction.GO_TO_START_SCREEN) {
reset();
}
if (input == ButtonAction.GO_TO_START_SCREEN) {
setGameStatus(GameStatus.START_SCREEN);
}
}
I've try using chain of responsibility but it didn't work, i don't know what to do next. Help me please and i really appreciate it.