Removing specific objects on Collision - lua

removing the coin im colliding whit seems a bit of a problem im got this :
local screenGroup = self.view
local options2 =
{
--required parameters
width = 16,
height = 16,
numFrames = 8,
--optional parameters; used for dynamic resolution support
sheetContentWidth = 128, -- width of original 1x size of entire sheet
sheetContentHeight = 16, -- height of original 1x size of entire sheet
}
local imageSheet1 = graphics.newImageSheet( "items/coin.png", options2 )
-- Example assumes 'imageSheet' is already created using graphics.newImageSheet()
-- non-consecutive frames
local sequenceData1 =
{
name="normal",
frames= {1,2,3,4,5,6,7,8}, -- frame indexes of animation, in image sheet
time = 600, --700 -- Optional, in milliseconds ; if not supplied, the animation is frame-based
loopCount = 0 -- Optional ; default is 0
}
local coin = {}
local coinspawn = function()
local i = display.newSprite( imageSheet1, sequenceData1 )
i.x = display.contentWidth/2
i.y = display.contentHeight/2
i:play()
i.collided = true
i.name = "coin"
physics.addBody(i, "dynamic",
{density=.1, bounce=0.1, friction=.2, shape= shape2 ,filter=playerCollisionFilter }
)
--player.gravityScale = 0.5
coinIntro = transition.to(i,{time=2000, x=display.contentWidth/2-50 ,onComplete=jetReady , transition=easing.OutExpo } ) --
coin[#coin+1] = i
end
timer.performWithDelay( 1000, coinspawn, 0 )
function coinPlus()
for i = #coin, 1, -1 do
if coin[i] ~= nil then
local function dellcoin()
if coin[i] ~= nil then
coin[i]:removeSelf()
coin[i] = nil
end
end
transition.to( coin[i], { time=100, alpha=0, onComplete = dellcoin} )
break
end
end
end
local function onCollision(event)
if event.phase == "began" and gameIsActive == true then
local obj1 = event.object1;
local obj2 = event.object2;
if obj1.name == "playerpop" then
if obj2.name == "BGfrontFL1" then --helper()
elseif obj2.name == "BGfrontFL2" then --helper()
elseif obj2.name == "coin" then coinPlus()
end
end
end
end
Runtime:addEventListener( "collision", onCollision )
withs kinda works but it removes the last spawned coin and not the one in collision, how can i fix this ?

In coinspawn you create the coin and add it to coin table. It appears that your coin table will contain all spawned coins that have not been collided with (that seems to be your intention anyways). Then when a coin collides the onCollision() will get called, which will call coinPlus(). The latter then loops over all coins in coin table, starting with the latest spawned one (at end of table), and if it is not nil it starts a fade out with removal on completion of fade-out. This is surely not what you intend: you want to delete only the coin collided.
So the biggest problem is the way the coin that was involved in collision gets removed: looping over all coins, don't think it is necessary. You should try passing coin as arg to coinPlus:
if obj1.name == "playerpop" then
if obj2.name == "BGfrontFL1" then --helper()
elseif obj2.name == "BGfrontFL2" then --helper()
elseif obj2.name == "coin" then coinPlus(obj2)
end
end
function coinPlus(coinToRemove)
local function dellcoin()
coinToRemove:removeSelf()
for i, coin in ipairs(coin) do
if coin == coinToRemove then
coin[i] = nil
break
end
end
end
transition.to( coinToRemove, { time=100, alpha=0, onComplete = dellcoin} )
end
Another problem is your use of '#' operator: it is only to be used with tables that don't have "holes", so if you really do remove individual entries from your table (thus creating holes), then #coin will no longer work (that's why I used ipairs).

Related

Display Object in Display Group is not displayed properly when created in Corona SDK

