How to move a new image every 2 seconds

edited May 2016 in Library Questions

Hey all- So I'm attempting to create a simple game where the user controls a black hole with the left/right arrow keys and tries to catch falling objects inside. I'm still on the falling objects piece. I'd like to drop a new "space" image every two seconds, but I'm hitting a wall in trying to sustain the image and keep the timer function on a continuous loop.

All help is greatly appreciated- thanks.

int currenttime = 0;//initializing the time variables
int interval = 1000;
int savedTime;
int totalTime = 2000;

PImage r;

int maxImages = 5;//# of space images
int imageIndex = 0;//#initial image to be displayed first

int x = 400; //x for black hole
int y1 = -10;
int x1 = int(random(10, 780));
PImage back;//loading the star image
PImage cir;//loading blur around black hole
///*
import ddf.minim.*;//audio player
Minim minim;//audio player
AudioPlayer player;
//*/
PImage[] spaceArray = new PImage[4]; //loading space images


void setup() {
  size(800, 575,P3D);
    savedTime= millis();//setting millis as savedTime
  frameRate(200);

   for (int i = 0; i< 4;i++){//for loop for space array
  //i = int(random(spaceArray.length));//array of space images
    spaceArray[i] = loadImage("space"+ i + ".png");
  }
///*
minim = new Minim(this);//audio player
player = minim.loadFile("01 Io.mp3");//loading song 
player.loop(); //playing and looping song
//*/  
  back = loadImage("stars.jpeg");//background

  PGraphics pg = createGraphics(200,200,P2D);//setting specs. for black hole blur ring
  pg.beginDraw();
  pg.fill(255);
  pg.background(0,0,0,0);
  pg.noStroke();
  pg.ellipse(100,100,100,100);
  pg.filter(BLUR,5);
  pg.endDraw();
  cir = pg.get();
  imageMode(CENTER);//tfguy44

  smooth();

}
void draw() {

   int passedTime = millis() - savedTime; 

 background(back);//star picture

 if (y1 < 1000){
   y1++;
 }

 image(spaceArray[imageIndex],x1, y1, 50, 60);//space image

 if (passedTime > totalTime){//timer to (with framerate =1000) drop an object every second
              //int y1 = -10;
//float x1 = random(10, 780);//randomized x for falling objects
for(int i =0; i<200; i++){
 imageUpdate();

}
 //image(spaceArray[0],x1,y1, 50,60);//space image
             //(image(spaceArray[(int)random(0,4)],x1,y1, 50,60));//space image
savedTime = millis();
println("got this far");
}


 if (keyPressed && (key == CODED)) {  // If it’s a coded key
    if (keyCode == LEFT) {    //if it's left arrow
     if (x - 53 >= 0 ) {   //keeps the black hole from leaving the screen left

      x-=7;
    } }else if (keyCode == RIGHT) {   //if it's the right arrow
    if (x + 53 <=800) { //stops it at edge of screen right
      x+=7;
} }
 }
  image(cir, x, 490);//white blur around black hole
  fill(0);//color hole
  ellipse(x, 490, 90, 90);//black hole


}
 void imageUpdate(){

//x1+=1;
y1+=1;
image(spaceArray[(int)random(0,4)],x1,y1, 50,60);
}
Tagged:

