Game speed increases itself on every restart - Corona - lua

am trying to make a jetpack joyride concept game on corona. First of all unlike other sdk where when you change a scene, the previous scene is automatically removed, in corona you have to remove each and object and function yourself.
After a week I was finally able to remove objects while changing scene, but now I have ran into a new problem. Everytime when I change scenes and come back to the gameScreen the speed of lasers and missiles increases every time ( maybe twice? ). How can I reset its speed?
I even added another "gameStatus" variable to remove the event listener and tried to print it, but game status never actually returned "ended", it was always "running". Here is the gameScene screen where everything happens
EDIT : I just noticed something, For example if 3 objects have already spawned then after reloading the screen only first 3 objects seem to be fast and its normal from fourth.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require( "physics" )
physics.start()
local image, text1
local function onSceneTouch( self, event )
if event.phase == "began" then
composer.gotoScene( "startScreen", "fade", 400 )
return true
end
end
function scene:create( event )
local sceneGroup = self.view
-- Variable
---------------------------------------
sW = display.contentWidth
sH = display.contentHeight
-- trying to reset gameStatus on restart
gameStatus = "ended"
gameStatus = "running"
--------------------------------------------------------
backgroundTiles = {}
drawBackground = function()
cieling = display.newRect( 0, 0, 2 * sW, sH * 0.05)
physics.addBody( cieling, "static", { density=0, friction=0, bounce=0 } )
cieling.x, cieling.y = cieling.contentWidth / 4, cieling.contentHeight * 0.25
cieling.alpha = 0
cieling.name = "ceiling"
ground = display.newRect( 0, sH, 2 * sW, sH * 0.05)
physics.addBody( ground, "static", { density=3, friction=1, bounce=0.1 } )
ground.x, ground.y = ground.contentWidth / 4, sH - ground.contentHeight * 0.25
ground.alpha = 0
ground.name = "ground"
end
drawBackground()
--------------------------------------------------------
--------------------------------------------------------
drawBird = function()
bird = display.newImageRect( "a/img/hero/hero.png", 80, 58)
bird.x, bird.y = 0 - sW * 0.1, ground.y - ground.contentHeight - 10
bird.isFixedRotation = true; bird.angularVelocity = 0;
physics.addBody( bird, { density=1, friction=0.5, bounce=0.1 } )
bird.name = "bird"
sceneGroup:insert( bird )
bird:addEventListener( "collision", onCollision )
end
coinsCollected, tokensCollected = 0, 0
birdFlight = function()
if ( gameStatus == "running" ) then
transition.to(bird, {
y = bird.y - 75,
transition = easing.outQuad,
onComplete = function() end
})
function birdFlight()
--transition.to( bird, { rotation=-45, time=300 } )
end
birdFlight()
function birdFall()
--transition.to( bird, { rotation=90, time=300 } )
end
timer.performWithDelay(700, birdFall, 1)
end
end
display.currentStage:addEventListener( "tap", birdFlight )
function onCollision(event)
if event.phase == "began" then
if event.other.name == "coin" then
coinsCollected = coinsCollected + 1
end
if event.other.name == "token" then
tokensCollected = tokensCollected + 1
end
if event.other.name == "missile" or event.other.name == "laser" or event.other.name == "rod" then
-- game ended
end
end
end
drawBird()
function atFrame(event)
if gameStatus == "running" then
bird.x,bird.y = sW / 3,bird.y + 9
if ( bird.y > sH - 70) then bird.y = sH - 70 end
if ( gameStatus == "ended" ) then bird.y = bird.y + 15 end
end
end
Runtime:addEventListener( "enterFrame", atFrame )
--------------------------------------------------------
--------------------------------------------------------
missile, missileAlert, missileMoving = {}, {}, {}
function removeMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < -100 then
display.remove( missile[i] )
missile[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function flyMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and missile[i].y ~= nil and gameStatus == "running" then
if missileMoving[i] == true then
missile[i].y = bird.y
end
missile[i].x = missile[i].x - 5
if missileAlert[i] ~= nil then
if missileMoving[i] == true then
missileAlert[i].y = bird.y
end
if missile[i].x < sW then
display.remove( missileAlert[i] )
missileAlert[i] = nil
end
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function holdMissile(i)
local onEnterFrame = function( event )
if missile[i] ~= nil and missile[i].x ~= nil and gameStatus == "running" then
if missile[i].x < sW * 1.5 then
if missileAlert[i] ~= nil then
missileAlert[i]:setFillColor(1,1,0)
end
missileMoving[i] = false
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnMissile(i)
missile[i] = display.newRect( sW*2, sH/2, 80, 80 )
missileAlert[i] = display.newRect( sW-80, bird.y, 80, 80 )
physics.addBody(missile[i],"kinematic",{isSensor=true})
missile[i].name = "missile"
sceneGroup:insert( missile[i] )
sceneGroup:insert( missileAlert[i] )
missileMoving[i] = true
flyMissile(i)
removeMissile(i)
holdMissile(i)
end
spawnMissile(1)
--------------------------------------------------------
--------------------------------------------------------
laser = {}
function moveAndRemovelaser(i)
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].x ~= nil and gameStatus == "running" then
laser[i].x = laser[i].x - 5
if laser[i].x < 0 - sW / 2 then
display.remove( laser[i] )
laser[i] = nil
end
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
print(gameStatus)
if gameStatus == "ended" then
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function spawnlaser(i)
laserSize = math.random(1,2)
laserPosition = math.random(1,3)
laserRotation = math.random(1,4)
if laserSize == 1 then
laser[i] = display.newRect( 100,100,50,sH/3 )
else
laser[i] = display.newRect( 100,100,50,sH/2 )
end
sceneGroup:insert( laser[i] )
laser[i].x = sW * 2
laser[i].y = sH / 2
laser[i].name = "laser"
if laserPosition == 1 and laserRotation ~= 4 then
laser[i].y = sH * 0.05 + 12
laser[i].anchorY = 0
elseif laserPosition == 3 and laserRotation ~= 4 then
laser[i].y = sH * 0.95 - 12
laser[i].anchorY = 1
end
if laserPosition == 1 and laserRotation == 4 then
laser[i].y = sH * 0.05 + laser[i].contentHeight / 2
elseif laserPosition == 3 and laserRotation == 4 then
laser[i].y = sH * 0.95 - laser[i].contentHeight / 2
end
if laserRotation == 1 then
laser[i].rotation = -45
elseif laserRotation == 2 then
laser[i].rotation = 0
elseif laserRotation == 3 then
laser[i].rotation = 45
elseif laserRotation == 4 then
local onEnterFrame = function( event )
if laser[i] ~= nil and laser[i].rotation ~= nil then
laser[i].rotation = laser[i].rotation + 5
end
end
Runtime:addEventListener( "enterFrame", onEnterFrame )
end
laser[i]:setFillColor(1,1,0)
physics.addBody(laser[i],"kinematic",{isSensor=true})
moveAndRemovelaser(i)
end
spawnlaser(1)
--------------------------------------------------------
image = display.newRect (100,100,100,100)
image.x = display.contentCenterX
image.y = display.contentCenterY
sceneGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( "Game Screen", 0, 0, native.systemFontBold, 24 )
text1:setFillColor( 255 )
text1.x, text1.y = display.contentWidth * 0.5, 50
sceneGroup:insert( text1 )
end
function scene:show( event )
local phase = event.phase
if "will" == phase then
gameStatus = "ended"
end
if "did" == phase then
print( "4" )
composer.removeScene( "startScreen" )
image:addEventListener( "touch", image )
gameStatus = "running"
end
end
function scene:hide( event )
local phase = event.phase
if "will" == phase then
print( "5" )
gameStatus = "ended"
image:removeEventListener( "touch", image )
Runtime:removeEventListener( "enterFrame", onEnterFrame )
end
end
function scene:destroy( event )
print( "6" )
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene

