#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Explosion doesn't happen after targethit but after shoot button is pressed after targethit

edited January 2017

The problem here is that no explosion is happening when target is hit.

Here's what happens, I "shoot" with LMB, bullet shoots, hits target and there should be an explosion, but no explosion happens. The explosion only happens if I press the "shoot" (LMB) button again.

What can I do to make the explosion explode as it should?

Second half of the code

edit: my code gets extremely bugged if i post it as a code(wtf) so ill try as a quote

Tagged:

• edited January 2017
``````void drawProjectile() {
if (objectInMotion) {
x-=speed * cos(angleAlpha)*time; // Invert sign because the movement is in inverse direction
y-=speed * sin(angleAlpha)*time; // Invert sign because the movement is in inverse direction
y+=0.5*gravity*time*time; // calculate Y by the expression: Y = Vy*t - 0.5*g*t*t (g points in the direction of the  Y axes)
y -= windSpeed*time;  // Positiv direction of the wind is from the south - reverse in comparisson to the axes Y
time+=timeStep;
stroke(0, 0, 0);
fill(255, 0, 0); // red color for the object
if (!explode) ellipse(x, y, r, r);

//     Try  to animate rectangle also
//     if (!explode){
//     rectMode(CENTER);
//     rect(x,y, 3*r,r);
//    rectMode(CORNER);
//     }

//     Try  to animate rectangle with rotation
if (!explode) {
pushMatrix();
translate(x, y);
rotate(angleAlpha-time*0.3);
rectMode(CENTER);
rect(0, 0, 3*r, r);
rectMode(CORNER);
popMatrix();
}

if ((x<targetDistance) && (y>height/11/2 && y < height-height/11/2)) {
explodePositionX = targetDistance; // if target is hit - activete explosion at current position
explodePositionY = y;
explode = true;
objectInMotion = false; // stop the movement of the object
}

if (x<0) objectInMotion = false; // stop the movement of the object if the target is not hit and object crosses the left boundary
if ((x<targetDistance) && (y>height/11/2 && y < height-height/11/2)) {
explodePositionX = targetDistance;
explodePositionY = y;
explode = true;
targetHit = true;
objectInMotion = false;
if (targetHit)
for (int i = 1; i <= 10; i++) {
if (y>height/22*i && y<height-height/22*i) {
Score++;
Total++;
}
}
}

drawExplosion();
}
}

// Draw esplosion, if collision is detected
void drawExplosion() {
if (explode) {
noStroke();
fill(255, explosionStrength*2, 0);
ellipse(explodePositionX, explodePositionY, explosionStrength, explosionStrength);
explosionStrength+=5;
//   delay(2);
if (explosionStrength>100) {
explode=false;
explosionStrength=1;
}
}
}
``````
• edit post, highlight code, press ctrl-o to format.

• same code, different poster here:

https://forum.processing.org/two/discussion/20548/need-desperate-help-figuring-out-this-game

says it's a school project...

• @koogs, when I press the ctrl+o to format it gets bugged

and it must be someone from my group, the code isn't the "same", the assignment is we have a base code and we must add/edit code

my code is almost how it needs to be except for some small problems, I just can't figure out how to get the explosion correct

• unless you post runnable code we are limited in what we can do

• or at least code that executes the explosion methods. it doesn't have to be the entire cannon code, just the explosion bit. but it should be runnable.

• @koogs can I pm you the code? If I post a runable code I'd have to post the entire code which has had a lot of work and changes done to it

• no

reduce the code to the bits that need debugging. the firing, the bullet stuff, none of that matters for debugging this bit. just enough code to exercise drawExplosion().

briefly looking at this you'd need:

explode = true
explosionStrength
explodePositionX and explodePositionY

• and if drawExplosion is ok then it's how you're calling it that's broken.

unit testing...

• looks ok. i've not changed drawExplosion (except to add some spaces to make it more readable, and to fix the spelling in the comment). press a key to explode...

``````boolean explode;
float explodePositionX, explodePositionY;
float explosionStrength;

void setup() {
size(400, 400);
}

void draw() {
background(0);
drawExplosion();
}

void keyPressed() {
explode = true;
explodePositionX = width / 2;
explodePositionY = height / 2;
explosionStrength = 1;
}

// Draw explosion, if collision is detected
void drawExplosion() {
if (explode) {
noStroke();
fill(255, explosionStrength * 2, 0);
ellipse(explodePositionX, explodePositionY, explosionStrength, explosionStrength);
explosionStrength += 5;
//   delay(2);
if (explosionStrength > 100) {
explode = false;
explosionStrength = 1;
}
}
}
``````

so check how you're calling it. you may be invalidating some of the global variables this relies on in other code (which is why i'd be tempted to move this into a class of its own).

• The explosion logic is similar to fireworks. It needs state check in order to know what to do:
• `if (explode)` is enough of a state check imo.