Problem with keypressed


#1

hello i want to make my “rect” moove when i press “LEFT” but with this code the rect is mooving even if nothin is pressed.
(i used keyreleased because i want to press many keys at the same time)
what’s wrong??:

int x =100;
int y =100;
int x2 = 1000;
int y2 =50;
boolean[] keys;

void setup() {
  size (1200, 600);
  background(245, 72, 72);
  keys=new boolean[1];
  keys[0]=true;
}

void draw() {  
  background(245, 72, 72); 
  Player player1 =  new Player(x, y);
  Player player2 = new Player(x2, y2);
  if (keys[0]=true) {
    x=x+1;
  }
  text(player1.x, 50, 50);
}



void keyPressed() {

  if (key==CODED) {
    if (keyCode==LEFT)
      keys[0]=false   ;
  }
}

void keyReleased() {

  if (key==CODED) {
    if (keyCode==LEFT)
      keys[0]=false;
  }
}


public class Player {
  int x;
  int y;
  public Player(int px, int py) {
    rect (px, py, 50, 100);              
    x = px;
    y = py;
  }
}

#2

Two problems:

  1. Both your keyPressed and keyReleased functions are setting keys[0] to false. One of them should set it to true!

  2. Your conditional check, if (keys[0]=true) { is an assignment, not a comparison! Try if (keys[0] == true) {, or even just if ( keys[0] ) {


#3

Oh thanks a lot x)
I think i just have eyes problems xD