Hi, I’m trying to make a representation of frogs jumping out of water.
This is my code:
ArrayList<Frogs> frog = new ArrayList();
PImage pataIzq, pataDer,cuerpo;
PImage bgBackline, bgBluerose, bgFloaters, bgGrassnotop, bgGrasstop,
bgLotus, bgMushroom, bgPinkrose, bgSoil, bgStones, bgSwamp, bgTrees,
bgTrunk, bgYellowrose;
boolean runOnce = true;
BackwardBg bbg;
AfterwardBg abg;
void setup() {
size(1200, 600);
pataIzq = loadImage("pata-izq.png");
pataDer = loadImage("pata-der.png");
cuerpo = loadImage("cuerpo2.png");
bbg = new BackwardBg();
abg = new AfterwardBg();
addFrogs();
}
void draw() {
bbg.display();
abg.display();
translate(width>>1, height>>1);
scale(0.7);
for (Frogs I : frog) {
I.show();
I.move();
}
}
class Frogs {
float theta=0f, x, y, l=200f, angle, dx;
boolean forward=false;
int xl=26, yl=16, speed;
Frogs() {
speed=int(random(6, 10));
angle = random(-PI, PI);
dx=random(-20, 20);
y=0;
}
void show() {
pushMatrix();
rotate(angle);
translate(x+dx, y);
//stroke(255, 0, 0);
//line(0, 0, -ylsin(radians(theta)), 0);
//------LEFT WING-----------
pushMatrix();
rotate(radians(theta+30));
image(pataIzq, 0, yl);
popMatrix();
//-------RIGHT WING------------
pushMatrix();
rotate(PI-radians(theta+30));
image(pataDer, 0, yl);
popMatrix();
//-------TUMMY-----------
imageMode(CENTER);
rotate(radians(90));
image(cuerpo, 0, 0);
//--------------------
popMatrix();
}
void move() {
l=ylsin(radians(5));
if (!forward) {
x+=l;
theta+=speed-2;
} else {
theta-=1;
if (theta<0)forward=false;
}
if (theta>50) {
forward=true;
}
}
}
void mousePressed() {
addFrogs();
}
void addFrogs() {
for (int i=0; i<8; i++) {
Frogs I = new Frogs();
frog.add(I);
}
}
class AfterwardBg {
PImage bgGrassnotop, bgGrasstop, bgTrees, bgMushrooms, bgStones,
bgBlackline;
void display () {
bgGrassnotop = loadImage("bg-grassnotop.png");
bgGrasstop = loadImage("bg-grasstop.png");
bgTrees = loadImage("bg-trees.png");
bgMushrooms = loadImage("bg-mushroom.png");
bgStones = loadImage("bg-stones.png");
bgBlackline = loadImage("bg-blackline.png");
imageMode(CENTER);
image(bgGrassnotop, 600, 300);
image(bgGrasstop, 600, 300);
image(bgTrees, 600, 300);
image(bgMushrooms, 600, 300);
image(bgStones, 600, 300);
image(bgBlackline, 600, 300);
}
}
class BackwardBg {
PImage bgBluerose, bgFloaters, bgLotus, bgPinkrose, bgSoil,
bgStones, bgSwamp, bgYellowrose;
void display () {
bgBluerose = loadImage("bg-bluerose.png");
bgFloaters = loadImage("bg-floaters.png");
bgLotus = loadImage("bg-lotus.png");
bgPinkrose = loadImage("bg-pinkrose.png");
bgSoil = loadImage("bg-soil.png");
bgStones = loadImage("bg-stones.png");
bgSwamp = loadImage("bg-swamp.png");
bgYellowrose = loadImage("bg-yellowrose.png");
imageMode(CENTER);
image(bgBluerose, 600, 300);
image(bgFloaters, 600, 300);
image(bgLotus, 600, 300);
image(bgPinkrose, 600, 300);
image(bgSoil, 600, 300);
image(bgStones, 600, 300);
image(bgSwamp, 600, 300);
image(bgYellowrose, 600, 300);
}
}
I have a few problems here.
If I call the display functions inside the setup, it loads fast but when the frog moves they leave a trail of frames behind. In contrary, if I call the functions inside draw, i don’t have that problem, but it’s like 1 fps / minute… and surely it happens because it’s calling the loadImage functions a lot of times.
To clarify, I need to import all elements separately because I need to make a custom handler to use color picker with each one of them separately.
Question: Is there any way to bypass this ? what would be the best option here ?
Question 2: Is there any way to place a z-index in images here ? The frogs while moving pass on top of the images that should have upper z-index.
Something like:
BackwardBG = z-index: 0;
Frogs = z-index: 1;
AfterwardBG = z-index: 2;
Thanks a lot for reading this extensive block.