To improve my Lua coding, I’ve started a basic template that can be used for games and I’d welcome any suggestions or contributions about how to improve the code. My hope is that it will be a useful starting point for basic games.
Right now, it just loads a screen and two buttons then allows the user to move backward and forward between those screens.
The full project file can be found here:
http://www.lot49.com/corona_template.zip
The code includes some of the routines from tutorial projects. I’d be particularly interested in how to better structure this so I’m not using global functions. If anyone wants to improve this feel free to post below.
Thanks.
#
– Basic game template with five screens: mainScreen (1), playScreen (2), rulesScreen (3), settingsScreen (4), scoreScreen (5)
– iPad screen 1024-by-768-pixel resolution
– iPhone/iPod/Android touch 480-by-320-pixel resolution
– Load external button library (should be in the same folder as main.lua)
local ui = require(“ui”)
local sprite = require(“sprite”)
– Get screensize
local screenW = display.stageWidth
local screenH = display.stageHeight
– Initialize game code
local screenIndex = 1
local screenGroup = {
“mainScreen.png”,
“playScreen.png”,
“rulesScreen.png”,
“settingsScreen.png”,
“scoreScreen.png”
}
local onScreenGroup = display.newGroup()
for i=1,#screenGroup do
local s = display.newImage(screenGroup[i])
s.isVisible = false
onScreenGroup:insert(s)
end
local gameOverGroup = display.newGroup()
– Set sounds
–yourSound = media.newEventSound( “youSoundFile.caf” )
– Seed randomizer
local seed = os.time()
math.randomseed( seed )
–Hide Status Bar
display.setStatusBar( display.HiddenStatusBar )
–insert background image
–local backgroundGroup = display.newGroup()
–local background = display.newImage(“background.png”)
–backgroundGroup:insert( background )
– Game Restore/Save
local function shouldResume ()
–check
end
local function onSystemEvent (event)
if event.type == “applicationExit” then
–save to disk
elseif event.type == “applicationStart” then
if shouldResume () then
–load from disk
else
–start normally
end
end
end
– Runtime:addEventListener(“enterFrame”, onSystemEvent)
– Create 2 buttons
local button1Press = function( event )
print (“Button1 pressed”)
screenIndex = screenIndex - 1
if screenIndex == 0 then
screenIndex = #screenGroup
end
screenShuffle(screenIndex)
return true
end
local button1Release = function( event )
print (“Button1 released”)
end
local button2Press = function( event )
print (“Button2 pressed”)
screenIndex = screenIndex + 1
if screenIndex == #screenGroup + 1 then
screenIndex = 1
end
screenShuffle(screenIndex)
return true
end
local button2Release = function( event )
print (“Button2 released”)
end
local button1 = ui.newButton{
default=“button1.png”,
rollover=“button1_over.png”,
onPress=button1Press,
onRelease=button1Release,
}
local button2 = ui.newButton{
default=“button2.png”,
rollover=“button2_over.png”,
onPress=button2Press,
onRelease=button2Release,
}
– A layout trick to make buttons equally spaced horizontally
– Create an array for convenience and add buttons to it
local buttons = {}
buttons[#buttons + 1] = button1
buttons[#buttons + 1] = button2
local numButtons = #buttons
– Solving for buttonSpacing: screenW = (numButtons+1)*buttonSpacing + numButtons*buttonW
local buttonW = buttons[1].width
local buttonSpacing = ( screenW - numButtons*buttonW ) / ( numButtons + 1 )
– Layout buttons evenly across width of screen
local x = buttonSpacing + 0.5*buttonW
local y = screenH*0.93
for i=1,numButtons do
local button = buttons[i]
button.x = x
button.y = y
x = x + buttonSpacing + buttonW
end
–end button code
–swtich screens
function screenShuffle (a)
print (“screenShuffle”)
for i=1,#screenGroup do
local s = onScreenGroup[i]
if i == a then
s.isVisible = true
else
s.isVisible = false
end
end
if a == 1 then
Runtime:removeEventListener( “enterFrame”, gameOn )
mainScreen()
elseif a == 2 then
Runtime:addEventListener( “enterFrame”, gameOn )
playScreen()
elseif a == 3 then
Runtime:removeEventListener( “enterFrame”, gameOn )
rulesScreen()
elseif a == 4 then
Runtime:removeEventListener( “enterFrame”, gameOn )
settingsScreen()
else
Runtime:removeEventListener( “enterFrame”, gameOn )
scoreScreen()
end
end
–mainScreen
function mainScreen()
print (“mainScreen”)
– additional code
end
–end mainScreen
–playScreen
function playScreen()
print (“playScreen”)
– additional code
end
–end playScreen
–rulesScreen
function rulesScreen()
print (“rulesScreen”)
– additional code
end
–end rulesScreen
–settingsScreen
function settingsScreen()
print (“settingsScreen”)
– additional code
end
–end settingsScreen
–scoreScreen
function scoreScreen()
print (“scoreScreen”)
– additional code
end
–end scoreScreen
– debugging function
local function printTouch( event )
if event.target then
local bounds = event.target.stageBounds
local width = event.target.width
local height = event.target.height
print (“event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
print ("width: " … width … " height: " … height)
print (“event.target.x: " … event.target.x … " event.x:” …event.x)
print (“event.target.y: " … event.target.y … " event.y:” …event.y)
end
end
– Handle background touch
local function bgTouch( event )
local phase = event.phase
if “began” == phase then
print (“bgTouch”)
end
return true
end
– Handle object touches
local function onTouch( event )
print (“OnTouch”)
local t = event.target
local phase = event.phase
if “began” == phase then
– Make target the top-most object
–local parent = t.parent
–parent:insert( t )
display.getCurrentStage():setFocus( t )
– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
t.isFocus = true
– Store initial position
–t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
– Make object move (subtract t.x0,t.y0 so that moves are
– relative to initial grab point).
–t.x = event.x - t.x0
–t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end
local gameOver = function()
print (“gameOver”)
Runtime:removeEventListener( “enterFrame”, gameOn )
local gameOverImage = display.newImage(“gameover.png”)
gameOverGroup:insert( gameOverImage )
gameOverImage.alpha = 0
transition.to(gameOverImage, {time=2000, alpha = 1.0, transition = easing.outQuad} )
–gameOverImage:addEventListener( “touch”, restart )
end
function gameOn (event)
print (“gameOn”)
end
local function turn (event)
print( “turn check” )
if ( event.type == “portraitUpsideDown” ) then
print (“turn!”)
end
end
main = function()
print (“Main”)
screenShuffle(screenIndex)
–Runtime:addEventListener( “orientation”, turn )
–background:addEventListener( “touch”, bgTouch )
end
main ()
[import]uid: 1560 topic_id: 691 reply_id: 300691[/import]