Egg Drop Example - for Corona SDK Mobile Game Development - Adapting to run in simulator

Hi. I am very new to Corona. I’ve been working my way through the Corona SDK Mobile Game Development book and I’m in chapter 7. The game relies n the Accelerometer for its character control and I only have a PC and the emulator right now. I’m trying to figure out how to make the game run in the simulator.

Here is what I have tried so far. I’ll list the code snippets I added and then the whole program, which has these snippets included. The program compiles with my snippets but it won’t move the character.

I would be really grateful if someone can show me and explain how to get the app running in the emulator on a PC. - Thanks - 1st post. Hope this is the right forum. I am an absolute beginner here.

snippet 1:

local isSimulator = “simulator” == system.getInfo(“environment”)

function dragChar(event)
if isSimulator then if event.phase ==“began” then
moveX = event.x - charObject.x
elseif event.phase == “moved” then
charObject.x = event.x - moveX
end
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
end

snippet 2: (towards end of code, (see below full program source)

function gameListeners(event)
if event == “add” then
paddle:addEventListener(“touch”, dragChar)

elseif event == “remove” then
paddle:removeEventListener(“touch”, dragChar)

end
end

Entire program source:
– Egg Drop Game

– Hide the status bar and add in display.newGroup() called gameGroup
display.setStatusBar( display.HiddenStatusBar )
local gameGroup = display.newGroup()

– Include the required external modules for the game
local sprite = require “sprite”
local physics = require “physics”
local ui = require “ui”

–Add in the display objects
local background
local ground
local charObject
local friedEgg
local scoreText
local eggText
local livesText
local shade
local gameOverScreen

– Add in variables
local gameIsActive = false
local startDrop
local gameLives = 3
local gameScore = 0
local eggCount = 0
local mRand = math.random

– Create the egg boundaries and density
local eggDensity = 1.0
local eggShape = {-12,-13, 12,-13, 12,13, 12,13 }
local panShape = {15,-13, 65,-13, 65,13, 15,13 }

– set up the accelerometer and audio
system.setAccelerometerInterval( 100 )
local eggCaughtSound = audio.loadSound( “friedEgg.wav” )
local gameOverSound = audio.loadSound( “gameover.wav” )

local isSimulator = “simulator” == system.getInfo(“environment”)

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

– create a local function to keep track of the score
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

– add the callGameOver() function here??? why here???
– ending game play
– have the Game Over sisplay screen pop up, display objects stop moving and event listeners deactivated
– add a sound notitidcaton to help triggerthe event

local callGameOver = function()

audio.play( gameOverSound )

gameIsActive = false
physics.pause()

shade = display.newRect( 0, 0, 570, 320 )
shade:setFillColor( 0, 0, 0, 255 )
shade.x = 240; shade.y = 160
shade.alpha = 0

gameOverScreen = display.newImageRect( “gameOver.png”, 400, 300 )

local newScore = gameScore
setScore( newScore )

gameOverScreen.x = 240; gameOverScreen.y = 160
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 )
end

– create the background imagery
– create a local function called drawBackground()

local drawBackground = function()
– add in the background image
background = display.newImageRect( “bg.png”, 480, 320 )
background.x = 240; background.y = 160

–add the ground elements and create the ground boundaries and close the function
gameGroup:insert( background )

ground = display.newImageRect( “grass.png”, 480, 75 )
ground.x = 240; ground.y = 325

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
– here is the old code which has a problem, See if you can figure out why it doesn’t work. Currently i don’t see it.

–[[local drawBackground = function()

– add in the background image
background = display.newImageRect( “bg.png”, 480, 320)
background.x = 240; background.y =160

– add the ground elements and create the ground boundaries and close the function
gameGroup:insert( background )

ground = display.newImageRect( “grass.png”, 480, 75 )
ground.x = 240; ground.y =325

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 ]]–
– create the heads up display for game information

local hud = function()

– display the text for eggs that are caught

eggText = display.newText( "Caught: " …eggCount, 0, 0, “Arial”, 45)
eggText:setTextColor( 255, 255, 255, 255 )
eggText.xScale = 0.5; eggText.yScale = 0.5
eggText.x = (480 - (eggText.contentWidth * 0.5)) - 15
eggText.y = 305
gameGroup:insert( eggText )

– add in the text to track the lives
livesText = display.newText( "Lives: " … gameLives, 0, 0, “Arial”, 45 )
livesText:setTextColor( 255, 255, 255, 255 ) --> white
livesText.xScale = 0.5; livesText.yScale = 0.5 --> for clear retina display ??
livesText.x = ( 480 - ( livesText.contentWidth * 0.5)) -15
livesText.y = 15
gameGroup:insert( livesText )

– add in text for the score and close the function
scoreText = display.newText( "Score: "… gameScore, 0, 0, “Arial”, 45)
scoreText:setTextColor( 255, 255, 255, 255 )
scoreText.xScale = 0.5; scoreText.yScale = 0.5
scoreText.x = (scoreText.contentWidth * 0.5) + 15
scoreText.y = 15
gameGroup:insert( scoreText )
end

