How to stagger animation of objects

edited January 2016 in Questions about Code

So, I've been working on this game for a School project. All in all, it's pretty simple, you control a character who shoots arrows at enemies coming down from the top of the screen. I have all the enemies in an ArrayList which goes through a for loop to have them initiate their attack. However, this leads to 20 enemies moving down the screen at once when I want to delay each iteration of the loop by a second or two so the player has time to respond and enemies come down one by one. I know of things like Thread.sleep(), the wait function, and I'm aware of the timer class, though I can't get any of them to work right. My main code is below, with the segment starting with if(screen == 2) being called into question. I realize my code is probably akin to that of a novice, but I ask that someone please help me figure this one out. The game itself relies on a lot of external files/ alternate images so the full folder can be found here: https://dropbox.com/s/vwa7s0lt3n9ov4r/Game.zip Any help at all would be greatly appreciated

import ddf.minim.*;

PImage title;
PImage field;
PImage screen2;
PImage frontchar;
PImage sidechar;
PImage backchar;
PImage enemy;
PImage arrows;
public int screen = 0;
public String direction;
AudioPlayer player1;
AudioPlayer player2;
AudioPlayer player0;
AudioSample shoot;
Minim minim;
Enemy red1 = new Enemy();Enemy red2 = new Enemy();Enemy red3 = new Enemy();Enemy red4 = new Enemy();Enemy red5 = new Enemy();
Enemy red6 = new Enemy();Enemy red7 = new Enemy();Enemy red8 = new Enemy();Enemy red9 = new Enemy();Enemy red10 = new Enemy();
Enemy red11 = new Enemy();Enemy red12 = new Enemy();Enemy red13 = new Enemy();Enemy red14 = new Enemy();Enemy red15 = new Enemy();
Enemy red16 = new Enemy();Enemy red17 = new Enemy();Enemy red18 = new Enemy();Enemy red19 = new Enemy();Enemy red20 = new Enemy();
Main_Character Ranger = new Main_Character();
Arrow arw = new Arrow();
ArrayList <Enemy> horde = new ArrayList(20);

public void setup()
{
  size(800,600);
  horde.ensureCapacity(20);
  title = loadImage("title2.png");
  field = loadImage("startingfield.png");
  screen2 = loadImage("crossroads.png");
  frontchar = loadImage("front.png");
  enemy = loadImage("enemychar.png"); 
  arrows = loadImage("arrow.png"); 
  background(field);
  Ranger.display(frontchar);
  minim = new Minim(this);
  player0 = minim.loadFile("eternalwind.mp3");
  shoot = minim.loadSample("arrowshoot.mp3");
  player1 = minim.loadFile("Outset.mp3", 2048);
  player0.play();  
  player2 = minim.loadFile("those.mp3", 2048);
  frontchar = loadImage("front.png");
  sidechar  = loadImage("sidechar.png");
  backchar  = loadImage("back.png"); 
  horde.add(red1);horde.add(red2);horde.add(red3);horde.add(red4);horde.add(red5);
  horde.add(red6);horde.add(red7);horde.add(red8);horde.add(red9);horde.add(red10);
  horde.add(red11);horde.add(red12);horde.add(red13);horde.add(red14);horde.add(red15);
  horde.add(red16);horde.add(red17);horde.add(red18);horde.add(red19);horde.add(red20);
}

public void draw()
{  
  background(field);
  Ranger.display(frontchar);

  if(((Ranger.getPosX() <= 720 && Ranger.getPosX() >=580) && Ranger.getPosY() < 5) && screen == 1)
    {
      Ranger.setPosX(608);
      Ranger.setPosY(525);
      screen++;
    }
  if(((Ranger.getPosX() <= 720 && Ranger.getPosX() >=580) && Ranger.getPosY() > 555) && screen == 2)
    {
      Ranger.setPosX(600);
      Ranger.setPosY(75);
      screen--;
    }  
  if(screen == 0)
    {
      player0.play();
      background(title);
    }
  if(screen == 1)
    {
      player0.mute();
      player2.mute();
      player1.unmute();
      player2.rewind();
      player1.play();
      background(field);
      Ranger.display(frontchar);

    }  
  if(screen == 2)
    {
      background(screen2);
      Ranger.display(frontchar);
      player1.mute();
      player2.unmute();
      player1.rewind();
      player2.play();
      for(int i = 0; i < 20; i++)
          {      
          Enemy current = horde.get(i);  
          current.attack(enemy);
          }
    }   
}


void keyPressed()
{
  if(key == 's' || key == 'S')
  {
    if(screen == 0)
      {
        screen++;
      }
  }
  if (key == 'a' || key == 'A')
  {
    shoot.trigger();
    Ranger.shootArrow(backchar, arrows);
  }

  if (key == CODED) 
  {
    if (keyCode == UP)
    {
     Ranger.moveUP(backchar);
    }
    if (keyCode == DOWN) 
    {
     Ranger.moveDOWN(frontchar);
    } 
    if (keyCode == LEFT) 
    {
      Ranger.moveLEFT(sidechar);
    }
    if (keyCode == RIGHT) 
    {
      Ranger.moveRIGHT(sidechar);
    }
  } 
}
Tagged:

Answers

  • edited June 2014 Answer ✓

    you need to have a timer in each enemy, so put it in the class Enemy (and say enemy 1 starts after 1.2 seconds, next after 2 seconds etc.)

    you can move them down at different speed when the speed is in the class (like SpeedY = random(1,3); or so)

    you can give them different start pos high above the screen, so they get visible later:

    y=random(-400,-100);

    Best, Chrisir

    ;-)

Sign In or Register to comment.