Scrappy Time

by Scrappy Ferret

About

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

API

A full API listing can be found here.

Examples

Move a player object using time based velocity.

-- Load plugin library
require "plugin.scrappyTime"

-- 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 )

Trusted vendor

Documentation

$5.00

Log in now to purchase this plugin from Scrappy Ferret.

Once you've activated this plugin, appropriate build.settings code will be generated for you to copy into your Solar2D project. Download links will also be available for inclusion into a Solar2D Native project.

Latest Solar2D contributions by Scrappy Ferret view all