In my project; when the white circle is hold on, the number of zombies is decreased by one. When the number of zombies reach zero, the wave number is increased by one. and the number of zombies are then equal to the number of waves. While that happens, a rectangle at the side is created after the wave value is increased by one.
However, my problem is that the rectangles are constantly being made in the same place where the second rectangle was created and that rectangles are being creating when the wave hasn't changed.
If you still don't understand, just paste the code in and you will.
Here is my code:
local wave = 1
local zombies = wave
local rectGroup = display.newGroup()
local rect = display.newRect(rectGroup,display.contentCenterX+90,display.contentCenterY,50,400)
local function moveup()
zombies=zombies-1
rectGroup.y=rectGroup.y+5
end
local needToup = false
local function handleEnterFrame( event )
if ( needToup == true ) then
moveup()
end
end
Runtime:addEventListener( "enterFrame", handleEnterFrame )
local function handleupButton( event )
if ( event.phase == "began" ) then
-- Fire the weapon
needToup = true
elseif ( event.phase == "ended" and needToup == true ) then
-- Stop firing the weapon
needToup = false
end
return true
end
local circleup = display.newCircle(display.contentCenterX,display.contentCenterY,40)
circleup:addEventListener("touch",handleupButton)
local waveText = display.newText(wave, display.contentCenterX, 100, native.systemFontBold, 25)
local waveTitle = display.newText("Wave:", waveText.x, waveText.y-20, native.systemFontBold, 25)
local zombiesText = display.newText(zombies, display.contentCenterX, 175, native.systemFontBold, 25)
local zombiesTitle = display.newText("Zombies:", zombiesText.x, zombiesText.y-20, native.systemFontBold, 20)
function zombiesText.enterFrame( self )
self.text = tostring(zombies)
end
Runtime:addEventListener("enterFrame", zombiesText )
function zombiesText.finalize( self )
Runtime:removeEventListener( "enterFrame", self )
end
zombiesText:addEventListener("finalize")
function waveText.enterFrame( self )
self.text = tostring(wave)
end
Runtime:addEventListener("enterFrame", waveText )
function waveText.finalize( self )
Runtime:removeEventListener( "enterFrame", self )
end
waveText:addEventListener("finalize")
local function gameLoop()
if zombies==0 then
wave=wave+1
zombies=wave
end
if wave>1 then
print("On wave"..tostring(wave))
display.newRect(rectGroup,display.contentCenterX+90,-rectGroup[rectGroup.numChildren].y+70,50,400)
Runtime:removeEventListener("enterFrame", gameLoop)
Runtime:addEventListener("enterFrame", gameLoop)
end
end
Runtime:addEventListener("enterFrame", gameLoop)

Listen Event for Any Object in a Table

