Site icon FOKA: Foxtrot Oscar Kilo Alpha

deltaTime: Corona SDK plugin for time based animations

I’ve recently published a small but handy plugin in the Corona plugin directory. The deltaTime plugin allows developers to turn regular animations into time-based animations that will play back at the same speed regardless of framerate variations. This is important because you always want to have your objects move at the same speed regardless of what the framerate is. Even when you set your app’s framerate at a fixed number it never really runs at exactly that speed; sometimes it’s faster and sometimes it’s slower. Even worse, the framerate varies considerably in cellphones as other processes are most likely running in the background and using CPU cycles for something other than your app. Using this library helps in making all animations run at the same apparent speed even when your framerate varies. After this library is properly set up (don’t worry it’s incredibly easy) you should be able to change your framerate from 30 to 60 or vice versa and your animations should run at exactly the same speed.

Usually you would animate a Corona DisplayObject like this:

local speed = 5
obj.x = obj.x + speed

Or like this:

local speed = 5
obj:translate(speed, 0)

And that’s fine, except that every frame your object will move a fixed amount of points. If your framerate slows down then your object will move slower and if your framerate goes up then the object will move faster.

With the deltaTime plugin you can make this animation run at the same speed regardless of your framerate. But first you need to make sure your Corona project is ready for it. First you need to go to the Corona Plugin Directory and activate the plugin (click the green Free icon). It’s absolutely free and you need to do this only once per Corona account even if you use the plugin for many projects.

Second you need to add the plugin to the build.settings file so that the Corona server adds it to your build.

settings = {
    -- You may of course have other keys before, after 
    -- or even inside your "plugins" key.
    plugins = {
        ["plugin.deltatime"] = {
            publisherId = "com.julianvidal",
        },
    },      
}

And lastly you need to load the plugin just like any module and start it.

local deltatime = require "plugin.deltatime"
deltatime.start()

Now instead of making your objects move by a fixed amount you have to make sure that your movements take into account the time multiplier. This is very straight forward:

local speed = 5 * deltatime.multiplier()
object:translate(speed, 0)

That’s it! If your framerate drops, say by half, the multiplier will double so your speed variable will double too and your animations will continue to run at the same speed.

 

 

 

 

Exit mobile version