2021-10-26

Sprite Trails

Overview

Hi! Less of a tutorial, but here’s a script you can use in GMS2.3 for sprite trails.

Code

/// Function
function draw_trail(_frameLen, _spacing = 0) {
	if (self[$ "__trail"] == undefined) {
		__trail = {
			frameArr : [],
			frameLen : 0,
			spacing : 0,
		}
	}
	
	if (__trail.spacing <= 0) {
		__trail.spacing = _spacing;
		array_push(__trail.frameArr, {
			x: x,
			y: y,
			sprite_index: sprite_index,
			image_index: image_index,
			image_xscale: image_xscale,
			image_yscale: image_yscale,
			image_angle: image_angle,
			image_blend: image_blend,
			image_alpha: image_alpha,
		});
		__trail.frameLen ++;
	} else { __trail.spacing--; }
	
	while (__trail.frameLen > _frameLen) {
		array_delete(__trail.frameArr, 0, 1);
		__trail.frameLen --;
	}
	
	for (var _i = 0; _i < __trail.frameLen; _i++) {
		var _frame = __trail.frameArr[_i];
		draw_sprite_ext(_frame.sprite_index, 
		                _frame.image_index, 
						_frame.x, 
						_frame.y, 
						_frame.image_xscale, 
						_frame.image_yscale, 
						_frame.image_angle, 
						_frame.image_blend, 
						_frame.image_alpha * (_i/_frameLen));
	}	
}
/// Usage
// frameLen = number of total frames
// spacing = how many game frames are between each frame; default value is 0
draw_trail(frameLen, spacing);

Example

Example Here, I set the total number of frames to 10, with a delay of 5 frames.

Final Thoughts

I got nothing to say; sprite trails look cool.
Have fun! -AH