We are about to switch to a new forum software. Until then we have removed the registration on this forum.
PImage fishImage, booty;
int q=1;
int p=1;
void setup() {
size(600, 600);
background(#124FB4);
}
void draw() {
ArrayList<Fish> fish=new ArrayList();
fishImage=loadImage("fish.jpg"); //load the image
booty=loadImage("booty.jpg");
if (q==1) {
for (int i=0; i<6; i++) { // yes i know it would have been simpler to put this in the setup() method but i do not like simple... :)
int x=int(random(width/5, width-50)); //x of fish
int y=int(random(height/5, height-50)); //y of fish
fish.add(new Fish(x, y));
println(i);
fill(#1203FF);
stroke(#15D3D2);
ellipse(x-15, y-5, 5, 5);
ellipse(x-20, y-20, 5, 5);
ellipse(x+5, y-45, 5, 5);
q=2;
}
image(booty, int(random(50, width-50)), height-75, 50, 50);
}
if (p==1) {
for (int i=0; i<1000; i++) { //draw rocks as ellipses
int x=int(random(width));
int y=int(random(height-25, height));
noStroke();
int w=int(random(5));
//if w==0 then it will fill the color of bubbles
if (w==1)
fill(#37EA1C);
if (w==2)
fill(#FFEB08);
if (w==3)
fill(#08F9FF);
if (w==4)
fill(#FF0808);
if (w==5)
fill(#FF08B9);
ellipse(x, y, 10, 10);
p=2;
}
}
for (int i=0; i<fish.size (); i++) {
fish.get(i).display();
}
for (int i=0; i<fish.size (); i++) {
fish.get(i).move();
}
}
class Fish {
private int x, y;
public Fish(int xpos, int ypos) {
x=xpos;
y=ypos;
}
void display() {
image(fishImage, x, y, width/15, height/15);
}
void move() { //this is the stuff that is not executing correctly. i want it to constantly increase x but it is not increasing x at all....
x+=5;
if (x>width) {
x=0;
println(x);
}
}
}
Answers
No.
NO.
NO!!! NO!!!!! NO!!!!!!!
NOOOOOOOOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!
X(
DO NOT USE loadImage() IN draw()!
@TfGuy44 what is the method i should be using instead of loadImage()?
@TfGuy44 could you please tell me how to make the fish move in the move method under the fish class? mine is not working
Please put the lines that use loadImage() into setup() before you do anything else.
ok and how do i make the fish move?
here is my updated code.... how can i make the fish move? that is the only part that i need help with... Thanks @TfGuy44
The problem is that your ArrayList of Fish, fish, is defined inside draw(), and so it is re-created every frame. But fish are only added to it on the first frame, when q==1!
Move line 13 to the top.
@TfGuy44 can you email with me to get the file?
@TfGuy44 I sent you the email containing the code and the data
I cut it down to just the fish. If you want to draw additional things, define additional global variables to remember them, create them in setup(), and update and render them in draw(). You should NOT rely on what was drawn in the previous frame - redraw every element that should be visible every time draw() runs.
@TfGuy44 how would I reverse them if they are at or greater than width? I currently have
The line x-=5 updates the Fish's position. If you give it a speed, you would have to use that new speed variable to update the position, instead of the constant 5.
where do I put the speedX variable here?
where it currently is the last else if statement cannot be executed because speedX has not been initialized... so where do i put speedX without it constantly updating speedX?
Define speed as a class variable, like x is, not as a local variable in your move() function.
When your fish is off the left edge, set the speed to 1.
When your fish is off the right edge, set the speed to -1.
Always update the fish's position based on its speed.
it's a shame...
all this is covered in the most basic examples
he must have a very bad teacher....
the speed thing for the fish:
@Chrisir yes, my teacher is very bad...
;-)
@Chrisir my updated code 100% working
Congratulations!
;-)
@Chrisir thanks!