Image not showing

why is my enemy image not showing?

//characters
PImage[] midas= new PImage[8];
PImage[] daughter= new PImage[3];
PImage[] enemy= new PImage[8];

boolean showMidas1= true;

//coordinates
float midasx= 1;
float midasy= 1;

float daughterx= width;
float daughtery= height;

float enemyprevx;
float enemyx= width-(width/4);

//i's
int i=1;
int i2= 1;
int i3= 1;


void setup() {
  size(600, 600);

  //load characters in
  midas[1] = loadImage("1.png");
  midas[2] = loadImage("2.png");
  midas[3] = loadImage("3.png");
  midas[4] = loadImage("4.png");
  midas[5] = loadImage("5.png");
  midas[6] = loadImage("6.png");
  midas[7] = loadImage("7.png");

  daughter[1] = loadImage("daughter.png");
  daughter[2] = loadImage("daughter gold.png");

  enemy[1]= loadImage("enemy1.png");
  enemy[2]= loadImage("enemy2.png");
  enemy[3]= loadImage("enemy3.png");
  enemy[4]= loadImage("enemy4.png");
  enemy[5]= loadImage("enemy5.png");
  enemy[6]= loadImage("enemy6.png");
  enemy[7]= loadImage("enemy7.png");
}
void draw() {
  background(0, 0, 0);
  tint(255, 255);
  //show and move the characters

  if ( (int) random(1, 3)== 1) {
    enemyx=enemyx+10;
  } else if ( (int) random(1, 3)== 2) {
    enemyx=enemyx-10;
  }

  if (enemyprevx== enemyx) {
    i3= 1;
    image( enemy[i3], enemyx, height-(height/4), 55, 85 );
  } else if (enemyprevx<enemyx) {
    pushMatrix();
    translate( enemyx + enemy[i3].width, height-(height/4));
    scale( -1, 1 );
    image( enemy[i3], enemyx, height-(height/4), 55, 85 );
    popMatrix();
  } else if (enemyprevx>enemyx) {
    image(enemy[i3], enemyx, height-(height/4), 55, 85);
  }



  if (dist(midasx, midasy, daughterx, daughtery) < 45) {
    i2=2;
  }

  image(daughter[i2], daughterx, daughtery, 40, 70);

  if (showMidas1==true&& ! keyPressed) {
    image(midas[1], midasx, midasy, 50, 80);
  } 
  if (keyPressed) {
    if (key == 'w') {
      midasy=midasy-10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
  if (keyPressed) {
    if (key == 's') {
      midasy=midasy+10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
  if (keyPressed) {
    if (key == 'a') {
      midasx=midasx-10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }
  if (keyPressed) {
    if (key == 'd') {
      midasx=midasx + 10;
      image(midas[i], midasx, midasy, 50, 80);
      showMidas1= false;
    }
  }



  i++;
  delay(100);
  if (i==7) {
    i=1;
  }
}

void keyReleased() {
  showMidas1 = true;
}

if i delete it it goes way too fastl.

That’s fine! We can fix that properly!

Instead of using delay(), check the value of millis() and only update things if it’s changed by over 100.

int last_time = 0;

void setup(){
  // ...
}

void draw(){
  if( millis() > last_time ){
    last_time = millis() + 100;
    // Do other things that should happen only once every 100 milliseconds.
  }
}
1 Like

why is this better than using delay()?

The delay function is pausing everything from happening. It just wait for the desired time to pass and then resume onto the next line of your program.

It is usually bad practice because you are “loosing” that time.

Using millis() as @TfGuy44 suggested, allow your program to keep running while running the desired part of your code only every 100 milliseconds. But if you need another part of your code to do something else in the meanwhile, nothing is preventing it this time.

1 Like

Some resource material:

References:

There are may topics related to timing and timers in the forum.

:)