Certainly! Below is a comprehensive guide on how to create a particle system in GameMaker Language (GML) that emits particles within a sprite's area. The particles will change color based on a variable named side, which can be either "up" or "down". When side is "up", particles will be pink, and when side is "down", they will be blue.
Overview
- Object Setup: Create an object that holds the sprite and manages the particle system.
- Particle System Creation: Initialize and configure the particle system based on the
side variable.
- Emitting Particles: Emit particles within the sprite's boundaries.
- Dynamic Updates: Allow
side to change during gameplay, updating particle colors accordingly.
- Cleanup: Properly destroy the particle system when the object is removed.
Let's go through each step in detail.
1. Object Setup
First, create an object (e.g., obj_ParticleEmitter) and assign the desired sprite to it. This object will manage the particle system.
Example
// Object: obj_ParticleEmitter
// Assign a sprite in the Sprite tab, e.g., spr_Emitter
2. Particle System Creation
In the Create Event of obj_ParticleEmitter, initialize the particle system, define particle types (colors), and set up an emitter.
Create Event Code
// Create Event
// --- Variables ---
side = "up"; // Can be "up" or "down"; set dynamically as needed
// --- Create Particle System ---
ps = part_system_create();
part_system_depth(ps, -10); // Render particles behind the sprite; adjust as needed
// --- Create Particle Types ---
pt_up = part_type_create();
pt_down = part_type_create();
// Configure 'up' particle type (Pink)
part_type_shape(pt_up, pt_shape_circle);
part_type_size(pt_up, 0.5, 1, 0, 0); // Size between 0.5 and 1
part_type_color(pt_up, c_pink, c_pink); // Start and end color
part_type_alpha2(pt_up, 1, 0); // Fade from opaque to transparent
part_type_speed(pt_up, 50, 100, 0, 0); // Speed range
part_type_direction(pt_up, 270, 15, 0); // Direction downward with some variance
part_type_gravity(pt_up, 0, 200); // Gravity effect
// Configure 'down' particle type (Blue)
part_type_shape(pt_down, pt_shape_circle);
part_type_size(pt_down, 0.5, 1, 0, 0);
part_type_color(pt_down, c_blue, c_blue);
part_type_alpha2(pt_down, 1, 0);
part_type_speed(pt_down, 50, 100, 0, 0);
part_type_direction(pt_down, 90, 15, 0); // Direction upward with some variance
part_type_gravity(pt_down, 0, 200);
// --- Create Emitter ---
emitter = part_emitter_create(ps, x, y);
// --- Set Emitter Region to Sprite Bounds ---
var half_width = sprite_width * 0.5;
var half_height = sprite_height * 0.5;
part_emitter_region(ps, emitter, -half_width, half_width, -half_height, half_height, ps_shape_rectangle, ps_distr_linear);
// --- Start Emitting Particles ---
// Initially emit using 'up' particle type
current_pt = pt_up;
part_emitter_stream(ps, emitter, current_pt, 1); // 1 particle per step; adjust as needed
Explanation:
- Variables: Initialize
side to "up". This can be changed dynamically later.
- Particle System (
ps): Created and set to render behind the sprite (depth = -10).
- Particle Types (
pt_up, pt_down):
pt_up: Configured for pink particles.
pt_down: Configured for blue particles.
- Both types have similar properties but different colors and directions.
- Emitter: Positioned at the object's coordinates (
x, y).
- Emitter Region: Defined to match the sprite's width and height, ensuring particles emit within the sprite's area.
- Emitting Particles: Starts emitting particles using the initial
current_pt based on side.
3. Emitting Particles
Particles will be continuously emitted within the sprite's boundaries. The emission rate and other properties can be adjusted as needed.
Step Event Code
// Step Event
// Update Emitter Position if the object moves
part_emitter_region(ps, emitter, -sprite_width * 0.5, sprite_width * 0.5, -sprite_height * 0.5, sprite_height * 0.5, ps_shape_rectangle, ps_distr_linear);
part_system_position(ps, x, y);
// Example: Toggle 'side' every 120 frames (2 seconds at 60 FPS)
// Remove or modify this logic as per your game requirements
if (keyboard_check_pressed(vk_space)) { // For example, toggle on spacebar press
if (side == "up") {
side = "down";
current_pt = pt_down;
} else {
side = "up";
current_pt = pt_up;
}
// Update the emitter to use the new particle type
part_emitter_stream(ps, emitter, current_pt, 1);
}
Explanation:
- Emitter Position: Continuously updates the emitter's region in case the object moves or resizes.
- Dynamic
side Change: Example code toggles side between "up" and "down" when the spacebar is pressed. Adjust this logic based on your game's requirements.
- Updating Particle Type: When
side changes, update current_pt to the corresponding particle type and stream particles using the new type.
4. Drawing Particles
In the Draw Event, draw both the sprite and the particles.
Draw Event Code
// Draw Event
// Draw the sprite as usual
draw_self();
// Draw the particles
part_system_drawit(ps);
Explanation:
draw_self(): Renders the sprite normally.
part_system_drawit(ps): Renders all particles associated with the particle system ps.
5. Cleanup
To prevent memory leaks, ensure that the particle system and its components are properly destroyed when the object is removed.
Clean Event Code
// Clean Event
// Destroy the emitter
part_emitter_destroy(ps, emitter);
// Destroy particle types
part_type_destroy(pt_up);
part_type_destroy(pt_down);
// Destroy the particle system
part_system_destroy(ps);
Explanation:
- Emitter: Destroyed using
part_emitter_destroy().
- Particle Types: Both
pt_up and pt_down are destroyed.
- Particle System: Finally, the entire particle system
ps is destroyed.
Complete Example
Here's the complete code summarizing all events for clarity.
Create Event
// Create Event
// --- Variables ---
side = "up"; // Initial side
// --- Create Particle System ---
ps = part_system_create();
part_system_depth(ps, -10); // Render behind the sprite
// --- Create Particle Types ---
pt_up = part_type_create();
pt_down = part_type_create();
// Configure 'up' particle type (Pink)
part_type_shape(pt_up, pt_shape_circle);
part_type_size(pt_up, 0.5, 1, 0, 0);
part_type_color(pt_up, c_pink, c_pink);
part_type_alpha2(pt_up, 1, 0);
part_type_speed(pt_up, 50, 100, 0, 0);
part_type_direction(pt_up, 270, 15, 0); // Downward direction
part_type_gravity(pt_up, 0, 200);
// Configure 'down' particle type (Blue)
part_type_shape(pt_down, pt_shape_circle);
part_type_size(pt_down, 0.5, 1, 0, 0);
part_type_color(pt_down, c_blue, c_blue);
part_type_alpha2(pt_down, 1, 0);
part_type_speed(pt_down, 50, 100, 0, 0);
part_type_direction(pt_down, 90, 15, 0); // Upward direction
part_type_gravity(pt_down, 0, 200);
// --- Create Emitter ---
emitter = part_emitter_create(ps, x, y);
// --- Set Emitter Region to Sprite Bounds ---
var half_width = sprite_width * 0.5;
var half_height = sprite_height * 0.5;
part_emitter_region(ps, emitter, -half_width, half_width, -half_height, half_height, ps_shape_rectangle, ps_distr_linear);
// --- Start Emitting Particles with Initial Type ---
current_pt = pt_up;
part_emitter_stream(ps, emitter, current_pt, 1); // 1 particle per step
Step Event
// Step Event
// Update Emitter Position and Region
part_system_position(ps, x, y);
part_emitter_region(ps, emitter, -sprite_width * 0.5, sprite_width * 0.5, -sprite_height * 0.5, sprite_height * 0.5, ps_shape_rectangle, ps_distr_linear);
// Example: Toggle 'side' with Spacebar
if (keyboard_check_pressed(vk_space)) {
if (side == "up") {
side = "down";
current_pt = pt_down;
} else {
side = "up";
current_pt = pt_up;
}
// Update emitter to use the new particle type
part_emitter_stream(ps, emitter, current_pt, 1);
}
Draw Event
// Draw Event
// Draw the sprite
draw_self();
// Draw the particles
part_system_drawit(ps);
Clean Event
// Clean Event
// Destroy the emitter
part_emitter_destroy(ps, emitter);
// Destroy particle types
part_type_destroy(pt_up);
part_type_destroy(pt_down);
// Destroy the particle system
part_system_destroy(ps);
Additional Customizations
Emission Rate
Adjust the number of particles emitted per step by changing the last parameter in part_emitter_stream(). For example, to emit 5 particles per step:
part_emitter_stream(ps, emitter, current_pt, 5);
Particle Lifetime
Control how long particles exist by setting part_type_life. For example, to have particles live between 60 to 120 steps (1-2 seconds at 60 FPS):
part_type_life(pt_up, 60, 120);
part_type_life(pt_down, 60, 120);
Particle Speed and Direction
Modify part_type_speed and part_type_direction to change how particles move. For upward or downward movement, direction angles are typically:
- Upwards: 90 degrees
- Downwards: 270 degrees
Using Different Shapes
Instead of circles, use other shapes for particles:
part_type_shape(pt_up, pt_shape_square);
part_type_shape(pt_down, pt_shape_triangle);
Dynamic Sprite Size
If your sprite can resize during gameplay, ensure the emitter region updates accordingly (already handled in the Step Event).
Conclusion
This guide provides a complete setup for creating a dynamic particle system in GML that changes particle colors based on a variable. You can further customize the particle properties to fit the aesthetic and functional needs of your game. Remember to manage resources properly by cleaning up the particle system when it's no longer needed to prevent memory leaks.
If you have any specific requirements or encounter issues, feel free to ask for further assistance!