Simple Event-Driven Program Help

I’m new to Lua and the Corona SDK. Currently I am working my way through the book Corona SDK Mobile GameDevelopment and Programming in Lua (mainly just using this book as a reference). Now I am starting to realize that event-driven programs can be written, but I am wondering how to structure such a program. Can anybody provide me with a link to an example?

Let’s say that I have created a couple of screen objects that have event listeners looking to see if the objects are touched and I want to call a function depending upon which object is touched. Is there some sort of simple do nothing program loop that would execute until an event occurs? Is there an instruction to halt the program and wait for an event? As you can tell, I’m clueless on how to structure a simple event-driven program. Any links to examples or a beginner tutorial would be helpful.

Well, you don’t need a program loop like you would in more traditional procedural languages unless you are using repetitive, time based functions.  For events attached to objects, you can do something as simple as:
 

function object1Clicked(event)
    print(“Object 1 clicked”)       
end
 
function object2Clicked(event)
    print(“Object 2 clicked”)   
end
 
local object1 = display.newImage(“object1.png”, 100, 100)
object1:addEventListener(“touch”, object1Clicked)                    
    
local object2 = display.newImage(“object2.png”, 200, 100)        
object2:addEventListener(“touch”, object2Clicked)                    

 
The program will simply execute the code and then wait for an event.  If you need a game loop for other purposes, you can use something like:
 

local timePrevious = system.getTimer()
 
local function gameLoop(event)    
    local timeDelta = event.time - timePrevious
    if timeDelta > 1000 then
        timePrevious = event.time
        print(“A second has passed”)
    end    
end
 
Runtime:addEventListener(“enterFrame”, gameLoop)

 
This is a more traditional game loop for Corona.  It executes the function move, checking the system clock for when a second has passed then executes the code, updating the check and printing to the console.  There are better examples than these, I was just trying to keep it simple.  Check the website documentation on Events for more information.

aven17 - thanks for your reply. The first examples you provided, along with the explanation, answered my question. I guess it will seem simple to me in the not too distant future. In addition to my more structured book reading, I’m also studying other people’s programs and then writing programs to verify that I truly understand it.  

One thing that I have noticed is that even the simple sample programs provided with Corona SDK is that the programmers tend to use a lot of tricks/shortcuts/more-advanced-methods;  they do stuff in 3 lines of code that would take me 10 lines of code. While it is good to see examples of such efficient programming, it makes it more difficult for beginners like me to figure out what is going on. I once made a living at writing industrial control programs using a “language” called ladder logic. It too has it’s tricks and shortcuts, but my goal was always to make my code understandable so that technicians and engineers could troubleshoot any problems. 10 lines of easy-to-understand code is better than 3 lines of code that few people can understand. Ok, that end my rather tame rant. 

Well I hope it helps.  It’s my first time responding to a help request.  I have been waiting for a response to my own question for a while now and happen to see yours.  I am pretty terrible about commenting code, but the website as well as the SDK itself both have some good example code.  For an example that uses both object events and a main loop, check the TimeAnimation example under the SampleCode/GettingStarted folder in your CoronaSDK directory.

Well, you don’t need a program loop like you would in more traditional procedural languages unless you are using repetitive, time based functions.  For events attached to objects, you can do something as simple as:
 

function object1Clicked(event)
    print(“Object 1 clicked”)       
end
 
function object2Clicked(event)
    print(“Object 2 clicked”)   
end
 
local object1 = display.newImage(“object1.png”, 100, 100)
object1:addEventListener(“touch”, object1Clicked)                    
    
local object2 = display.newImage(“object2.png”, 200, 100)        
object2:addEventListener(“touch”, object2Clicked)                    

 
The program will simply execute the code and then wait for an event.  If you need a game loop for other purposes, you can use something like:
 

local timePrevious = system.getTimer()
 
local function gameLoop(event)    
    local timeDelta = event.time - timePrevious
    if timeDelta > 1000 then
        timePrevious = event.time
        print(“A second has passed”)
    end    
end
 
Runtime:addEventListener(“enterFrame”, gameLoop)

 
This is a more traditional game loop for Corona.  It executes the function move, checking the system clock for when a second has passed then executes the code, updating the check and printing to the console.  There are better examples than these, I was just trying to keep it simple.  Check the website documentation on Events for more information.

aven17 - thanks for your reply. The first examples you provided, along with the explanation, answered my question. I guess it will seem simple to me in the not too distant future. In addition to my more structured book reading, I’m also studying other people’s programs and then writing programs to verify that I truly understand it.  

One thing that I have noticed is that even the simple sample programs provided with Corona SDK is that the programmers tend to use a lot of tricks/shortcuts/more-advanced-methods;  they do stuff in 3 lines of code that would take me 10 lines of code. While it is good to see examples of such efficient programming, it makes it more difficult for beginners like me to figure out what is going on. I once made a living at writing industrial control programs using a “language” called ladder logic. It too has it’s tricks and shortcuts, but my goal was always to make my code understandable so that technicians and engineers could troubleshoot any problems. 10 lines of easy-to-understand code is better than 3 lines of code that few people can understand. Ok, that end my rather tame rant. 

Well I hope it helps.  It’s my first time responding to a help request.  I have been waiting for a response to my own question for a while now and happen to see yours.  I am pretty terrible about commenting code, but the website as well as the SDK itself both have some good example code.  For an example that uses both object events and a main loop, check the TimeAnimation example under the SampleCode/GettingStarted folder in your CoronaSDK directory.