You are getting this problem because you are registering an event listener twice.
You need to add and remove them only when the scene enters and exits. The proper place to remove game objects is scene:destroy() and scene:hide()
My advice is that you try to re-organize your code and try to follow these guidelines:
scene:create() : This happens the first time a scene is used. You want create your game objects here. This event will only happen once, unless the scene is destroyed and then used again.
scene:show() : You want to register all your event listeners here (touch, etc) and setup your game. This function gets called every-time the scene is shown. Every event listener that is registered here should be un-registered to prevent double-triggering.
scene:hide() : You want to un-register all event listeners here. This will prevent the next scene from triggering events on your previous scene's listeners too.
scene:destroy() : You want to remove all game objects here (images, text, widgets, etc). The scene is no longer needed and everything should be cleaned.
To better understand how the Scene's work I recommend reading this doc:
http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

So I ended up creating a function and putting all the removeEventListeners in that function and called that function when the game ended.
For future reference always keep a list of all the timer and event listeners that you have created anywhere in the screen and remove them all together when ending the game

Related

Attempt to call global (a nil value)

I'm trying to spawn asteroids in this game every few seconds. But whenever I run the game, I get an error that says
stack traceback:
main.lua:277: in function '_listener'
?: in function '?'
?: in function <?:172>
I've tried moving some of the code around but it's not helping. My professor looked at my code and doesn't know exactly what the issue is, but that the loop that I'm using is missing a something. Here is all the code that includes "createAsteroid"
EDIT: I added my entire main.lua file to this question. The separate files that I do have in this project are the character and the background image, everything else besides the config file is in here. I did try deleting an extra end In the gameLoop function but when I did, I got an error that basically said it expected an end there.
display.setStatusBar( display.HiddenStatusBar )
------------------------------
-- RENDER THE SAMPLE CODE UI
------------------------------
local sampleUI = require( "sampleUI.sampleUI" )
local physics = require( "physics" )
physics.start()
math.randomseed( os.time() )
------------------------------
-- CONFIGURE STAGE
------------------------------
local composer = require( "composer" )
local mainScene = display.newGroup()
display.getCurrentStage():insert( mainScene )
----------------------
-- BEGIN SAMPLE CODE
----------------------
-- Frequently used variables
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local originX = display.screenOriginX
local originY = display.screenOriginY
local width = display.actualContentWidth
local height = display.actualContentHeight
local score = 0
local died = false
local asteroidsTable = {}
local player
local gameLoopTimer
local scoreText
local uiGroup = display.newGroup() -- Display group for UI objects like the score
-- Load background and character from "background.lua" and "character.lua" respectively
local background = require( "background" )
mainScene:insert( background )
local character = require( "character" )
mainScene:insert( character )
local asteroid = display.newImage( "asteroid.png", 180, -50 )
asteroid.rotation = 5
physics.addBody( asteroid, { density=3.0, friction=0.5, bounce=0.3, radius=25 })
-------------------------
-- BEGIN MOVEMENT LOGIC
-------------------------
-- Movement logic variables
local movementSpeed = 1.5
local moving = false
local moveDirection = "down"
-- Sets if the character should move and updates sprite animation
local function setMoving( state )
moving = state
if ( moving ) then
character:play()
else
character:pause()
character:setFrame(2)
end
end
-- Sets the direction that the player should move and updates the character's sprite facing
local function setMovementDirection( direction )
-- Don't change anything if we haven't altered direction
if ( moveDirection == direction ) then
return
end
-- Update the sprite playback
moveDirection = direction
character:setSequence( "walk-" .. direction )
-- Refresh animation playback, which can pause after changing the sprite sequence
setMoving( moving )
end
-- Set movement magnitudes
local movementX = 0
local movementY = 0
local function setMovement( x, y )
local updatedMovement = false
-- Horizontal movement checks
if ( movementX ~= x and nil ~= x ) then
movementX = x
updatedMovement = true
end
-- Abort if nothing is updating
-- We do this since axis/key events can fire multiple times with the same values
if ( not updatedMovement ) then
return
end
-- Determine movement direction
if ( 0 ~= movementX or 0 ~= movementY ) then
-- Favor horizontal animations over vertical ones
if ( math.abs( movementX ) >= math.abs( movementY ) ) then
if ( 0 < movementX ) then
setMovementDirection( "right" )
else
setMovementDirection( "left" )
end
else
if ( 0 < movementY ) then
setMovementDirection( "down" )
else
setMovementDirection( "up" )
end
end
end
-- Update moving animation/variable
if ( 0 == movementX and 0 == movementY ) then
setMoving( false )
else
setMoving( true )
end
end
-- Handle character translation on the screen per frame
local function onFrameEnter()
if ( 0 ~= movementX ) then
character.x = character.x + ( movementSpeed * movementX )
setMoving( true )
end
if ( 0 ~= movementY ) then
character.y = character.y + ( movementSpeed * movementY )
setMoving( true )
end
end
Runtime:addEventListener( "enterFrame", onFrameEnter )
---------------------------
-- BEGIN INPUT CODE: TOUCH
---------------------------
local padGraphic, padButtonUp, padButtonDown, padButtonLeft, padButtonRight
-- Determine if we have a joystick connected or not
local inputDevices = system.getInputDevices()
local function getHasJoystick()
for i = 1, #inputDevices do
if ( "joystick" == inputDevices[i].type ) then
return true
end
end
return false
end
local hasJoystick = getHasJoystick()
-- If we don't have any controllers found, create a virtual D-pad controller
if ( not hasJoystick ) then
-- Called when one of the virtual D-pad buttons are used
local function onTouchEvent( event )
local phase = event.phase
local targetID = event.target.id
if ( "began" == phase or "moved" == phase ) then
if ( "up" == targetID ) then
setMovement( 0, -1 )
elseif ( "down" == targetID ) then
setMovement( 0, 1 )
elseif ( "left" == targetID ) then
setMovement( -1, 0 )
elseif ( "right" == targetID ) then
setMovement( 1, 0 )
elseif ( "padGraphic" == targetID ) then
setMovement( 0, 0 )
end
elseif ( "ended" == phase or "cancelled" == phase ) then
-- An alternative to checking for "cancelled" is to set focus on the control
-- However, we don't want an incoming phone call to bug out input
if ( "up" == targetID or "down" == targetID ) then
setMovement( nil, 0 )
elseif ( "left" == targetID or "right" == targetID ) then
setMovement( 0, nil )
end
end
return true
end
-- Display score
scoreText = display.newText( uiGroup, "Score: " .. score, 400, 40, native.systemFont, 36 )
-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )
local function updateText()
scoreText.text = "Score: " .. score
end
local function createAsteroid()
local newAsteroid = display.newImageRect( mainGroup, objectSheet, 1, 102, 85 )
table.insert( asteroidsTable, newAsteroid )
physics.addBody( newAsteroid, "dynamic", { radius=25, bounce=0.8 } )
newAsteroid.myName = "asteroid"
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newAsteroid.x = -60
newAsteroid.y = math.random( 500 )
newAsteroid:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) )
elseif ( whereFrom == 2 ) then
-- From the top
newAsteroid.x = math.random( display.contentWidth )
newAsteroid.y = -60
newAsteroid:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) )
elseif ( whereFrom == 3 ) then
-- From the right
newAsteroid.x = display.contentWidth + 60
newAsteroid.y = math.random( 500 )
newAsteroid:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) )
end
newAsteroid:applyTorque( math.random( -6,6 ) )
end
-- Create the visuals for the on-screen D-pad
local padSize = 200
padGraphic = display.newImageRect( mainScene, "pad.png", padSize, padSize )
padGraphic.x = originX + padSize/2 - 40
padGraphic.y = height + originY - padSize/2 + 40
padGraphic.alpha = 0.35
padGraphic.id = "padGraphic"
padGraphic:addEventListener( "touch", onTouchEvent )
-- Creates one of the invisible virtual D-pad buttons
local function createPadButton( buttonID, offsetX, offsetY )
local btn = display.newRect( mainScene, padGraphic.x+offsetX, padGraphic.y+offsetY, padSize/5, padSize/5 )
btn:addEventListener( "touch", onTouchEvent )
btn.id = buttonID
btn.isVisible = false
btn.isHitTestable = true
return btn
end
-- Create buttons for handling the D-pad input
padButtonUp = createPadButton( "up", 0, padSize/-5 )
padButtonDown = createPadButton( "down", 0, padSize/5 )
padButtonLeft = createPadButton( "left", padSize/-5, 0 )
padButtonRight = createPadButton( "right", padSize/5, 0 )
end
-----------------
-- GAME LOOP --
-----------------
local function gameLoop()
-- Create new asteroid
createAsteroid()
-- Remove asteroids which have drifted off screen
for i = #asteroidsTable, 1, -1 do
local thisAsteroid = asteroidsTable[i]
if ( thisAsteroid.x < -100 or
thisAsteroid.x > display.contentWidth + 100 or
thisAsteroid.y < -100 or
thisAsteroid.y > display.contentHeight + 100 )
then
display.remove( thisAsteroid )
table.remove( asteroidsTable, i )
end
end
end
timer.performWithDelay(1000, gameLoop, 0)
--------------------------------------------------
-- BEGIN INPUT CODE: KEYBOARD & BASIC CONTROLLER
--------------------------------------------------
-- Detect if a joystick axis is being used
local joystickInUse = false
-- Keyboard input configuration
local keyUp = "up"
local keyDown = "down"
local keyLeft = "left"
local keyRight = "right"
-- Called when a key event has been received
local function onKeyEvent( event )
local keyName = event.keyName
local phase = event.phase
-- Handle movement keys events; update movement logic variables
if ( not joystickInUse ) then
if ( "down" == phase ) then
if ( keyUp == keyName ) then
setMovement( nil, -1 )
elseif ( keyDown == keyName ) then
setMovement( nil, 1 )
elseif ( keyLeft == keyName ) then
setMovement( -1, nil )
elseif ( keyRight == keyName ) then
setMovement( 1, nil )
end
elseif ( "up" == phase ) then
if ( keyUp == keyName ) then
setMovement( nil, 0 )
elseif ( keyDown == keyName ) then
setMovement( nil, 0 )
elseif ( keyLeft == keyName ) then
setMovement( 0, nil )
elseif ( keyRight == keyName ) then
setMovement( 0, nil )
end
end
end
return false
end
Runtime:addEventListener( "key", onKeyEvent )
------------------------------------------
-- BEGIN INPUT CODE: ADVANCED CONTROLLER
------------------------------------------
-- We only support advanced controllers when one is detected
if ( getHasJoystick ) then
-- Detect axis event updates
local function onAxisEvent( event )
local value = event.normalizedValue
local axis = event.axis.type
local descriptor = event.axis.descriptor
-- We only care about "x" and "y" input events
-- However, touch-screen events can fire these as well so we filter them out
if ( ( "x" ~= axis and "y" ~= axis ) or ( string.find( descriptor, "Joystick" ) == nil ) ) then
return
end
-- Detect zero movement at a certain cutoff so we don't get the character moving very, very slowly
if ( math.abs(value) < 0.15 ) then
value = 0
end
-- Based on which axis type we are dealing with, set movement variables
if ( "x" == axis ) then
setMovement( value, nil )
elseif ( "y" == axis ) then
setMovement( nil, value )
end
-- Some devices will send both up/down/left/right keys and the axis value
-- We let our code know that we are using a joystick value so they do not conflict
if ( 0 ~= value ) then
joystickInUse = true
else
joystickInUse = false
end
end
Runtime:addEventListener( "axis", onAxisEvent )
end
local function onCollision( event )
if ( ( obj1.myName == "player" and obj2.myName == "asteroid" ) or
( obj1.myName == "asteroid" and obj2.myName == "player" ) )
then
display.remove( player )
else
ship.alpha = 0
timer.performWithDelay( 1000, restoreShip )
end
end
Runtime:addEventListener( "collision", onCollision )
Go to line 277 in your file main.lua
You will find a function call createAsteroid(). Within this scope createAsteroid is not defined.
So let's see if we can find a definition for createAsteroid in this file.
Line 218: local function createAsteroid() ...
Check if the function call is within the scope of that function...
No! It is inside an if-statement while the function call is outside that statement. As createAsteroid is local to that if-statement it is unknown (nil) inside gameLoop and hence may not be called.
Finding such scope issues could be very easy if you had proper indentation!

