This question already has an answer here:
Hi I'm working on a simple checkers and I have a problem with checking if the player clicked on the enemy. When player moves it seems as if the condition if(valid_position = true) is always true so the player can move to the position of an enemy. It might be a problem with for-loop setting it to true.
int mouse_x;
int mouse_y;
int selectedI;
boolean valid_position = true;
ArrayList<Integer> playerX = new ArrayList<>();
ArrayList<Integer> playerY = new ArrayList<>();
ArrayList<Integer> kingX = new ArrayList<>();
ArrayList<Integer> kingY = new ArrayList<>();
ArrayList<Integer> enemyX = new ArrayList<>();
ArrayList<Integer> enemyY = new ArrayList<>();
public void mousePressed(MouseEvent e) {
mouse_x = (e.getX()-3)/64;
mouse_y = (e.getY()-23)/64;
if(player_move = true){
for (int i = 0; i < enemyX.size(); i++){
if((enemyX.get(i) == mouse_x)&&(enemyY.get(i) == mouse_y)){
valid_position = false;
System.out.println("enemy");
break;
}
}
for (int i = 0; i < playerX.size(); i++){
if((playerX.get(i) == mouse_x)&&(playerY.get(i) == mouse_y)){
selectedI = i;
valid_position = true;
System.out.println("player");
break;
}
}
if(valid_position = true){
if((mouse_x == playerX.get(selectedI)-1 && mouse_y == playerY.get(selectedI)-1) || (mouse_x == playerX.get(selectedI)+1 && mouse_y == playerY.get(selectedI)-1)){
if(mouse_y == 0){
kingX.add(mouse_x);
kingY.add(mouse_y);
playerX.remove(selectedI);
playerY.remove(selectedI);
}else{
playerX.set(selectedI,mouse_x);
playerY.set(selectedI,mouse_y);
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire