Reputation:
I wrote code that is supposed to remove 25 HP (health points) each time a rectangle (created by pressing the m key) intersects another rectangle, the enemy. Currently, the enemy looses 25HP continuously after I've pressed the m key only once until I press the m key again outside of the enemy body.
This is my damage code for the enemy. It turns white when killed, that's why I have df1
etc. there.
void damage() {
//if (callMethod) {
HP -=25;
//callMethod = false;
System.out.println(" " + HP);
//}
if (HP == 0) {
df1 = 200;
df2 = 200;
df3 = 200;
}
}
This is the code for the m input.
void Fight() {
if (keyPressed) {
if (key == 'm'|| key == 'M') {
//villkor, flytta höger, X-led.
fill(255, 0, 0, 63);
noStroke();
rect(xF, yF, wF, hF);
xFF = xF;
yFF = yF;
wFF = wF;
hFF = hF;
}
}
}
Here I have my intersect code:
if (g.intersect(f)) {
f.damage();
}
I would appreciate any help I can get. Apologies for my bad English grammar :)
Upvotes: 0
Views: 183
Reputation: 42174
You can use another boolean
variable that tracks whether the action was already taken.
Here's a small example:
boolean alreadyPressed = false;
void draw() {}
void mousePressed() {
if(!alreadyPressed){
background(random(255));
}
alreadyPressed = true;
}
You can then reset the boolean
variable whenever you want to be eligible to detect the event again.
Upvotes: 0