Common Design Tasks

Pausing and Restarting Animations

If you have programmatic animations, you can pause and restart them. Here’s how you can make a button start and stop an animation:

local logo = display.newImage( "logo.png", 160, 240 )
function logo:enterFrame(event)
        -- do something like make the logo bounce around the edges of the screen 
end
 
Runtime:addEventListener( "enterFrame", logo );
 
function logo:tap( event )
        if logo.isPaused then -- initially nil which is false anyways
                Runtime:removeEventListener( "enterFrame", self )
        else
                Runtime:addEventListener( "enterFrame", self )
        end
        return true -- we handled the event so don't propagate
end
 
logo:addEventListener( "tap", logo )

Managing Screens

When you design your application interface, you’ll likely find yourself storyboarding in terms of “screens” such as the splash screen, the home screen (sometimes called the main screen or menu screen), or some other screen.

Group objects are the perfect way to manage the content for each screen. In this way, you can create a group for the splash screen, for the home screen, and for any other screen. You can then leverage the transition library to create animated transitions (fades, sliding, etc) back and forth between screens.

Saving Data on Application Exit

When you save data to a file, you need to decide where you are going to put it, what the name of the file is, and what data you need to save.

Typically, you would put this in the documents directory of your application's sandbox.

local path = system.pathForFile( "data.txt", system.DocumentsDirectory )
 
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
if file then
   -- read all contents of file into a string
   local contents = file:read( "*a" )
   print( "Contents of " .. path .. "\n" .. contents )
   io.close( file )
else
   -- create file b/c it doesn't exist yet
   file = io.open( path, "w" )
   local numbers = {1,2,3,4,5,6,7,8,9}
   file:write( "Feed me data!\n", numbers[1], numbers[2], "\n" )
   for _,v in ipairs( numbers ) do file:write( v, " " ) end
   file:write( "\nNo more data\n" )
   io.close( file )
end

Restoring Data on Application Launch

In certain situations, it is desirable to resume where the user left off between launches. In order to accomplish this, you need to register for the appropriate system events. Here is a skeleton of how your app might be structured:

local function shouldResume()
        -- return true or false depending on whether we need to resume
end
 
local function onSystemEvent( event )
        if event.type == "applicationExit" then
                -- save stuff to disk
        elseif event.type == "applicationStart" then
                if shouldResume() then 
                        -- load stuff off disk
                else
                        -- start app up normally
                end
        end
end
 
Runtime:addEventListener( "system", onSystemEvent );

Replies

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jhocking
User offline. Last seen 8 weeks 5 days ago. Offline
Joined: 4 Dec 2010

I think that top example is incomplete but I'm not sure. Shouldn't you have something like

1
logo.isPaused = not logo.isPaused

just before the return in logo:tap()?

dylanhong97
User offline. Last seen 1 year 10 weeks ago. Offline
Joined: 22 Jan 2011

i have a spinning picture in my application, when i tried to make it stop from a timer it stayed in the same position but was twitching back and forth, how can i stop this??? thanks

jlandyr
User offline. Last seen 49 weeks 1 day ago. Offline
Joined: 22 Jan 2011

hi, how can I pause a timer??? I'm using a timer in my game but I need pause this timer..