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]
[import]uid: 52491 topic_id: 30941 reply_id: 123732[/import]