Attempt to perform arithmetic on field 'y' (a nil value)

I'm new here and I'm struggling with this error in Corona, when game ends (lives=0) and I try to remove the background (that is moving with a function "move"):
"Attempt to perform arithmetic on field 'y' (a nil value)"
in line "background.y = background.y + 4"
Is there anybody who can explain me what is the mistake?
THE CODE:
--add PHYSICS
local physics = require( "physics" )
physics.start()
physics.setGravity( 0, 0 )
local lives = 1
local died = false
--###
--add background
background = display.newImageRect( "background.png", 800, 14000 )
background.x = display.contentCenterX
background.y = 730
background.myName = "background"
--add bottle
bottiglia = display.newImageRect( "bottiglia.png", 41, 104 )
physics.addBody( bottiglia, "dynamic", { radius=45, bounce=0.5 } )
bottiglia.x = display.contentCenterX
bottiglia.y = 10
bottiglia.myName = "bottiglia"
--function move
local function move()
bottiglia.y = bottiglia.y + 4
background.y = background.y + 4
end
Runtime:addEventListener( "enterFrame", move )
--###
--add player
studente = display.newImageRect( "studente.png", 98, 79 )
studente.x = display.contentCenterX
studente.y = display.contentHeight - 100
physics.addBody( studente, { radius=40, isSensor=true } )
studente.myName = "studente"
--###
--function collision
local function onCollision( event )
if ( event.phase == "began" ) then
local obj1 = event.object1
local obj2 = event.object2
if ( ( obj1.myName == "studente" and obj2.myName == "bottiglia" ) or
( obj1.myName == "bottiglia" and obj2.myName == "studente" ) )
then
if ( died == false ) then
died = true
-- lives update
lives = lives - 1
livesText.text = "Lives: " .. lives
if ( lives == 0 ) then
display.remove( studente )
display.remove( background)
timer.performWithDelay( 100, endGame )
end
else
studente.alpha = 0
timer.performWithDelay( 500, restoreStudente )
end
end
end
end
Runtime:addEventListener( "collision", onCollision )
livesText = display.newText( "Lives: " .. lives, 200, 80, native.systemFont, 36 )
--thank you all
The Runtime listener (move function) is working all the time. It changes position of bottiglia and background objects but since background does not exist any more you get an error.
A simple solution is to remove the global listener using Runtime:removeEventListener() before you remove the background object.
Use Runtime:removeEventListener("enterFrame", move)
If you don't want remove listener, you can add checks for nil:
--function move
local function move()
if (bottiglia ~= nil and bottiglia.y ~= nil) then
bottiglia.y = bottiglia.y + 4
end
if (background~= nil and background.y ~= nil) then
background.y = background.y + 4
end
end
This is also pretty risky to use such global variables: bottiglia and background.
You can make it little safer if make them as (or something like that):
myGlobalsVars = { }
myGlobalsVars.myGlobalsVars = display.newGroup()
myGlobalsVars.background = display.newGroup()