Another newbie query. Now on my third day of working with Corona.
The following code works fine: Balloons are generated and float into the air. Now I want to use :addEventListener( "tap", pushBalloon ) to make it so that when a balloon is clicked the pushBalloon is executed. Can anyone tell me what variable I would use and how I would define it? And also I guess I would have to change the pushBalloon function too for the new variable.
Thank you.
local function createBalloon()
local randomBalloon = math.random( 10 )
local newBalloon = display.newImageRect( objectSheet, randomBalloon, 112, 142 )
table.insert( balloonsTable, newBalloon )
physics.addBody( newBalloon, "dynamic", { radius=70, bounce=0 } )
newBalloon.myName = "bigBalloon"
newBalloon.alpha = 0.75
newBalloon.gravityScale = randomBalloon/-150
local whereFrom = math.random( 3 )
if ( whereFrom == 1 ) then
-- From the left
newBalloon.x = 100
newBalloon.y = display.contentHeight+150
elseif ( whereFrom == 2 ) then
-- From the top
newBalloon.x = 160
newBalloon.y = display.contentHeight+150
elseif ( whereFrom == 3 ) then
-- From the right
newBalloon.x = 220
newBalloon.y = display.contentHeight+150
end
end
local function gameLoop()
-- Create new balloon
createBalloon()
-- Remove balloons which have drifted off screen
for i = #balloonsTable, 1, -1 do
local thisBalloon = balloonsTable[i]
if ( thisBalloon.x < -100 or
thisBalloon.x > display.contentWidth + 100 or
thisBalloon.y < -100 )
then
display.remove( thisBalloon )
table.remove( balloonsTable, i )
end
end
end
local function pushBalloon()
-- balloon:applyLinearImpulse( 0.2, -2, balloon.x, balloon.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
newBalloon.gravityScale = 10
end
You are adding the newBalloon objects to a table, but you should add the event listener to each newBalloon DisplayObject as it is instantiated. This doesn't do exactly what you ask in the title (where simply inserting an object into a table would effectively add an event listener to that object), but achieves the event response I think you are looking for.
If you are tapping the balloon, you would put the listener on the balloon. If you use a "tap" event, the target property tells you which object was touched, so your pushBalloon() function works for any balloon.
local pushBalloon( event )
local balloon = event.target
if event.phase == "began"
-- do something to the balloon object (apply impulse, etc.)
end
end
local function createBalloon()
...
local newBalloon = display.newImageRect( ... )
if newBalloon then
-- set properties of DisplayObject and add event listener
newBallon:addEventListener( "tap", pushBalloon )
end
...
end
I have wrapped the call to addEventListener() in a check to make sure newBalloon ~= nil.

Game speed increases itself on every restart - Corona

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

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.

Assignment confusion in Lua

I am analysing Fishies projest from sample codes of Corona and i couldn't understand that assignment.
background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
Here is the full code:
-- Seed randomizer
local seed = os.time();
math.randomseed( seed )
display.setStatusBar( display.HiddenStatusBar )
-- Preload the sound file (theoretically, we should also dispose of it when we are completely done with it)
local soundID = audio.loadSound( "bubble_strong_wav.wav" )
-- Background
local halfW = display.viewableContentWidth / 2
local halfH = display.viewableContentHeight / 2
-- Create a table to store all the fish and register this table as the
-- "enterFrame" listener to animate all the fish.
local bounceAnimation = {
container = display.newRect( 0, 0, display.viewableContentWidth, display.viewableContentHeight ),
reflectX = true,
}
local backgroundPortrait = display.newImage( "aquariumbackgroundIPhone.jpg", 0, 0 )
local backgroundLandscape = display.newImage( "aquariumbackgroundIPhoneLandscape.jpg", -80, 80 )
backgroundLandscape.isVisible = false
local background = backgroundPortrait
-- Handle changes in orientation for the background images
local backgroundOrientation = function( event )
-- TODO: This requires some setup, i.e. the landscape needs to be centered
-- Need to add a centering operation. For now, the position is hard coded
local delta = event.delta
if ( delta ~= 0 ) then
local rotateParams = { rotation=-delta, time=500, delta=true }
if ( delta == 90 or delta == -90 ) then
local src = background
-- toggle background to refer to correct dst
background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
background.rotation = src.rotation
transition.dissolve( src, background )
transition.to( src, rotateParams )
else
assert( 180 == delta or -180 == delta )
end
transition.to( background, rotateParams )
audio.play( soundID ) -- play preloaded sound file
end
end
-- Add a global listener
Runtime:addEventListener( "orientation", backgroundOrientation )
--
-- Fishies
local numFish = 10
local file1 = "fish.small.red.png"
local file2 = "fish.small.blue.png"
--
-- Define touch listener for fish so that fish can behave like buttons.
-- The listener will receive an 'event' argument containing a "target" property
-- corresponding to the object that was the target of the interaction.
-- This eliminates closure overhead (i.e. the need to reference non-local variables )
local buttonListener = function( event )
if "ended" == event.phase then
local group = event.target
-- tap only triggers change from original to different color
local topObject = group[1]
if ( topObject.isVisible ) then
local bottomObject = group[2]
-- Dissolve to bottomObject (different color)
transition.dissolve( topObject, bottomObject, 500 )
-- Restore after some random delay
transition.dissolve( bottomObject, topObject, 500, math.random( 3000, 10000 ) )
end
-- we handled it so return true to stop propagation
return true
end
end
--
--
--
-- Add fish to the screen
for i=1,numFish do
-- create group which will represent our fish, storing both images (file1 and file2)
local group = display.newGroup()
local fishOriginal = display.newImage( file1 )
group:insert( fishOriginal, true ) -- accessed in buttonListener as group[1]
local fishDifferent = display.newImage( file2 )
group:insert( fishDifferent, true ) -- accessed in buttonListener as group[2]
fishDifferent.isVisible = false -- make file2 invisible
-- move to random position in a 200x200 region in the middle of the screen
group:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) )
-- connect buttonListener. touching the fish will cause it to change to file2's image
group:addEventListener( "touch", buttonListener )
-- assign each fish a random velocity
group.vx = math.random( 1, 5 )
group.vy = math.random( -2, 2 )
-- add fish to animation group so that it will bounce
bounceAnimation[ #bounceAnimation + 1 ] = group
end
--
-- Function to animate all the fish
function bounceAnimation:enterFrame( event )
local container = self.container
container:setFillColor( 0, 0, 0, 0) -- make invisible
local containerBounds = container.contentBounds
local xMin = containerBounds.xMin
local xMax = containerBounds.xMax
local yMin = containerBounds.yMin
local yMax = containerBounds.yMax
local orientation = self.currentOrientation
local isLandscape = "landscapeLeft" == orientation or "landscapeRight" == orientation
local reflectX = nil ~= self.reflectX
local reflectY = nil ~= self.reflectY
-- the fish groups are stored in integer arrays, so iterate through all the
-- integer arrays
for i,v in ipairs( self ) do
local object = v -- the display object to animate, e.g. the fish group
local vx = object.vx
local vy = object.vy
if ( isLandscape ) then
if ( "landscapeLeft" == orientation ) then
local vxOld = vx
vx = -vy
vy = -vxOld
elseif ( "landscapeRight" == orientation ) then
local vxOld = vx
vx = vy
vy = vxOld
end
elseif ( "portraitUpsideDown" == orientation ) then
vx = -vx
vy = -vy
end
-- TODO: for now, time is measured in frames instead of seconds...
local dx = vx
local dy = vy
local bounds = object.contentBounds
local flipX = false
local flipY = false
if (bounds.xMax + dx) > xMax then
flipX = true
dx = xMax - bounds.xMax
elseif (bounds.xMin + dx) < xMin then
flipX = true
dx = xMin - bounds.xMin
end
if (bounds.yMax + dy) > yMax then
flipY = true
dy = yMax - bounds.yMax
elseif (bounds.yMin + dy) < yMin then
flipY = true
dy = yMin - bounds.yMin
end
if ( isLandscape ) then flipX,flipY = flipY,flipX end
if ( flipX ) then
object.vx = -object.vx
if ( reflectX ) then object:scale( -1, 1 ) end
end
if ( flipY ) then
object.vy = -object.vy
if ( reflectY ) then object:scale( 1, -1 ) end
end
object:translate( dx, dy )
end
end
-- Handle orientation of the fish
function bounceAnimation:orientation( event )
print( "bounceAnimation" )
for k,v in pairs( event ) do
print( " " .. tostring( k ) .. "(" .. tostring( v ) .. ")" )
end
if ( event.delta ~= 0 ) then
local rotateParameters = { rotation = -event.delta, time=500, delta=true }
Runtime:removeEventListener( "enterFrame", self )
self.currentOrientation = event.type
for i,object in ipairs( self ) do
transition.to( object, rotateParameters )
end
local function resume(event)
Runtime:addEventListener( "enterFrame", self )
end
timer.performWithDelay( 500, resume )
end
end
Runtime:addEventListener( "enterFrame", bounceAnimation );
Runtime:addEventListener( "orientation", bounceAnimation )
-- This function is never called,
-- but shows how we would unload the sound if we wanted to
function unloadSound()
audio.dispose(soundID)
soundID = nil
end
Lua has a slightly strange behaviour when it comes to ands and ors.
The expression a and b evaluates to a if a is considered false (only nil and false is considered false, all other values including 0 are considered true) and if a is considered true the expression evaluates to b.
The expression a or b evaluates to a if a is considered true and b if a is considered false.
Note: in both cases if the expression evaluates to a that value of b isn't even evaluated. This is called short circuit logic. In and if a is false, the and can't possibly be true, so there is no point in wasting computation time to evaluate b. Similarly, if a is true in a or b there is no point to evaluate b as the or can't possibly be false.
The construct cond and valiftrue or valiffalse (or the equivalent, due to operator precedence, (cond and valiftrue) or valiffalse) is equivalent to other language's ternary if statement, with one caveat: valiftrue must not evaluate to false. If that is the case, the whole expression will always evaluate to valiffalse. (Try and reason it out, that's the best way I find to get a grip on this construct.)
was about to post in detail but #JPvdMerwe got it spot on.
To be precise,
background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
translates to
if backgroundLandscape == background then
background = backgroundPortrait
else
background = backgroundLandscape
end
EDIT
As #JPvdMerwe pointed out, in this case, if backgroundPortrait is false then
background = backgroundLandscape
will be executed all the time.

Resources