Answers

  • the idea is that in draw() a image is displayed with the index imageIndex (no matter where the timer is, one image is the current image)

    so when the timer is due, just change the index imageIndex in imageUpdate()

    int currenttime = 0;//initializing the time variables
    int interval = 1000;
    int savedTime;
    int totalTime = 2000;
    
    PImage r;
    
    int maxImages = 5;//# of space images
    int imageIndex = 0;//#initial image to be displayed first
    
    int x = 400; //x for black hole
    int y1 = -10;
    int x1 = int(random(10, 780));
    PImage back;//loading the star image
    PImage cir;//loading blur around black hole
    ///*
    //import ddf.minim.*;//audio player
    //Minim minim;//audio player
    //AudioPlayer player;
    //*/
    PImage[] spaceArray = new PImage[4]; //loading space images
    
    
    void setup() {
      size(800, 575, P3D);
      savedTime= millis();//setting millis as savedTime
      frameRate(200);
    
      for (int i = 0; i< 4;i++) {//for loop for space array
        //i = int(random(spaceArray.length));//array of space images
        spaceArray[i] = loadImage("space"+ i + ".png");
      }
      ///*
      //  minim = new Minim(this);//audio player
      // player = minim.loadFile("01 Io.mp3");//loading song 
      // player.loop(); //playing and looping song
      //*/  
      back = loadImage("stars.jpeg");//background
    
      PGraphics pg = createGraphics(200, 200, P2D);//setting specs. for black hole blur ring
      pg.beginDraw();
      pg.fill(255);
      pg.background(0, 0, 0, 0);
      pg.noStroke();
      pg.ellipse(100, 100, 100, 100);
      pg.filter(BLUR, 5);
      pg.endDraw();
      cir = pg.get();
      imageMode(CENTER);//tfguy44
    
      smooth();
    }
    void draw() {
    
      int passedTime = millis() - savedTime; 
    
      //  background(back);//star picture
    
        if (y1 < 1000) {
        y1++;
      }
    
      //image(spaceArray[imageIndex], x1, y1, 50, 60);//space image
    
      if (passedTime > totalTime) {//timer to (with framerate =1000) drop an object every second
        //int y1 = -10;
        //float x1 = random(10, 780);//randomized x for falling objects
        //    for (int i =0; i<200; i++) {
        imageUpdate();
        //   }
        //image(spaceArray[0],x1,y1, 50,60);//space image
        //(image(spaceArray[(int)random(0,4)],x1,y1, 50,60));//space image
        savedTime = millis();
        println("got this far");
      }
    
    
      if (keyPressed && (key == CODED)) {  // If it’s a coded key
        if (keyCode == LEFT) {    //if it's left arrow
          if (x - 53 >= 0 ) {   //keeps the black hole from leaving the screen left
    
            x-=7;
          }
        }
        else if (keyCode == RIGHT) {   //if it's the right arrow
          if (x + 53 <=800) { //stops it at edge of screen right
            x+=7;
          }
        }
      }
      //  image(cir, x, 490);//white blur around black hole
      fill(0);//color hole
      ellipse(x, 490, 90, 90);//black hole
    }
    void imageUpdate() {
    
      //x1+=1;
      y1+=1;
      imageIndex=(int)random(0, 4);
      println(imageIndex); 
      //image(spaceArray[(int)random(0, 4)], x1, y1, 50, 60);
    }
    
    //
    
  • I'm sorry- I should have been more detailed in my description. I would like a new image to drop down every two seconds, then maintain on its path downward so it can be a continuous game with variability in the dropping images.

  • Just load all images into an array in setup() and display them in draw()

    Are there more than one item at a time falling?

  • this is the simple version. It restarts when a image leaves the screen.

    // 
    
    
    int currenttime = 0;//initializing the time variables
    int interval = 1000;
    int savedTime;
    int totalTime = 2000;
    
    PImage r;
    
    int maxImages = 5;//# of space images
    int imageIndex = 0;//#initial image to be displayed first
    
    int x = 400; //x for black hole
    int y1 = -10;
    int x1 = int(random(10, 780));
    PImage back;  //loading the star image
    PImage cir;   //loading blur around black hole
    ///*
    //import ddf.minim.*;//audio player
    //Minim minim;//audio player
    //AudioPlayer player;
    //*/
    PImage[] spaceArray = new PImage[4]; //loading space images
    
    
    void setup() {
    
      size(800, 575, P3D);
    
      background(111);
    
      println("1");
      savedTime= millis();//setting millis as savedTime
      frameRate(200);
    
      for (int i = 0; i < 4; i++) {//for loop for space array
        //i = int(random(spaceArray.length));//array of space images
        spaceArray[i] = loadImage("space"+ trim(str(i)) + ".jpg");
      }
      ///*
      //  minim = new Minim(this);//audio player
      // player = minim.loadFile("01 Io.mp3");//loading song 
      // player.loop(); //playing and looping song
      //*/  
      back = loadImage("stars.jpeg");//background
    
      PGraphics pg = createGraphics(200, 200, P2D);//setting specs. for black hole blur ring
      pg.beginDraw();
      pg.fill(255);
      pg.background(0, 0, 0, 0);
      pg.noStroke();
      pg.ellipse(100, 100, 100, 100);
      pg.filter(BLUR, 5);
      pg.endDraw();
      cir = pg.get();
      imageMode(CENTER);//tfguy44
    
      smooth();
      println("2");
    }
    
    void draw() {
    
      background(111);
    
      // println("3");
      int passedTime = millis() - savedTime; 
    
      //  background(back);//star picture
      //
      if (y1 < height) {
        // fall
        y1++;
      }
      else {
        // restart 
        x1 = int(random(10, 780));
        y1=-10;
        imageUpdate();
      }
    
      image(spaceArray[imageIndex], x1, y1, 50, 60);//space image
    
      // println("4");
    
      if (passedTime > totalTime) {//timer to (with framerate =1000) drop an object every second
        //int y1 = -10;
        //float x1 = random(10, 780);//randomized x for falling objects
        //    for (int i =0; i<200; i++) {
        //   imageUpdate();
        //   }
        //image(spaceArray[0],x1,y1, 50,60);//space image
        //(image(spaceArray[(int)random(0,4)],x1,y1, 50,60));//space image
        savedTime = millis();
        println("got this far");
      }
    
    
      if (keyPressed && (key == CODED)) {  // If it’s a coded key
        if (keyCode == LEFT) {    //if it's left arrow
          if (x - 53 >= 0 ) {   //keeps the black hole from leaving the screen left
    
            x-=7;
          }
        }
        else if (keyCode == RIGHT) {   //if it's the right arrow
          if (x + 53 <=800) { //stops it at edge of screen right
            x+=7;
          }
        }
      }
      //  image(cir, x, 490);//white blur around black hole
      fill(0);//color hole
      ellipse(x, 490, 90, 90);//black hole
    }
    
    void imageUpdate() {
      //x1+=1;
      imageIndex=(int)random(0, 4);
      if (imageIndex > 4)
        imageIndex=0;
      println(imageIndex); 
      //image(spaceArray[(int)random(0, 4)], x1, y1, 50, 60);
    }
    
    //
    
  • here...

    // 
    
    ArrayList <itemFalling> items = new ArrayList();
    
    int savedTime;
    int totalTime = 2000;
    
    // int maxImages = 5;    //# of space images
    int imageIndex = 0;      //#initial image to be displayed first
    
    int x = 400;   // x for black hole
    int y1 = -10;
    int x1 = int(random(10, 780));
    PImage back;   //loading the star image
    PImage cir;    //loading blur around black hole
    
    ///*
    //import ddf.minim.*;//audio player
    //Minim minim;//audio player
    //AudioPlayer player;
    //*/
    
    PImage[] spaceArray = new PImage[4]; //loading space images
    
    // --------------------------------------------------------
    
    void setup() {
    
      size(800, 575, P3D);
    
      background(111);
    
      println("Start of setup()");
      savedTime= millis();//setting millis as savedTime
      frameRate(200);
    
      for (int i = 0; i < 4; i++) {//for loop for space array
        //i = int(random(spaceArray.length));//array of space images
        spaceArray[i] = loadImage("space"+ trim(str(i)) + ".jpg");
      }
      ///*
      //  minim = new Minim(this);//audio player
      // player = minim.loadFile("01 Io.mp3");//loading song 
      // player.loop(); //playing and looping song
      //*/  
      back = loadImage("stars.jpeg");//background
    
      PGraphics pg = createGraphics(200, 200, P2D);//setting specs. for black hole blur ring
      pg.beginDraw();
      pg.fill(255);
      pg.background(0, 0, 0, 0);
      pg.noStroke();
      pg.ellipse(100, 100, 100, 100);
      pg.filter(BLUR, 5);
      pg.endDraw();
      cir = pg.get();
      imageMode(CENTER);//tfguy44
    
      smooth();
      println("End of setup()");
    } // func setup()
    
    void draw() {
    
      background(111);
      //  background(back);//star picture
      //
      int passedTime = millis() - savedTime; 
      // 
      for (itemFalling currItem : items) {
        currItem.display();
        currItem.move();
      } //for
    
      for (int i = items.size(); i>=0; i--) { 
        if (i<items.size()) {
          itemFalling currItem = items.get(i);
          if (!currItem.alive) { 
            items.remove(i);
          } // if
        } // if
      } // for
    
      if (passedTime > totalTime) {  
        // timer to (with framerate =1000) drop an object every second
        //
        itemFalling newItem = new itemFalling ( int(random(10, 780)), -10, (int)random(0, 4)  ); 
        items.add( newItem ); 
        savedTime = millis();
      }
    
      if (keyPressed && (key == CODED)) {  // If it’s a coded key
        if (keyCode == LEFT) {    //if it's left arrow
          if (x - 53 >= 0 ) {     //keeps the black hole from leaving the screen left
            x-=7;
          }
        }
        else if (keyCode == RIGHT) {   //if it's the right arrow
          if (x + 53 <=800) {   //stops it at edge of screen right
            x+=7;
          }
        }
      } // if CODED 
    
      //  image(cir, x, 490);
      //  white blur around black hole
      fill(0);//color hole
      ellipse(x, 490, 90, 90);//black hole
    } // func draw 
    
    //======================================================
    
    class itemFalling {
    
      float x;
      float y;
      int imageIndex;
      boolean alive = true;     
    
      // constr 
      itemFalling (float x_, float y_, int imageIndex_) {
        x=x_;
        y=y_;
        imageIndex = imageIndex_;
      }
    
      void display () {
        image(spaceArray[imageIndex], x, y, 50, 60); //space image
      }
    
      void move() {
        if (y < height+20) {
          // fall
          y++;
        }
        else {
          // kill it  
          alive = false;
        } // else
      }
      //
    } // class 
    //
    
Sign In or Register to comment.