How do I make randomly generated objects move?

Hi! I’m trying to code something like scrolling credits with some text. I loaded each text block as an image and generated the text order randomly. I now want those text blocks to move up the screen like movie-ending credits. I am however getting errors and I don’t know what is wrong.

PImage [] verse;
int numverse=0;
int choosev;
int versebef=-2;
float posy = 150;
float xpos,ypos;
float yspeed = 0.15;
int ydirection = 1;
void setup () {
 size (400,400);
 background (250,180,60);
 verse = new PImage [10];
 verse[0]=loadImage ("bea.png");
 verse[1]=loadImage ("chen.png");
 verse[2]=loadImage ("felipe.png");
 verse[3]=loadImage ("jero.png");
 verse[4]=loadImage ("lxau.png");
 verse[5]=loadImage ("magu.png");
 verse[6]=loadImage ("malala.png");
 verse[7]=loadImage ("mat.png");
 verse[8]=loadImage ("meli.png");
 verse[9]=loadImage ("sasi.png");
}
void draw () {
 background (250,180,60);
 mugres.move();
 mugres.write();
}
class mugres { 
  //float xpos,ypos; //starting position
  float yspeed; //speed of scroll
  int ydirection = 1; //direction up
  void move() {
   xpos = random (0,30);
   yspeed = 1.5;
   ypos = ypos - (yspeed * ydirection);
  }
  void write() {
  if (numverse < 10) {
  choosev = int (random(10));
  image (verse[choosev], random(-10,30), ypos);
  ypos = ypos + 100;
  numverse = numverse + 1;
       } 
  }
}

It’s a convention that the class name starts with a capital letter.

So Mugres

But also you need an object derived from the class; so before setup ()

Mugres mugres = new Mugres();

Then the rest might work

Great! There’s no error anymore however it still doesn’t run as I expect. The text pops up real quick and disappears, rather than scroll up the screen…

void draw () {
 background (250,180,60);
 team.write();
 team.move();

}

class Mugres {
  
  float xpos,ypos; //starting position
  float yspeed; //speed of scroll
  int ydirection = 1; //direction up
  
  void write() {
  if (numverse < 10) {
  choosev = int (random(10));
  image (verse[choosev], random(-10,30), ypos);
  ypos = ypos + 100;
  numverse = numverse + 1;
       } 
  }
  
  void move() {
   xpos = random (0,50);
   yspeed = 1.5;
   ypos = ypos - (yspeed * ydirection);
   
  }
  



}

ypos should initially be height (define it before write ())

and then say ypos=ypos-1;

Hello!

You’ve commented this as “up” direction.
However, I think you want: -1 for the y position to move upward.

:nerd_face:

here is my version

team = new Mugres(); // must call AFTER size command

Sketch

PImage [] verse;
// int numverse=0;
// int choosev;
//int versebef=-2;
//float posy = 150;
//float xpos, ypos;
//float yspeed = 0.15;
//int ydirection = 1;

Mugres team;

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

  background (250, 180, 60);

  team = new Mugres(); // must call AFTER size command

  verse = new PImage [6];
  verse[0]=loadImage ("IMG_6023.JPG");
  verse[1]=loadImage ("IMG_6025.JPG");
  verse[2]=loadImage ("IMG_6029.JPG");
  verse[3]=loadImage ("IMG_6030.JPG");
  verse[4]=loadImage ("IMG_6031.JPG");
  verse[5]=loadImage ("IMG_6036.JPG");
  verse[6]=loadImage ("malala.png");
  verse[7]=loadImage ("mat.png");
  verse[8]=loadImage ("meli.png");
  verse[9]=loadImage ("sasi.png");

  for (PImage img : verse)
    img.resize(width, 100);
}

void draw () {
  background (250, 180, 60);
  team.write();
  team.move();
}

// =========================================================================================

class Mugres {

  float xpos=0, ypos=height; //starting position
  float yspeed=1.5; //speed of scroll
  int ydirection = 1; //direction up
  int choosev = int (random(3));

  void write() {
    // when we are still inside screen
    if (ypos+verse[choosev].height > 0) {
      image (verse[choosev], xpos, ypos); // using xpos here
      // ypos = ypos + 100;
      //numverse = numverse + 1;
    } else {
      // we left the screen at the top: new image
      choosev = int (random(3));
      ypos=height; //starting position
    }
  }

  void move() {
    xpos = random (0, 12);
    yspeed = 1.5;
    ypos = ypos - 1; // (yspeed * ydirection);
  }
}//class
//

1 Like