Page 1 of 1

Needing help with using Java to create a fly swatter game. Using the image() presents an error saying it expects a PImag

Posted: Fri Apr 29, 2022 7:09 am
by answerhappygod
Needing help with using Java to create a fly swatter game.
Using the image() presents an error saying it expects a PImage,
float, float which are all present in the code.
PImage fly;
PImage flybye;
PImage swatter;
PImage swatted;
float[] fX,fY; // fly locations array
float[] swat; // fly swatted binary boolean array, 1 =
swatted, 0 = not swatted
int score=0; // increments when swatted.
int fly_size = 50;
int swatter_size = 100;
void setup(){
size(800,400);
fX=new float[0];
fY=new float[0];
swat=new float[0];
// load images
fly = loadImage("fly.png");
flybye = loadImage("flybye.png");
swatter = loadImage("swatter.png");
swatted = loadImage("swatted.png");
fX =append(fX, random(800)); //first fly - random
location
fY =append(fY, random(400));
swat =append(swat,0); // used as a boolean and matches to
each individual fly, 0 = fly not swatted, 1 = swatted.
}
void populate(){ // draw the flies in memory to the
screen.
for(int i=0;i<fX.length;i++){
if(swat==1){ // if swatted
// resize the fly image and place based on
fx/fy array values
image(flybye, fX, fY, fly_size,
fly_size);
} else { // not swatted
image(fly, fX, fY, fly_size, fly_size);
}
}
}
void collisionDetect(){ //collision detection - detect collision
between swatter and fly
for(int i=0; i<swat.length;i++){ // bounding box
detection
if(mouseX > i && mouseX < i + fly_size
&& mouseY > i && mouseY < i +
fly_size){
// condition should look at location of mouse
and individual coordinates in fX and fY
swat = 1; // swatted
fX =append(fX, random(800)); //new fly placed
in random location when old fly dies.
fY =append(fY, random(400));
swat =append(swat,0); // new fly not
swatted
score++; //increment score
}
}
}
void draw(){
background(255);
populate(); // draw flys to screen.
fill(0);
// set a text size and location for the score.
if(mousePressed){ // image swap
collisionDetect();
image(swatted, mouseX, mouseY); //draw swatter
image to around mouse locaiton - might want to play with this to
get it to look right.
}else{
image(swatter, mouseX, mouseY); // if not pressed
then alternative image.
}

}