[lua]–maingame.lua
local storyboard = require( “storyboard” )
local scene storyboard.newScene()
local sprite = require “sprite”
local physics = require “physics”
local ui = require “ui”
local background
local ground
local charObject
local brknLychee
local pauseBtn
local pauseMenuBtn
local pauseBG
local menuBtn
local scoreText
local highScoreText
local lyText
local livesText
local shade
local gameOverScreen
local gameIsActive = false
local startDrop
local gameLives = 3
local gameScore = 0
local lyCount = 0
local highscore
local mRand = math.random
local lyDensity = 1.0
local lyShape = { -12, 13, 12,-13, 12,13, -12,13 }
local bagShape = { 15, -13, 65,-13, 65,13, 15,13 }
system.setAccelerometerInterval( 100 )
– load sounds
local lyCaughtSound = audio.localSound( “damLy.wav” )
local gameOverSound = audio.localSound( “gameover.wav” )
local btnSound = audio.loadSound( “btnSound.wav” )
– first add save values from beebes class
–***********************************************
–saveValue() --> used for saving high score, etc.
–***********************************************
local saveValue = function( strFilename, strValue )
– will save specified value to specified file
local theFile = strFilename
local theValue = strValue
local path = system.pathForFile( theFile, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “w+” )
if file then
–write game score to text file
file:write( theValue )
io.close( file )
end
end
–***************************************************
– loadValue() --> load saved value from file (returns loaded value as string)
–***************************************************
local loadValue = function( strFilename )
– will load specified file, or create new file if it doesn’t exist
local theFile = strFilename
local path = system.pathForFile( theFile, 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” )
io.close( file )
return contents
else
– create file b/c it doesn’t exist yet
file = io.open( path, “w” )
file:write( “0” )
io.close( file )
return “0”
end
end
– Called when the scene’s view does not exist:
function scene:createScene( event )
local gameGroup = self.view
– completely remove loadgame’s view
storyboard.removeScene( “loadgame” )
print( “\nmaingame: createScene event”)
end
– called imediately after scene has moved onscreen:
function scene:enterScene( event )
local gameGroup = self.view
local gameActivate = function()
gameIsActive = true
– show the pause button
pauseBtn.isVisible = true
pauseBtn.isActive = true
end
local moveChar = function(event)
charObject.x = display.contentCenterX - (display.contentCenterX * (event.yGravity*3))
if((charObject.x - charObject.width * 0.5) < 0) then
charObject.x = charObject.width * 0.5
elseif((charObject.x + charObject.width * 0.5) > display.contentWidth) then
charObject.x = display.contentWidth - charObject.width * 0.5
end
end
local setScore = function( scoreNum )
local newScore = scoreNum
gameScore = newScore
if gameScore < 0 then gameScore = 0; end
scoreText.text = "Score: " … gameScore
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = (scoreText.contentWidth * 0.5) + 15
scoreText.y = 15
end
local callGameOver = function()
audio.play( gameOverSound )
gameIsActive = false
physics.pause()
– make sure pause button is hidden/inactive
pauseBtn.isVisible = false
pauseBtn.isActive = false
shade = display.newRect( 0, 0, 570, 320 )
shade:setFillColor( 0, 0, 0, 255 )
shade.x = 240; shade.y = 160
shade.alpha =
gameOverScreen = display.newImageRect( “over.png”, 800, 640 )
local newScore = gameScore
setScore( newScore )
gameOverScreen.x = 400; gameOverScreen.y = 320
gameOverScreen.alpha = 0
gameGroup:insert( shade )
gameGroup:insert( gameOverScreen )
– Transition game over assets
transition.to( shade, { time=200, alpha= 0.65 } )
transition.to( gameOverScreen, { time=500, alpha=1 } )
scoreText.isVisible = false
scoreText.text = "Score: " … gameScore
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = 240
scoreText.y = 160
scoreText:toFront()
timer.performWithDelay( 0, function() scoreText.isVisible = true; end, 1 )
–Compare scores
if gameScore > highScore then
highScore = gameScore
local highScoreFilename = “highScore.data”
saveValue( highScoreFilename, tostring(highScore) )
end
– show high score
highScoreText = display.newText( "Best Game Score: " … highScore, 0, 0, “Arial”, 30 )
highScoreText:setTextColor( 255, 255, 255, 255 )
highScoreText.xScale = 0.5; highScoreText.yScale = 0.5
highScoreText.x = 240
highScoreText.y = 120
gameGroup:insert( highScoreText )
local onMenuTouch = function( event _
if event.phase == “release” then
audio.play( buttonSound )
storyboard.gotoScene( “mainmenu”, “fade”, 500 )
end
end
menuBtn = ui.newButton{
defaultSrc = “menubtn.png”,
defaultX = 60,
defaultY = 60,
onEvent = onMenuTouch,
id = “MenuButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
menuBtn.x = 100; menuBtn.y = 260
gameGroup:insert( menuBtn )
end
local drawBackground = function()
background = display.newImageRect( “bg1.png”, 853, 640 )
background.x 853; background.y = 640
gameGroup:insert( background )
ground = display.newImageRect( “collision.png”, 960, 150 )
ground.x = 960; ground.y = 150
ground.myName = “ground”
local groundShape = { -285,-18, 285,-18, 285,18, -285,18 }
physics.addBody( ground, “static”, { density=1.0, bounce=0, friction=0.5, shape=groundShape } )
gameGroup:insert( ground )
end
local hud = function()
lyText = display.newText( "Caught: " … eggCount, 0, 0, “Arial”, 45 )
lyText:setTextColor( 255, 255, 255, 255 )
lyText.xScale = 0.5; lyText.yScale = 0.5
lyText.x = (480 - (lyText.contentWidth * 0.5)) - 15
lyText.y 305
gameGroup:insert( lyText )
livesText = display.newText( "Lives: " … gameLives, 0, 0, “Arial”, 45 )
livesText:setTextColor( 255, 255, 255, 255 ) --> white
livesText.xScale = 0.5; livesText.yScale = 0.5
livesText.x = (480 - (livesText.contentWidth * 0.5)) - 15
livesText.y = 15
gameGroup:insert( livesText )
scoreText = display.newText( "Score: " … gameScore, 0, 0, “Arial”, 45 )
scoreText:setTextColor( 255, 255, 255, 255 ) --> white
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = (scoreText.contentWidth * 0.5) + 15
scoreText.y = 15
gameGroup:insert( scoreText )
local onPauseTouch = function( event )
if event.phase == “release” and pauseBtn.isActive then
audio.play( buttonSound )
– pause the game
if gameIsActive then
gameIsActive = false
physics.pause()
local function pauseGame()
timer.pause( startDrop )
print(“timer has been paused”)
end
timer.performWithDelay(1, pauseGame)
–Shade
if not shade then
shade = display.newRect( 0, 0, 570, 380 )
shade:setFillColor( 0, 0, 0, 255 )
shade.x = 240; shade.y = 160
gameGroup:insert( shade )
end
pauseBtn:toFront()
else
if shade then
display.remove( shade )
shade = nil
end
if pauseBG then
pauseBG.isVisible = false
pauseBG.isActive = false
end
gameIsActive = true
physics.start()
local function resumeGame()
timer.resume( startDrop )
print(“timer has been resumed”)
end
timer.performWithDelay(1, resumeGame)
end
end
end
pauseBtn = ui.newButton{
defaultSrc = “pausebtn.png”,
defaultX = 95,
defaultY = 95,
onEvent = onPauseTouch,
id = “PauseButton”,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
pauseBtn.x = 38; pauseBtn.y = 288
pauseBtn.isVisible = false
pauseBtn.isActive = false
gameGroup:insert( pauseBtn )
pauseBG = display.newImageRect( “pauseoverlay.png” 960, 640 )
pauseBG.x = 240; pauseBG.y = 160
pauseBG.isVisible = false
pauseBG.isActive = false
gameGroup:insert( pauseBG )
end
local livesCount = function()
gameLives = gameLives - 1
livesText.text = “Lives: " … gameLives
livesText.xScale = 0.5; livesText.yScale = 0.5 --> for clear retina display text
livesText.x = (480 - (livesText.contentWidth * 0.5)) - 15
livesText.y = 15
print(gameLives … " fruits left”)
if gameLives < 1 then
callGameOver()
end
end
local createChar = function()
local charObject = display.newImage(“basket1.png”, 215, 215, true )
charObject:prepare(“move”)
charObject:play()
charObject.x = 240; charObject.y = 250
physics.addBody( charObject, “static”, { density=1.0, bounce=0.4, friction=0.15, radius=20, shape=panShape} )
charObject.rotation = 0
charObject.isHit = false
charObject.myName = “character”
brknLy = display.newImageRect( “brknLy.png”, 100, 100 )
brknLy.alpha = 1.0
brknLy.isVisible = false
gameGroup:insert( charOject )
gameGroup:insert( brknLy )
end
local onLyCollision = function( self, event )
if event.force > 1 and not self.isHit then
audio.play( LyCaughtSound )
self.isHit = true
print( “Fruit destroyed!”)
self.isVisible = false
–self.isBodyActive = false
brknLy.x = self.x; brknLy = self.y
brknLy.alpha = 0
brknLy.isVisible = true
local fadeLy function()
transition.to( brknLy, { time=500, alpha=0 } )
end
transition.to( brknLy, { time=50, alpha1.0, onCompleteFadeLy } )
self.parent:remove( self )
self = nil
if event.other.Myname == “character” then
lyCount = lyCount + 1
lyText.text = "Caught: " … lyCount
lyText.Xscale = 0.5; lyText.yScake = 0.5 -->for clear retina display text
lyText.x = (480 - (lyText.contentWidth * 0.5)) - 15
lyText.y = 305
print(“fruit caught”)
local newScore = gameScore + 500
setScore( newScore )
elseif event.other.myName == “ground” then
livesCount()
print(“ground hit”)
end
if gameLives < 1 then
timer.cancel( startDrop )
print(“timer cancelled”)
end
end
end
local lyDrop = function()
local ly = display.newImageRect( “ly.png”, 0, 0 )
ly.x = 240 + mRand( 120 ); ly.y = -100
ly.isHit = false
physics.addBody( ly, “dynamic”, { density=lyDensity, bounce=0, friction=0.5, shape=lyShape } )
ly.isFixedRotation = true
gameGroup:insert( ly )
ly.postCollision = onLyCollision
ly:addEventListenr( “postCollision”, ly )
end
local lyTimer = function()
startDrop = timer.performWithDelay( 1000, lyDrop, 0 )
end
–Initializes all functions created
local gameStart = function()
physics.start( true )
physics.setGravity( 0, 9.8 )
drawBackground()
createChar()
lyTimer()
hud()
gameActivate()
Runtime:addEventListenr(“accelerometer”, moveChar)
– Loud high score for this level
local highScoreFilename = “highScore.data”
local loadedHighScore = loadValue( highScoreFilename )
highScore = tonumber(loadedHighScore)
end
gameStart()
print( “maingame: enterScene event” )
end
– called when scene is about to move offscreen:
function scene:exitScene( event )
print( “maingame: exitScene event” )
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addVentListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
[/lua]
When I run the main file lua the simulator goes blank. Did create a options, loadmainmenu, pause overlay, and config set for ipad size. But still doesn’t work. What am I doing wrong? [import]uid: 128294 topic_id: 29676 reply_id: 329676[/import]