tank
November 25, 2020, 3:59am
1
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.
noel
November 25, 2020, 7:03am
2
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;
}
}
glv
November 25, 2020, 8:21am
3
tank
November 25, 2020, 8:31am
5
@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];
}
}
}
}
noel
November 25, 2020, 8:38am
6
If you want to animate something you need to use the draw() loop.
Try to adapt your code to the example I posted above.
tank
November 25, 2020, 8:42am
7
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()?
noel
November 25, 2020, 8:45am
8
Yes, if you want, you can also create custom functions and call them in the time driven draw() loop.
tank
November 25, 2020, 8:58am
10
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!
noel
November 25, 2020, 8:58am
11
tank:
Could anyone tell me what’s wrong in my coding?
tank:
I would like to drop either 2 imageA or 1 imageB
Well I would start with like I said using the draw() loop.
Without it, nothing will drop;
tank
November 25, 2020, 9:04am
12
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.
Chrisir
November 25, 2020, 9:15am
13
tank:
if(coin % 5 == 2)
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) {
noel
November 25, 2020, 9:25am
14
Chrisir:
iirc
hehe, had to look that up
1 Like
Chrisir
November 25, 2020, 9:33am
15
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];
// }
}
}
//
tank
November 25, 2020, 9:53am
16
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!
Chrisir
November 25, 2020, 9:58am
17
tank:
there should be 2 values
when you use only one value it is a short form for
float coin=random(0, 1);
first value 0 then
Chrisir
November 25, 2020, 10:00am
18
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.
noel
November 25, 2020, 10:24am
19
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