Loading...
Logo
Processing Forum
I've been working on a simple game ad have come across a problem. After i shoot "character", the image of "character" dying just flashes on the screen for a fraction of a second. I would like to figure out a way for the dead character to be displayed for about 1 second.


/code/
PFont fontA;
PImage astroid;
PImage ship;
PImage GameOver;
PImage stars;
PImage explosion;
boolean shoot = false;
int randx()
{
  return int(random(600));
}
int[] sphereXCoords = {
  randx(), randx(), randx(), randx(), randx()
  };
int[] sphereYCoords = {
  0, 0, 0, 0, 0
};
void setup()
{
  size(600, 620);
  astroid = loadImage("darth_vader_and_stormtrooper_lego_minifigures_alarm_clocks_2.png");
  ship = loadImage("x-wing.png");
  GameOver= loadImage("Game_Over_Screen.png");
  stars = loadImage ("stars.png");
  explosion = loadImage ("Explosion.png");
}
void draw()
{
  background(stars);
  fill(color(0, 255, 0));
  stroke(color(0, 0, 0));
  triangle( mouseX-8, 580, mouseX+8, 580, mouseX, 565);
  fill(color(0, 0, 255));
  stroke(color(0, 0, 255));
  if (shoot==true)
  {
    sphereKiller(mouseX);
    shoot = false;
  }
  sphereDropper();
  gameEnder();
}
void mousePressed()
{
  shoot = true;
}
void sphereDropper()

  stroke(255);
  for (int i=0; i<5; i++)
  {
    /*
    ellipse(sphereXCoords[i], sphereYCoords[i]++,
     sphereDiameter, sphereDiameter);
     */
    image(astroid, sphereXCoords[i], sphereYCoords[i]++, 30, 40);
  }
}
void sphereKiller(int shotX)
{
  boolean hit = false;
  for (int i = 0; i < 5; i++)
  {
    if ((shotX >= (sphereXCoords[i]-30/2)) &&
      (shotX <= (sphereXCoords[i]+30/2)))
    {
      hit = true;
      line(mouseX, 565, mouseX, sphereYCoords[i]);
      image(explosion, sphereXCoords[i], sphereYCoords[i],
      80, 90);
      sphereXCoords[i] = randx();
      sphereYCoords[i] = 0;
    }
  }
  if (hit == false)
  {
    line(mouseX, 565, mouseX, 0);
  }
}
void gameEnder()
{
  for (int i=0; i< 5; i++)
  {
    if (sphereYCoords[i]==600)
    {
      fill(color(255, 0, 0));
      background(GameOver);
      noLoop();
    }
  }
}

Replies(4)

See the Technical FAQ where I tried to explain how to do this kind of stuff. The last article is currently just a placeholder, I should resume its writing.
When the character dies, store the current time in a variable(named tDeath for ex) with System.currentTimeMillis() and set a characterDeadUpdating flag as true. Update the animation till   (System.currentTimeMillis() - tDeath < 1 second). I didn't run your code but t he algorithm would be similar to something shown below:
Copy code
  1. if(characterDeadUpdating == true && (System.currentTimeMillis() - tDeath < 1000) )
    {
          //do the needed stuff here
    }
    else
          characterDeadUpdating = false;
Do you think you could help implement this into the rest of my code? I tried, but I'm always getting an error.
Thanks!
You can use Processing's millis() instead of  System.currentTimeMillis() too.