– keep track of the lives of the main character

local livesCount = function()
gameLives = gameLives -1
livesText.text = “Lives: “… gameLives
livesText.xScale = 0.5; livesText.yScale = 0.5
livesText.x = (480 - (livesText.contentWidth * 0.5)) - 15
livesText.y = 15
print(gameLives …” eggs left”)
if gameLives < 1 then
callGameOver()
end
end

– creating the main character

local createChar = function()

– create the sprite sheet for the main character
local characterSheet = sprite.newSpriteSheet( “charSprite.png”, 128, 128)
local spriteSet = sprite.newSpriteSet(characterSheet, 1, 4)
sprite.add( spriteSet, “move”, 1, 4, 400, 0)
charObject = sprite.newSprite( spriteSet )
charObject:prepare(“move”)
charObject:play()
charObject.x = 240; charObject.y = 250
physics.addBody( charObject, “static”, {
density=1.0,
bounce=0.4,
friction=0.15,
shape=panShape } )
charObject.rotation = 0
charObject.isHit = false
charObject.myName = “character”

– add in the transition image after the egg made a collission
friedEgg = display.newImageRect( “friedEgg.png”, 40, 23)
friedEgg.alpha = 1.0
friedEgg.isVisible = false
gameGroup:insert( charObject )
gameGroup:insert( friedEgg )
end

– creating the egg collision
local onEggCollision = function ( self, event )
if event.force > 1 and not self.isHit then
audio.play( eggCaughtSound )
– make self invisible and inactive and replace it with the friedEgg display object
self.isHit = true
print( “Egg destroyed!”)
self.isVisible = false
friedEgg.x = self.x; friedEgg.y = self.y
friedEgg.alpha = 0
friedEgg.isVisible = true

local fadeEgg = function()
transition.to( friedEgg, { time=500, alpha=0 } )
end
transition.to( friedEgg, { time=50, alpha=1.0, onCompelte=fadeEgg } )
self.parent:remove( self )
self = nil

– using if event.other.myName == “character” update the eggCount when the character
– catches eggs and update the score by 500 points on every collission

