Scrappy Time

Scrappy Time is a simple time management library for Solar2D that simplifies time based movement and FPS calculation.

As with all my libraries it is free to use however if you find it useful, and are able to, I’d appreciate a coffee, or if you like you could purchase the plugin.

Image Credit: Time free by ProSymbols from the Noun Project.

Getting Started

In order to use the library you will need to include it in your project, to do so please see this guide.


API

Scrappy.Time:init()

Initiates the library.


Scrappy.Time:delta()

Gets the current time delta for the game.


Scrappy.Time:fps()

Gets the fps of the game.


Examples

Move a player object using time based velocity.

-- Initiate the time library
Scrappy.Time:init()

-- Create a 'player' object
local player = display.newRect( display.contentCenterX, display.contentCenterY, 10, 10 )

-- Set up a table for the player's velocity
player.velocity = { x = 0, y = 0 }

-- enterFrame handler
function onEnterFrame()

	-- Move the player along the x/y axis using their velocity adjusted by the current delta time
    player.x = player.x + ( player.velocity.x * Scrappy.Time:delta() )
    player.y = player.y + ( player.velocity.y * Scrappy.Time:delta() )

	-- Output the current fps
	print( string.format( "FPS: %d", Scrappy.Time:fps() ) )

end


-- onKey handler
function onKey( event )

	-- Set the player's velocity depending on the key, making sure to set it to 0 on key release
	-- Please note: This is a terrible way to do this but it's good enough for our purposes
    if event.keyName == "up" then
		player.velocity.y = event.phase == "down" and -1 or 0
	elseif event.keyName == "down" then
		player.velocity.y = event.phase == "down" and 1 or 0
	elseif event.keyName == "left" then
		player.velocity.x = event.phase == "down" and -1 or 0
	elseif event.keyName == "right" then
		player.velocity.x = event.phase == "down" and 1 or 0
	end

end

-- Register some events
Runtime:addEventListener( "enterFrame", onEnterFrame )
Runtime:addEventListener( "key", onKey )