Block does not move when button is pressed

float[] blockx= new float[9];
float[] blocky= new float[9];
int i=1;

void setup(){
  size(400,300);
  blockx[1]= 0;
  blocky[1]=0;
}
void draw(){
  if(keyPressed){
    if(key=='d'){
      blockx[i]=blockx[i]+100;}}
    
    
    
 rect(blockx[i], blocky[i], 100,100);
 strokeWeight(10);
 noFill();
 rect(0,0, 100,100);
 rect(100,0, 100,100);
 rect(200,0, 100,100);
 rect(300,0, 100,100);
 rect(400,0, 100,100);
 rect(0,100, 100,100);
 rect(100,100, 100,100);
 rect(200,100, 100,100);
 rect(300,100, 100,100);
 rect(400,100, 100,100);
 rect(0,200, 100,100);
 rect(100,200, 100,100);
 rect(200,200, 100,100);
 rect(300,200, 100,100);
 }

when i press d it should move the block but it’s not. why? and how do i fix this?

1 Like

At end of draw(); replace 100 with 2

Additionally place background at start of draw()

1 Like

thank you @Chrisir do you also happen to know why the block is not moving down here:

float[] blockx= new float[9];
float[] blocky= new float[9];
float timeneeded= 3000;
int i=1;

void setup() {
  size(400, 300);
  blockx[1]= 0;
  blocky[1]=0;
}
void draw() {
  background(127);
  
  if(timeneeded == millis()){
    blocky[i]=blocky[i]+100;
    timeneeded=timeneeded*2;}

  if (keyPressed&&blockx[i]<300) {
    if (key=='d') {
      blockx[i]=blockx[i]+100;
    }
  }
  
    if (keyPressed&&blockx[i]>0) {
    if (key=='a') {
      blockx[i]=blockx[i]-100;
    }
  }
  
  fill(255);
  rect(blockx[i], blocky[i], 100, 100);

  strokeWeight(10);
  noFill();
  rect(0, 0, 100, 100);
  rect(100, 0, 100, 100);
  rect(200, 0, 100, 100);
  rect(300, 0, 100, 100);
  
  rect(0, 100, 100, 100);
  rect(100, 100, 100, 100);
  rect(200, 100, 100, 100);
  rect(300, 100, 100, 100);
  
  rect(0, 200, 100, 100);
  rect(100, 200, 100, 100);
  rect(200, 200, 100, 100);
  rect(300, 200, 100, 100);
}

Replace == with < here

thank you, why do i get ArrayIndexOutOfBoundsException: 10?

float[] blockx= new float[10];
float[] blocky= new float[10];
float timeneeded= 3000;
int i=1;

void setup() {
  size(400, 300);
  blockx[1]= 0;
  blocky[1]=0;
}
void draw() {
  background(127);

  if (blocky[i]==200&& blockx[i] != 300) {
    i++;
  }
    if (blocky[i]==200&& blockx[i] == 300) {
    blockx[i]= 0;
    blocky[i]=0;
  }
  
  if(blockx[i]==blockx[1]&&blocky[i]==blocky[1]-100){
  i++;}
    if(blockx[i]==blockx[2]&&blocky[i]==blocky[2]-100){
  i++;}
    if(blockx[i]==blockx[3]&&blocky[i]==blocky[3]-100){
  i++;}
    if(blockx[i]==blockx[4]&&blocky[i]==blocky[4]-100){
  i++;}
    if(blockx[i]==blockx[5]&&blocky[i]==blocky[5]-100){
  i++;}
    if(blockx[i]==blockx[6]&&blocky[i]==blocky[6]-100){
  i++;}
    if(blockx[i]==blockx[7]&&blocky[i]==blocky[7]-100){
  i++;}
    if(blockx[i]==blockx[8]&&blocky[i]==blocky[8]-100){
  i++;}
    if(blockx[i]==blockx[9]&&blocky[i]==blocky[9]-100){
  i++;}


  if (frameCount%120 == 0) {
    blocky[i]=blocky[i]+100;
  }

  if (keyPressed&&blockx[i]<300) {
    if (key=='d') {
      blockx[i]=blockx[i]+100;
    }
  }
  
  if (keyPressed&&blockx[i]>0) {
    if (key=='a') {
      blockx[i]=blockx[i]-100;
    }
  }

  fill(255);

  rect(blockx[i], blocky[i], 100, 100);
  
 if(i==2){
   rect(blockx[1], blocky[1], 100, 100);
 }
 if(i==3){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
 }
  if(i==3){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
 }
 if(i==4){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
 }
  if(i==5){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
   rect(blockx[5], blocky[5], 100, 100);
 }
  if(i==6){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
   rect(blockx[5], blocky[5], 100, 100);
   rect(blockx[6], blocky[6], 100, 100);
 }
   if(i==7){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
   rect(blockx[5], blocky[5], 100, 100);
   rect(blockx[6], blocky[6], 100, 100);
   rect(blockx[7], blocky[7], 100, 100);
 }
    if(i==8){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
   rect(blockx[5], blocky[5], 100, 100);
   rect(blockx[6], blocky[6], 100, 100);
   rect(blockx[7], blocky[7], 100, 100);
   rect(blockx[8], blocky[8], 100, 100);
 }
     if(i==9){
   rect(blockx[1], blocky[1], 100, 100);
   rect(blockx[2], blocky[2], 100, 100);
   rect(blockx[3], blocky[3], 100, 100);
   rect(blockx[4], blocky[4], 100, 100);
   rect(blockx[5], blocky[5], 100, 100);
   rect(blockx[6], blocky[6], 100, 100);
   rect(blockx[7], blocky[7], 100, 100);
   rect(blockx[8], blocky[8], 100, 100);
   rect(blockx[9], blocky[9], 100, 100);
 }
  strokeWeight(10);
  noFill();
  rect(0, 0, 100, 100);
  rect(100, 0, 100, 100);
  rect(200, 0, 100, 100);
  rect(300, 0, 100, 100);

  rect(0, 100, 100, 100);
  rect(100, 100, 100, 100);
  rect(200, 100, 100, 100);
  rect(300, 100, 100, 100);

  rect(0, 200, 100, 100);
  rect(100, 200, 100, 100);
  rect(200, 200, 100, 100);
  rect(300, 200, 100, 100);
}

you increase i in several places but when using i in the arrays blockx and blockx it gives an ArrayIndexOutOfBoundsException: 10 when i >= 10.

You can check if i is <10 before using it in the arrays blockx and blockx

1 Like

…and the reason that this works: on each call, millis() is a specific number of ellapsed milliseconds, like 713, or 3967. You are not going to exactly match (==) the number of milliseconds to your timer – you are only going to pass it (>) when your timer is done.

2 Likes