if event.other.myName == “character” then
eggCount = eggCount + 1
eggText.text = "Caught: " … eggCount
eggText.xScale = 0.5; eggText.yScale = 0.5
eggText.x = (480 - (eggText.contentWidth * 0.5)) - 15
eggText.y = 305
print(“egg 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

– Add the Eggdrop Object
local eggDrop = function()
local egg = display.newImageRect( “egg.png”, 26, 30)
egg.x = 240 + mRand( 120 ); egg.y = -100
egg.isHit = false

physics.addBody( egg, “dynamic”, { density=eggDensity, bounce=0, friction=0.5, shape=eggShape } )
egg.isFixedRotation = true
gameGroup:insert( egg )

– add in the postCollision event for the egg display object
egg.postCollision = onEggCollision
egg:addEventListener( “postCollision”, egg )
end
– make the egg drop once every second
local eggTimer = function()
startDrop = timer.performWithDelay( 1000, eggDrop, 0 )
end

– withing the first if statement in the onEggCollision() function, cancel the timer using
– timerID, startDrop.
– Add the statement if gameLives < 1 to the eggs from falling

if gameLives < 1 then
timer.cancel( startDrop )
print(“timer cancelled”)
end

– Activate the game

local gameActivate = function()
gameIsActive = true
end

– Initialize all the games actinos by making a new functin called gameStart()

local gameStart = function()
– start the physics property and set the gravity for the falling eggs
physics.start( true )
physics.setGravity(0, 9.8)
– to make sure that your physical object boundaries for your display objects are in the right place
– use physics.setDrawMode(“hybrid”)
physics.setDrawMode( “hybrid” )

– Activate all the functions instantiated.
– Add an event listener for charObject using the “touch” for the moveChar function.
drawBackground()
createChar()
eggTimer()
hud()
gameActivate()
Runtime:addEventListener(“accelerometer”, moveChar)

end

function gameListeners(event)
if event == “add” then
paddle:addEventListener(“touch”, dragChar)

elseif event == “remove” then
paddle:removeEventListener(“touch”, dragChar)

end
end

– Instantiate the gameStart() function and return the group gameGroup

gameStart()
return gameGroup

[import]uid: 173948 topic_id: 30941 reply_id: 330941[/import]

Hey there,

Fine forum to post in although that code isn’t readable - you need to put code inside tags;
< lua > code here < /lua >
(Without the spaces, of course.)

The accelerometer wont actually work on the simulator on Mac or PC, you should install on device to test that.

Peach :slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 123732[/import]

Hi guyduff,

This is what I did to control the character by dragging:
This is in the enterScene function
[lua] local moveChar = function(event)

–charObject.x = display.contentCenterX - (display.contentCenterX * (event.yGravity * 3)) --> move with accelerometer

if event.phase == “began” then --> move with touch command
moveX = event.x - charObject.x
elseif event.phase == “moved” then
charObject.x = event.x - moveX
end

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[/lua] [import]uid: 146966 topic_id: 30941 reply_id: 123815[/import]

Thanks Peach. I’ll make sure to enclose furture code in [lua] [/lua]. I’m just learning corona now and for development purposes I’d like to be able to rapidly test in the simulator before porting to the device and so I need a work around to test without the simulator so that I can do quick testing.

I will try it with a device as soon as I get my Android in next week. See you around and thanks again. Have a great weekend!

Guy :slight_smile:

[import]uid: 173948 topic_id: 30941 reply_id: 123883[/import]

Hey there,

Fine forum to post in although that code isn’t readable - you need to put code inside tags;
< lua > code here < /lua >
(Without the spaces, of course.)

The accelerometer wont actually work on the simulator on Mac or PC, you should install on device to test that.

Peach :slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 123732[/import]

Hi guyduff,

This is what I did to control the character by dragging:
This is in the enterScene function
[lua] local moveChar = function(event)

–charObject.x = display.contentCenterX - (display.contentCenterX * (event.yGravity * 3)) --> move with accelerometer

if event.phase == “began” then --> move with touch command
moveX = event.x - charObject.x
elseif event.phase == “moved” then
charObject.x = event.x - moveX
end

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[/lua] [import]uid: 146966 topic_id: 30941 reply_id: 123815[/import]

@Guy - if you have an iOS device there is Corona Remote which may be worth a look, else you do need to use a device, yes. (The iOS simulator can’t do tilt either, obviously, not just Corona.)

Let me know how you get on, hope you weekend was most enjoyable!

Peach :slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 124256[/import]

Thanks Peach. I’ll make sure to enclose furture code in [lua] [/lua]. I’m just learning corona now and for development purposes I’d like to be able to rapidly test in the simulator before porting to the device and so I need a work around to test without the simulator so that I can do quick testing.

I will try it with a device as soon as I get my Android in next week. See you around and thanks again. Have a great weekend!

Guy :slight_smile:

[import]uid: 173948 topic_id: 30941 reply_id: 123883[/import]

@Guy - if you have an iOS device there is Corona Remote which may be worth a look, else you do need to use a device, yes. (The iOS simulator can’t do tilt either, obviously, not just Corona.)

Let me know how you get on, hope you weekend was most enjoyable!

Peach :slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 124256[/import]

Peach I don’t mean to be nasty, but could you please read the poor guys post before making pointless comments. The guy seems well aware that you can’t use the accelerometer on the simulator. His post says he is trying to make it run in the simulator ie: “simulate” the movement of the accelerometer in the simulator. He says he doesn’t have a device, so why on earth would you tell him that he should install it on one?

Guyduff then says he is getting an android, so you start talking about iOS. Why? Then you tell him again that he needs a device…umm, so what is the simulator for? Yes if he need to test the accelerometer function sure, but all he is trying to do is get it moving on the simulator, I’m pretty darn sure that he is not trying to tilt his PC to get this working.

I’m coming from the same place as this guy - I’m a beginner with a PC, no device, and that stupid book Corona SDK Mobile Game Development. Earlier in the book it gets you program in the accelerometer and then so you can test it in the simulator (without accelerometer) it gets you to program a few lines starting with “if isSimulator then”. With the Egg Drop game it asks you to work out the simulator equivalent to the accelerometer for yourself, and doesn’t give you the answer if you get stuck.

Again I don’t mean to be nasty but if there is one thing I hate on the internet is when someone has a genuine question and all they get back is completely unhelpful comments that go off on a tangent that has nothing to do with the question. All I can say is thankgoodness for maher_c - he or she has actually answered his question. [import]uid: 191776 topic_id: 30941 reply_id: 129436[/import]

Peach I don’t mean to be nasty, but could you please read the poor guys post before making pointless comments. The guy seems well aware that you can’t use the accelerometer on the simulator. His post says he is trying to make it run in the simulator ie: “simulate” the movement of the accelerometer in the simulator. He says he doesn’t have a device, so why on earth would you tell him that he should install it on one?

Guyduff then says he is getting an android, so you start talking about iOS. Why? Then you tell him again that he needs a device…umm, so what is the simulator for? Yes if he need to test the accelerometer function sure, but all he is trying to do is get it moving on the simulator, I’m pretty darn sure that he is not trying to tilt his PC to get this working.

I’m coming from the same place as this guy - I’m a beginner with a PC, no device, and that stupid book Corona SDK Mobile Game Development. Earlier in the book it gets you program in the accelerometer and then so you can test it in the simulator (without accelerometer) it gets you to program a few lines starting with “if isSimulator then”. With the Egg Drop game it asks you to work out the simulator equivalent to the accelerometer for yourself, and doesn’t give you the answer if you get stuck.

Again I don’t mean to be nasty but if there is one thing I hate on the internet is when someone has a genuine question and all they get back is completely unhelpful comments that go off on a tangent that has nothing to do with the question. All I can say is thankgoodness for maher_c - he or she has actually answered his question. [import]uid: 191776 topic_id: 30941 reply_id: 129436[/import]

:slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 129552[/import]

:slight_smile: [import]uid: 52491 topic_id: 30941 reply_id: 129552[/import]