Failed to back to Menu from Game Over . Score unable to display on game over screen

I'm very new to corona. I learned all from the web and this is what i produced. The game seems fine but when game over screen is displayed, the button i put to back to menu scene doesn't work and the score that player get failed to be displayed to..
Here is my code...someone kindly please help me out.. what code should i change or add to it??
Thank you. Any help is much appreciated.
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require("physics")
local widget = require "widget"
physics.start()
rand = math.random( 20 )
local slap_sound = audio.loadSound("Sound/slap2.mp3")
local ow = audio.loadSound("Sound/ow.mp3")
local buttonSound = audio.loadSound("Sound/sound2.mp3")
local background
local back
local count={total1=0,total=0,touch=0,life=3}
local total
local time_remain = 5
local mossie
local bee
local shade
local gameOverScreen
local winScreen
local countdown
local life
local pauseBtn
local resumeBtn
local gametmr
---------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE
-- unless "composer.removeScene()" is called.
---------------------------------------------------------------------------------
local gameOver = function()
composer.removeScene("easy")
physics.pause()
--audio.play(gameOverSound)
background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0
gameOverScreen = display.newImage("Images/gameover.png",400,300)
gameOverScreen.x = 160
gameOverScreen.y = 240
gameOverScreen.alpha = 0
transition.to(gameOverScreen,{time=500,alpha=1})
--total.isVisible = true
total.text="Score : "..count.touch
total.x = 160
total.y = 400
--total:setTextColor(000000)
botwall.isVisible = false
mossie.isVisible = false
bee.isVisible = false
life.isVisible = false
countdown.isVisible = false
pauseBtn.isVisible = false
resumeBtn.isVisible = false
local myButton = widget.newButton
{
left = 100,
top = 100,
id = "myButton",
label = "Menu",
onEvent = handleButtonEvent
}
myButton.isVisible = true
end
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
composer.gotoScene ("menu")
end
end
local function countDown(e)
time_remain = time_remain-1
countdown.text = time_remain
if time_remain == 0 then
gameOver()
end
end
local pauseGame = function(e)
if(e.phase=="ended") then
audio.play(buttonSound)
physics.pause()
timer.pause(gametmr)
pauseBtn.isVisible = false
resumeBtn.isVisible = true
return true
end
end
local resumeGame = function(e)
if(e.phase=="ended") then
audio.play(buttonSound)
physics.start()
timer.resume(gametmr)
pauseBtn.isVisible = true
resumeBtn.isVisible = false
return true
end
end
local collisionListener=function(self,event)
if(event.phase=="began")then
if(event.other.type=="mossie")then
audio.play(ow)
count.life=count.life-1
if(count.life==0) then
gameOver()
end
event.other:removeSelf()
event.other=nil
else
event.other:removeSelf()
event.other=nil
end
end
end
function onTouch(mossie)
audio.play(slap_sound)
count.touch=count.touch+1
total.text="Score : "..count.touch
mossie.target:removeSelf()
end
function killIt(e)
if(e.phase == "ended") then
gameOver()
end
end
local bottomWall = function()
botwall=display.newImage("Images/tangan.png")
botwall.x = 160
botwall.y = 500
botwall:setFillColor(22,125,185,255)
botwall.type="botwall"
botwall.collision=collisionListener
physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
botwall:addEventListener("collision",botwall)
end
local function newMossie(event)
total.text="Score : "..count.touch
life.text="Life : "..count.life
mossie = display.newImage("Images/biasa.png")
mossie.x = 60 + math.random( 160 )
mossie.y = -100
mossie.type="mossie"
mossie:setFillColor(255,0,0)
physics.addBody( mossie, { density=0.3, friction=0.2, bounce=0.5} )
mossie.name = "mossie"
mossie:addEventListener("touch",onTouch)
end
local function newBee(event)
bee = display.newImage("Images/lebah.png")
bee.x = 60 + math.random( 160 )
bee.y = -100
bee.type="other"
physics.addBody( bee, { density=1.4, friction=0.3, bounce=0.2} )
bee:addEventListener("touch",killIt)
end
-- local forward references should go here
---------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
background = display.newImageRect( "Images/bg.jpg", display.contentWidth, display.contentHeight )
background.anchorX = 0
background.anchorY = 0
background.x, background.y = 0, 0
total=display.newText("Score : 0",display.contentWidth * 0.5, 20, "Arial", 26)
total:setTextColor(000000)
countdown=display.newText(time_remain ,display.contentWidth * 0.9, 20, "Arial", 26)
countdown:setTextColor(000000)
life = display.newText("Life : 3 " ,display.contentWidth * 0.5, 50, "Arial", 26)
life:setTextColor(000000)
pauseBtn = display.newImage("Images/pause.png")
pauseBtn.x = display.contentWidth * 0.1
pauseBtn.y = display.contentHeight - 450
resumeBtn = display.newImage("Images/playb.png")
resumeBtn.x = display.contentWidth * 0.1
resumeBtn.y = display.contentHeight - 450
botwall=display.newImage("Images/tangan.png")
botwall.x = 160
botwall.y = 500
botwall:setFillColor(22,125,185,255)
botwall.type="botwall"
botwall.collision=collisionListener
physics.addBody(botwall,"static",{ density=100.0, friction=0.0, bounce=0.0} )
sceneGroup:insert(background)
sceneGroup:insert(total)
sceneGroup:insert(countdown)
sceneGroup:insert(life)
sceneGroup:insert(pauseBtn)
sceneGroup:insert(resumeBtn)
sceneGroup:insert(botwall)
resumeBtn.isVisible = false
pauseBtn:addEventListener("touch", pauseGame)
resumeBtn:addEventListener("touch", resumeGame)
botwall:addEventListener("collision",botwall)
dropMossie = timer.performWithDelay( 2000 , newMossie, -1 )
dropBee = timer.performWithDelay( 1800 , newBee, -1)
gametmr = timer.performWithDelay(1000, countDown, -1)
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
--physics.stop()
--timer.cancel(gametmr)
--pauseBtn:removeEventListener("touch", pauseGame)
--resumeBtn:removeEventListener("touch", resumeGame)
--botwall:removeEventListener("collision",botwall)
--bee:removeEventListener("touch",killIt)
--mossie:removeEventListener("touch",onTouch)
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
---------------------------------------------------------------------------------
return scene
I suggest to make another composer scene for gameOverScreen (just like you did with these code block you post). Then do a composer.gotoScene(gameOver) read more here. And for the score I suggest that you make something like this.
The funtion handleButtonEvent is declared after you are creating a button widget. You can get the desired result by moving the function about the widget code.
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
composer.gotoScene ("menu")
end
end
local myButton = widget.newButton
{
left = 100,
top = 100,
id = "myButton",
label = "Menu",
onEvent = handleButtonEvent
}
myButton.isVisible = true
end

