Particle effects
The Saxion GameApp includes a built-in particle engine that makes it easy to add cool visual effects to your games. Particle effects are small, animated graphics like sparks, smoke, fire, explosions, magic glows, or falling leaves. They are often used to make games feel more alive and visually appealing.
Instead of drawing each particle individually, a particle effect is a predefined system that controls many small images automatically. It handles things like position, speed, direction, size, color, and lifespan of the particles. For example, a fire effect might create orange sparks that rise and fade out over time, without you having to write all that logic manually.
Resources
If you want to play around with the functionalities without creating your own particles first, please download the flame-particle.zip here.
Creating particle effects (GDX Particle Editor)
The GDX Particle Editor is a tool that lets you create and customize particle effects visually. Instead of writing .p files by hand, you can design fire, smoke, sparks, or magic effects using sliders, color pickers, and preview windows.
You can download the editor here.
The following video gives you an introduction to the application and explains how you can create your own particles. You don't need to watch the part about the LibGDX code, since that has already been covered for you in our implementation. We use the tool only to create the .p files and the corresponding textures.
Loading a particle effect
Before using a particle effect, you need to load it:
// Load a particle effect named "flame" from the file "effects/flame.p"
GameApp.addParticleEffect("flame", "effects/flame.p");
- "flame" is the name you will use to spawn or manage the effect.
- "effects/flame.p" is the path to the particle file in your project.
⚠️ Note: The particle image files (.png) must be in the same folder as the .p file.
You only need to load an effect once. After that, you can spawn it multiple times.
Spawning Particle Effects
To make a particle effect appear in your game, use spawnParticleEffect():
// Spawn the "flame" effect at position x=300, y=200
long effectId = GameApp.spawnParticleEffect("flame", 300, 200);
- effectId is a unique ID for this specific spawn.
- This is a similar construction as with sound. The unique ID can be used later to stop this particular instance of the effect.
Stopping a Particle Effect
You can stop a specific particle effect using its ID:
You can also stop all active particle effects at once:
Updating particle effects
The particle effects know how they need to be updated (in which direction, etc.), but you have to call the update method in every frame:
This will update all the running particle effects. This ensures that particles move, fade, and disappear correctly.
Rendering particle effects
After updating, you need to draw the particle effects using the sprite batch:
GameApp.startSpriteBatch();
// Draw all active particle effects
GameApp.renderParticleEffects();
GameApp.endSpriteBatch();
Checking if a Particle Effect Exists
You can check if a particle effect has been loaded:
Dispose particle effect
You can also dispose the loaded particle effect when you are finished with it (e.g. in the hide() method). This will not stop the effect if it is still running!
⚠️ Note: Disposing an effect does not stop active spawns. You should stop them manually if needed.
Example: Particle effect screen
public class ParticleExampleScreen extends ScalableGameScreen {
public ParticleExampleScreen() {
super(1280, 720);
}
@Override
public void show() {
// Load a particle effect
GameApp.addParticleEffect("flame", "particles/flame.p");
// Spawn a particle effect at a fixed position
GameApp.spawnParticleEffect("flame", 400, 300);
}
@Override
public void render(float delta) {
super.render(delta);
GameApp.clearScreen();
// Update all active particle effects
GameApp.updateParticleEffects();
// Start sprite batch to render particles
GameApp.startSpriteRendering();
// Render all active particle effects
GameApp.renderParticleEffects();
GameApp.endSpriteRendering();
}
@Override
public void hide() {
// Dispose particle effects when screen is hidden
GameApp.disposeParticleEffect("fire");
}
}
Notes
- You can have multiple particle effects running at the same time.
- Effects will automatically stop when they finish if you don’t loop them.
- The .p file and its images must be in the same folder.