URGENT: How to repeat an image movement per second

I would like drop either 2 imageA or 1 imageB from the y = 0 border / upper border by using a coin to decide which to fall.

I am using Processing 2.2.1.

if (millis() - startTime >= 1000)
{
float coin = random(1, 100);
startTime = millis();
}
if(coin % 5 == 2)
{
image(a, aX, aY, aW, aH);
aY = aY + aDy;
aX = random(0, width - aW) ; ;
}else
{
for(int i = 0; i < b.length; i = i + 1){
image(b[i], bX[i], bY[i], bW[i], bH[i]);
bY[i] = bY[i] + bDy[i];
image(b[i], bX[i], bY[i], bW[i], bH[i]);
bY[i] = bY[i] + bDy[i];
}
}
}

Thank you so much.

Hi @tank
There are too many failures in your code to understand what you want.
Are you using 2 arrays of images PImage[] a, b;? They are not declared.
If you have stipulated the x, y positions, width, and length in arrays, what does the array bDy[] do?
Below a simple code to drop ellipses randomly every second.

boolean head, tails;
int y, startTime;

void setup() {
  size(300, 300);
}

void draw() {
  background(255);
  if (millis()-startTime > 1000) { 
     int coin = int(random(0, 2));
     if(coin == 0) {
       head = true;
       tails = false;
     } else {
       head = false;
       tails = true;
     }
    startTime = millis();
    y = 0;
  }
  if(head) {
    fill(255, 0, 0);
    ellipse(100, y, 30, 30);
    y += 10;
  }
  if(tails) {
    fill(0, 0, 255);
    ellipse(200, y, 30, 30);
    y += 10;
  }
}

Original post here:
https://discourse.processing.org/t/urgent-how-to-repeat-an-image-movement-per-second/25667

@noel
Thank you for your response!
I am sorry that I did not type that properly.
Would it be better now?

PImage a;
int aW = 50;
int aH = 50;
float aX = random(0, width - aW) ;
int aY = 0 - bombH ;
int aDx = int(random(3, 6));
int aDy = int(random(3, 6));

PImage[] b = new PImage [10];
float[] bX = new float [10];
int[] bY = new int [10];
int[] bDx = new int [10];
int[] bDy = new int [10];
int[] bW = new int [10];
int[] bH = new int [10];
boolean[] bDrop = new boolean [10];

int startTime = millis();

void setup()
{
a = loadImage("a.png");

  for(int i = 0; i < b.length; i = i + 1)
  {
    b[i] = loadImage("b" + i + ".png");
    bW[i] = 50;
    bH[i] = 50;
    bX[i] = int(random(0, width - bW[i]));
    bY[i] = 0 - bH[i];
    bDx[i] = int(random(3, 5));
    bDy[i] = int(random(3, 5));
    
    bDrop[i] = true;
    
    score = 0;
  }
}

void draw()
{
if (millis() - startTime >= 1000)
{
float coin = random(1, 100);
startTime = millis();
}
if(coin % 5 == 2)
{
image(a, aX, aY, aW, aH);
aY = aY + aDy;
aX = random(0, width - aW) ; ;
}else
{
for(int i = 0; i < b.length; i = i + 1){
image(b[i], bX[i], bY[i], bW[i], bH[i]);
bY[i] = bY[i] + bDy[i];
image(b[i], bX[i], bY[i], bW[i], bH[i]);
bY[i] = bY[i] + bDy[i];
}
}
}
}

If you want to animate something you need to use the draw() loop.
Try to adapt your code to the example I posted above.

Oh, thank you so much.
Can I say it is a similar concept of creating my own function and then apply it in void draw()?

Yes, if you want, you can also create custom functions and call them in the time driven draw() loop.

This is not a homework.
This is a gift that I would like to present to my grandie.
My grandie is dying and that’s why it is urgent!

Well I would start with like I said using the draw() loop.
Without it, nothing will drop;

The dropping action is within the draw() loop.

As what you said, I will put the dropping action inside a self-created function and call it in the draw() loop.

Thank you so much.

I don’t think that’s a coin…

what about

noel’s way with

     int coin = int(random(0, 2)); // gives 0 or 1, never 2, iirc 
     if(coin == 0) {
    

hehe, had to look that up

1 Like

here is a version that doesn’t really use your arrays but works with
images a and b ONLY

PImage a;
int aW = 50;
int aH = 50;
float aX = random(0, width - aW) ;
int aY = 11; // 0 - bombH ;
int aDx = int(random(3, 6));
int aDy = int(random(3, 6));

PImage b ;//= new PImage [10];
float[] bX = new float [10];
int[] bY = new int [10];
int[] bDx = new int [10];
int[] bDy = new int [10];
int[] bW = new int [10];
int[] bH = new int [10];
boolean[] bDrop = new boolean [10];

int startTime = millis();
int     score = 0;
float coin=random(1); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

void setup()
{
  size(900, 900); 
  background(0); 

  a = loadImage("a.jpg");

  // for (int i = 0; i < b.length; i = i + 1)
  int i=0; 
  {
    b = loadImage("b" + ".jpg");
    bW[i] = 50;
    bH[i] = 50;
    bX[i] = int(random(0, width - bW[i]));
    bY[i] = 0 - bH[i];
    bDx[i] = int(random(3, 5));
    bDy[i] = int(random(3, 5));

    bDrop[i] = true;

    score = 0;
  }

  startTime = millis();
}

void draw()
{

  background(0); 

  if (millis() - startTime > 1000)
  {
    coin = random(1);
    // reset ALL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    startTime = millis();
    aX = random(0, width - aW) ;
    aY = 0;
    bY[0] = 0;
  }

  if (coin > 0.5)
  {
    // show ONE image 
    image(a, aX, aY, aW, aH);
    aY = aY + aDy;
    // aX = random(0, width - aW) ;
  } else
  {
    // show TWO images 
    int  i=0; 
    //for (int i = 0; i < b.length; i = i + 1) {
    image(b, bX[i], bY[i], bW[i], bH[i]);
    bY[i] = bY[i] + bDy[i];
    image(b, bX[i]+111, bY[i], bW[i], bH[i]);
    bY[i] = bY[i] + bDy[i];
    // }
  }
}

//

Thank you so much!
That genuinely helps a lot!
I found something interesting!
I’ve learnt that within random(), there should be 2 values, i.e. the lower limit and the upper limit.
In your program, random(1), could explain that please? This is the 1st time I have ever seen such an expression!
Thank you so much!

when you use only one value it is a short form for

float coin=random(0, 1);

first value 0 then

Question is whether

  • the 1 second should start when the image has left the screen (at lower border) or
  • (like in my version) when 1 second has passed since image has started to fall.

With the first you can look longer at the images. Might be better.

I just would like to add why I prefer:
int number = int(random(low, high+1);
over:
float number = random(high);
It’s because the latter actually never reaches the high number, the boundaries of the random function are not included, and when you for some reason, later on, convert it to an integer, you will miss the last one.

1 Like