Making a character running when holding a button while 2 seconds

I've a character moving right and left correctly. I want now the hero to run while the button is pressed during 2 seconds.
This is my actual code:
function LeftArrow(event)
motionx = -hero.speed
end
function RightArrow(event)
motionx = hero.speed
end
local function MoveHero (event)
hero.x = hero.x + motionx
end
Runtime:addEventListener("enterFrame", MoveHero)
-- Stop character movement when no arrow is pushed
local function StopHero (event)
if event.phase =="ended" then
motionx = 0
end
end
Runtime:addEventListener("touch", StopHero )
Any suggestion?
Okay I think i found the answer !
function GoFastLeft()
hero.speed=10
motionx = -hero.speed
end
function GoFastRight()
hero.speed=10
motionx = hero.speed
end
function LeftArrow(event)
if ( event.phase == "began" ) then
motionx = -hero.speed
FastTimer = timer.performWithDelay( 1000, GoFastLeft, 1 )
elseif ( event.phase == "ended" ) then
hero.speed=2
timer.cancel( FastTimer )
motionx = 0
end
return true
end
function RightArrow(event)
if ( event.phase == "began" ) then
motionx = hero.speed
FastTimer2 = timer.performWithDelay( 1000, GoFastRight, 1 )
elseif ( event.phase == "ended" ) then
hero.speed=2
timer.cancel( FastTimer2 )
motionx = 0
end
return true
end

