problem with mouseClicked function
in
Programming Questions
•
2 years ago
hey guys,
essentially I'm making a game where I have a little character following my mouse around the screen with zombies bouncing all around him. what's suppose to happen is if the mouse clicks on the zombie, it goes away. However, when I click on the zombie, nothing happens. Can someone help me out?
PImage zombie;
boolean headShot = false;
boolean deadZombie = false;
int size = 60;
float xpos1, ypos1;
float xspeed = 2.8;
float yspeed = 2.2;
int xdirection = 1;
int ydirection = 1;
PImage hero;
float xpos;
float ypos;
float drag = 30;
void setup() {
size(800, 500);
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
hero = loadImage("Disintegrating-Soldier.jpg");
zombie = loadImage("msmile120.jpg");
}
void draw() {
background(800);
float difx = mouseX - xpos-hero.width/2;
if (abs(difx) > 1) {
xpos = xpos + difx/drag;
xpos = constrain(xpos, 0, width-hero.width);
}
float dify = mouseY - ypos-hero.height/2;
if (abs(dify) > 1) {
ypos = ypos + dify/drag;
ypos = constrain(ypos, 0, height-hero.height);
}
image(hero, xpos, ypos);
zombieMaker();
if (mouseX > width-size && mouseX < width+size &&
mouseY > height-size && mouseY < height+size){
headShot = true;
}
}
void zombieMaker()
{
if(deadZombie == false){
int size = 60;
xpos1 = xpos1 + ( xspeed * xdirection );
ypos1 = ypos1 + ( yspeed * ydirection );
}else { deadZombie = true;
}
if (xpos1 > width-size || xpos1 < 0) {
xdirection *= -1;
}
if (ypos1 > height-size || ypos1 < 0) {
ydirection *= -1;
}
image(zombie, xpos1, ypos1);
}
void mouseClicked() {
if (headShot) {
deadZombie = true;
}
}
Thanks!
essentially I'm making a game where I have a little character following my mouse around the screen with zombies bouncing all around him. what's suppose to happen is if the mouse clicks on the zombie, it goes away. However, when I click on the zombie, nothing happens. Can someone help me out?
PImage zombie;
boolean headShot = false;
boolean deadZombie = false;
int size = 60;
float xpos1, ypos1;
float xspeed = 2.8;
float yspeed = 2.2;
int xdirection = 1;
int ydirection = 1;
PImage hero;
float xpos;
float ypos;
float drag = 30;
void setup() {
size(800, 500);
frameRate(30);
smooth();
// Set the starting position of the shape
xpos = width/2;
ypos = height/2;
hero = loadImage("Disintegrating-Soldier.jpg");
zombie = loadImage("msmile120.jpg");
}
void draw() {
background(800);
float difx = mouseX - xpos-hero.width/2;
if (abs(difx) > 1) {
xpos = xpos + difx/drag;
xpos = constrain(xpos, 0, width-hero.width);
}
float dify = mouseY - ypos-hero.height/2;
if (abs(dify) > 1) {
ypos = ypos + dify/drag;
ypos = constrain(ypos, 0, height-hero.height);
}
image(hero, xpos, ypos);
zombieMaker();
if (mouseX > width-size && mouseX < width+size &&
mouseY > height-size && mouseY < height+size){
headShot = true;
}
}
void zombieMaker()
{
if(deadZombie == false){
int size = 60;
xpos1 = xpos1 + ( xspeed * xdirection );
ypos1 = ypos1 + ( yspeed * ydirection );
}else { deadZombie = true;
}
if (xpos1 > width-size || xpos1 < 0) {
xdirection *= -1;
}
if (ypos1 > height-size || ypos1 < 0) {
ydirection *= -1;
}
image(zombie, xpos1, ypos1);
}
void mouseClicked() {
if (headShot) {
deadZombie = true;
}
}
Thanks!
1

. Should look like this:
