Creating Explosions and Debris
explosions_start.zip |
There are two ways for creating the illusion of an explosion.
The simplest way is using a support class provided by the creators of Greenfoot:
The simplest way is using a support class provided by the creators of Greenfoot:
- Simply add a new Explosion object to the world and run the scenario. You will hear the explosion as you create the Explosion object (the sound is played in the constructor) and then, when the scenario starts, you'll see the explosion image grow and remove itself from the world.
You can find the explosion class, image, and sound at the top of this page.
To add this to your scenario, drag the sound into the sounds folder, the .java file into your general project folder, and use "set image...">"import file" to add the image to your image folder.
- By simply adding an explosion object you create the explosion effect. If you want this in your scenarios, just add the Explosion class to your project, copy the explosion.wav sound and explosion.png image to the approximate folders in your project and you're good to go.
- The slightly more complicated approach is to create an explosion effect by replacing the object you're blowing up with a number of pieces of debris whose image is a distorted image based on the original object's image. In this example, we will blow up a rock.
- In the Rock class, I created an explode method, which plays the MetalExplosion.wav sound, removes the Rock object from the world and replaces it with 50 Debris objects.
- The Debris class uses an image which is just cut our of the Rock's image (using an image editing program like Gimp). In the Debris class' constructor, I set the object's initial rotation to a random angle, distort the image scaling the image to a random rectangle of size w by h, where w and h are random numbers, and set the object's initial speed to a random number. Then, in the act method, I make each debris object move in the direction it is facing and remove itself from the world when it touches an edge.
- You can recreate this effect using any object's image.