Saving values to another document in Lua with Corona SDK

I'm trying to save highscores to a table whenever a gameOver function is called.
When I go back in the app and try to read the highscores(They're displayed as newText where text is set to the proper highscore of the level.).
But it's not doing what it's supposed to. I can read the the levels highscores but when I try to change the values in the table, the highscore text doesn't change.
I have my main.lua and a myData.lua - The highscore table should be placed in the myData.lua in order to reach it from all the levels in the game.
this is my table
highScores = {
1,
2,
3,
4,
5,
6,
7
}
and I was trying to save/change the value with
highScores[1] = score
Where score is the score count in-game.
I've realized this is not the way to go about it, and what I can find on google seems to overly complicated for what I see as a simple task.
What am I doing wrong?
This is my entire level1.lua - The actual level that's running and trying to save it's score to the highScore table level1.lua):
local composer = require( "composer" )
local scene = composer.newScene()
local myData = require( "myData" )
local physics = require("physics")
physics.setDrawMode( "hybrid" )
-- forward references
local w = display.actualContentWidth
local h = display.actualContentHeight
local dropCount = 0
local spawnShit = 0
local allowDrop = 1
local spawnTime = 17
local countdownTimer
local score
local gX = 0
local gY = 0
score = 0
local countDownNumber = 10
local gameOver
local scoreT = display.newText( {text="Score: "..score, font=system.nativeSystemFont, fontSize=14,} )
scoreT.x = w * 0.5
scoreT.y = h * 0.1
local countDownText = display.newText( {text="", font=system.nativeSystemFont, fontSize=14} )
countDownText.x = w * 0.5
countDownText.y = h * 0.2
local drop01 = display.newImage("drop01.png")
drop01.x = -100
local drop02 = display.newImage("drop02.png")
drop02.x = -100
local drop03 = display.newImage("drop03.png")
drop03.x = -100
local drop04 = display.newImage("drop04.png")
drop04.x = -100
local timerSpawn
local timer2
-- Display objects
local background = display.newImage( "bluebg.png" )
background.x = w*0.5
background.y = h*0.5
background.width = w
background.height = h
local bckBtn = display.newText({text="<--BACK", font=system.nativeSystemFont, fontSize=14})
bckBtn.x = 50
bckBtn.y = 20
local egon = display.newImage( "Egon.png" )
egon.x = w*0.5
egon.y = h*0.85
egon.width = 100
egon.height = 97
local destroyAll = display.newRect( 0, h, w, 10 )
destroyAll.width = w*2
destroyAll.alpha = 0
local overlayBg = display.newRect( -500, -500, w, h )
overlayBg:setFillColor( 0, 0, 0 )
overlayBg.alpha = 0.4
--functions
function gameOver ()
if timerSpawn == nil then
else
timer.cancel(timerSpawn)
timerSpawn = nil
spawnShit = 0
end
if countdownTimer == nil then
else
timer.cancel(countdownTimer)
countdownTimer = nil
end
highScores[1] = score
transition.to( overlayBg, {x=w/2, y=h/2, time=500 } )
end
function goBack (event)
if "began" == event.phase then
gameOver()
if timerSpawn == nil then
else
timer.cancel(timerSpawn)
end
if countdownTimer == nil then
else
timer.cancel(countdownTimer)
end
elseif event.phase == "ended" then
timer2 = timer.performWithDelay(1000, function()
composer.gotoScene("select", "fade", 500)
end)
if overlayBg == nil then
else
overlayBg:removeSelf( )
end
return true
end
return true
end
function moveEgon (event)
if "moved" == event.phase then
egon.x = event.x
end
end
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function spawnObjects (event)
dropCount = math.random(1,4)
--if stopTimer == 1 then
-- timerSpawn = nil
--spawnShit = nil
--end
if spawnShit == 1 then
print( 'spawnShit' )
if dropCount == 1 then
-- Drop01 function and settings
drop01 = display.newImage( "drop01.png" )
drop01.x = math.random(10, 470)
drop01.y = -40
drop01.width = 50
drop01.height = 50
drop01.myName = "01"
physics.addBody( drop01, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 2 then
--Do shit for drop02
drop02 = display.newImage( "drop02.png" )
drop02.x = math.random(10, 470)
drop02.y = -40
drop02.width = 50
drop02.height = 50
drop02.myName = "02"
physics.addBody( drop02, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 3 then
drop03 = display.newImage( "drop03.png" )
drop03.x = math.random(10, 470)
drop03.y = -40
drop03.width = 50
drop03.height = 50
drop03.myName = "03"
physics.addBody( drop03, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
elseif dropCount == 4 then
drop04 = display.newImage( "drop04.png" )
drop04.x = math.random(10, 470)
drop04.y = -40
drop04.width = 50
drop04.height = 50
drop04.myName = "04"
physics.addBody( drop04, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
end
end
return true
end
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
function onCollision (event)
if "began" == event.phase then
--v--do shit when touching surface
if event.other.myName == "01" then
-- Do shit for drop01 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score+1
countDownNumber = countDownNumber + 10
scoreT.text = "Score: "..score
end
if event.other.myName == "02" then
-- Do shit for drop02 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score+1
scoreT.text = "Score: "..score
end
if event.other.myName == "03" then
-- Do shit for drop03 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score-1
scoreT.text = "Score: "..score
end
if event.other.myName == "04" then
-- Do shit for drop04 --
-- Change score, powersups etc
event.other:removeSelf( )
score = score-1
scoreT.text = "Score: "..score
end
elseif "ended" == event.phase then
-- Do shit when leaving surfaces
end
return true
end
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function showCountDown (event)
-- Condition to show and hide countdown
if countDownNumber <= 1 or score == -1 then
spawnShit = 0
countDownNumber = 0
timer.cancel(timerSpawn)
timer.cancel(countdownTimer)
countdownTimer = nil
highScores[1] = score
print( 'NO MORE SPAAAAAAAAAAAAAAAWWNS' )
end
if countDownNumber >= 1 then
countDownNumber = countDownNumber -1
countDownText.text = countDownNumber
spawnShit = 1
end
return true
end
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
function destroy (event)
if "began" == event.phase then
event.other:removeSelf( )
else if "ended" == event.phase then
end
return true
end
end
--function scene:create( event )
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc
--Listeners
background:addEventListener( "touch", moveEgon )
bckBtn:addEventListener( "touch", goBack )
egon:addEventListener( "collision", onCollision )
destroyAll:addEventListener( "collision", destroy )
--SceneGroup insert
sceneGroup:insert( background )
sceneGroup:insert(egon)
sceneGroup:insert(bckBtn)
sceneGroup:insert(drop01)
sceneGroup:insert(drop02)
sceneGroup:insert(drop03)
sceneGroup:insert(drop04)
sceneGroup:insert(scoreT)
sceneGroup:insert(countDownText)
sceneGroup:insert(overlayBg)
sceneGroup:insert(destroyAll)
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
physics.start( )
gX = 0
gY = 10
physics.setGravity( gX, gY )
timercount = 10
spawnShit = 1
score = 0
scoreT.text = "Score: "..score
-- ADD physic bodies ----
physics.addBody( egon, "static", {density=0.1, friction=0.1, bounce=0.8 } )
physics.addBody( destroyAll, "static", {density=0.1, friction=0.1, bounce=0.1 } )
countDownNumber = 10
if countdownTimer == nil then
countdownTimer = timer.performWithDelay( 1000, showCountDown, 0 )
else
end
----------- Timers ------------
if timerSpawn == nil then
timerSpawn = timer.performWithDelay(500, spawnObjects, 0 )
else
end
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio,
--timer.pause( timerSpawn )
physics.stop()
spawnShit = nil
score = nil
timerSpawn = nil
countdownTimer = nil
overlayBg = nil
--timer.cancel(timerSpawn)
physics.removeBody( egon )
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
bckBtn:removeEventListener("touch", goBack )
egon:removeEventListener("touch", moveEgon )
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
Allright. The scores are being saved. The problem was the way i displayed the score text, because it wasn't reloading, it was just re-using the old value.
FIX:
i added an
score01.text = highScore[1]
everytime the scene shows and it automatically updates the score when the select screen shows.

Resources