We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to get a bomb to drop and when it hits the ground, it brings up an image of an explosion - the bomb resets and drops again.
NEXT I want a new random location for the bomb. I don't know where I am going wrong. I am trying to make this more efficient by using function for each thing that I am doing. I am sure there is something major wrong here, but I just can't make heads of tails of it right now . I don't know how to attach the two images.
I also had to make up my own TIME DELAY because I can't figure out how to do it.
start_drop=0
def setup():
global img, img2, random_start_x
size(400, 400)
background(255) # white
img=loadImage("bombsmall.jpg")
img2=loadImage("explosion.jpg")
random_start_x=int(random(1,400))
def draw():
global img, img2
random_start()
def random_start():
random_start_x=int(random(1,400))
print("This is the new start on x ", random_start_x)
bomb()
def bomb():
global start_drop, img, img2, random_start_x
background(255)
image(img, random_start_x, start_drop)
start_drop=start_drop+5
if start_drop>360:
c=0
while c<1000:
explosion()
print(c)
c=c+1
start_drop=0
random_start()
def explosion():
global img2
print("test")
img2=loadImage("explosion.jpg")
image(img2, random_start_x, 100)
Answers
Ok, I am breaking it down into easier to read code - just the basics now, with no images and no random functions.
ok, I am getting it. It was the while loop(which was my time delay attempt) that is causing the image problem, now onto the random placement.
Ok I figured it out, but without the time delay it doesnt look very good. The other issue is that when attempting to use functions (I commented out the functions I wanted to use), I cant seem to get it to work. Here is the working code. I wish I understood the original issue with the while loop, time delay and why when creating functions for each individual thing I want to happen (see explosion function below), things seem to go awry.
The image you see only updates at the end of draw. Having a delay inside draw just means is longer until you see that image. And any intermediate startups will be lost or will all appear at the same time.
Read the FAQ about setup and draw.
Thank you. I appreciate your patience in helping me understand this. I just finished reading the FAQ on setup and draw, but I am still having a hard time understanding this. I am looking at my latest post that is the stripped down version.
-Setup is just to load everything once - I load the images, size of window, strokeweight and random integer -Draw runs continually, like a movie. - this is going to run the code over and over.
So in my code, it goes through the setup first and then jumps to the DRAW function. It then draws the background (white) and then jumps to the BOMB() function.
In the BOMB() function, it runs it loads the bomb image and adds 5 on the y axis , it then jumps back to the draw function and add 5 again. This will run until it reaches the conditional statement of > 350. Once it reaches the bottom (350), it creates a new image (which is the explosion) and it starts all over again. In the conditional statement, right after the EXPLOSION image is where I need to put in a time delay of some sort.
Do I have this right so far?
I show how to manage a time delay properly in draw, or at least, one way to do it. Check it below.
Kf
You need to have a means to say that bomb has entered the EXPLODING state from its previous MOVING state.
It's a very good idea to define some
class
for your Bomb, so its attributes and states will be encapsulated in 1 place only.Here's an online example in Java for a fireworks animation.
It's complicated, but just as you can have some idea on how to deal w/ animation & state:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9Q6oRai8-41WJ/latest
Pay special attention to Firework::display() method:
It checks whether field hasExploded is
true
orfalse
.In this case, there are 2 states only: Either the Firework is in a rising() or in an exploding() animation.
There's also a 3rd state here too, which is isInactive.
It becomes
true
after the exploding() animation has finished.kfrajer ,
I copied/pasted your code and it works great! BUT I am still trying to figure this out.
If the bomb is in the air, and hasn't reached 350 units (the ground), then you set the target_time to whatever the milliseconds are + 2000. So from the very top, you are starting a timer by taking the computer time of milliseconds and adding 2000?
Else, the explosion is set to 1 (or similar to a boolean, TRUE FALSE, I believe)
If the target_time subtract milliseconds is greater than 0 (I am not understanding this part) and the bomb has HIT the ground (350), go to the EXPLOSION function. Else, set the explosion to ZERO
If the target_time subtract the milliseconds is LESS than 0 this time (again, I am not getting this part) and it has hit the ground (<350), repeat the animation, no explosion.
The explosion function, bringing up the image of fire.
So, when does the boolean of setExplosion = 0 or 1 come in? I am glad this works, but trying to understand HOW it is accomplishing the time delay is throwing me off. Where does the delay actually occur?