Flambe code snippet: Control overall speed

Code snippets, Haxe

flambe-speedadjusterFlambe has a nice specialized component called SpeedAdjuster. It adjusts the update speed of an entity (and its components and children), and can be used for slow – motion and fast forward effects.

Sometimes it helps to see things at very slow speed. You can use the SpeedAdjuster to debug your game. I wrote a function that gives you control over the speed using [ and ] keys on your keyboard.

// press [ or ] to decrease or increase speed. returns SignalConnection
function addDebugSpeedAdjuster(entity:Entity)
{
if (!entity.has(SpeedAdjuster)) entity.add(new SpeedAdjuster(1));
return System.keyboard.down.connect(function(event:KeyboardEvent)
{
var speedAdjuster = entity.get(SpeedAdjuster);
if (event.key == Key.LeftBracket)
{
speedAdjuster.scale._ /= 1.61803398875;
trace(“Speed Adjuster scale: ” + entity.get(SpeedAdjuster).scale._);
}
else if (event.key == Key.RightBracket)
{
speedAdjuster.scale._ *= 1.61803398875;
trace(“Speed Adjuster scale: ” + entity.get(SpeedAdjuster).scale._);
}
});
}

If you want to use it in debug-mode to control the entire speed of everything, you can use it like this.

#if debug
addDebugSpeedAdjuster(System.root);
#end

Of course, make sure all your components make use of the deltatime from the onUpdate function.

Learn more about Flambe in this guide.

Say something interesting

Please link to code from an external resource, like gist.github.com.