Lua - How to create a slide-show? - lua

This is one version of the code required for a scene to scene transition in Lua with a timer and without user input, just like a slide-show:

main.lua:
display.setStatusBar(display.HiddenStatusBar)
local composer = require "composer"
composer.gotoScene("scene1")
This takes you directly to scene1.
scene1:
local composer = require ( "composer")
local scene = composer.newScene()
local function showScene2()
local options = {
effect = "fade",
time = 50,
}
composer.gotoScene( "scene2", options)
end
-- create scene
function scene:createScene ( event )
local sceneGroup = self.view
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will") then
local background = display.newImage("images/picture1.png", 240, 160)
sceneGroup:insert( background )
elseif ( phase == "did") then
timer.performWithDelay(3000, showScene2)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
end
--end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene)
scene:addEventListener( "show", scene)
scene:addEventListener( "hide", scene)
scene:addEventListener( "destroy", scene)
return scene
scene2 (etc..) is exactly the same except for the next scene's name -
-- local function showScene3()
-- composer.gotoScene( "scene3", options)
-- timer.performWithDelay(3000, showScene3)

Related

timer.performWithDelay() not returning table on variable reassignment

Goal
I have a global variable made to store the table returned by timer.performWithDelay. My goal was, in the scene:show() function for the timer to be cancelled, and recreated with a new delay.
Problem
I'm getting a nil return value in the variable used to store the table when the timer is recreated.
Code:
local timerVar
local function update()
print("updating")
print(timerVar)
timer.cancel(timerVar)
timerVar = timer.performWithDelay(delay, timerFunction, 0)
print(timerVar)
end
function scene:create(event)
timerVar = timer.performWithDelay(delay, timerFunction, 0)
end
function scene:show(event)
if (phase == "will") then
update()
timer.resume(timerVar)
end
end
function scene:hide(event)
if (phase == "will") then
timer.pause(timerVar)
end
end
Console output:
updating
table: 095D9CA8
nil
What's happening here?
Is timer.cancel() removing the the timerVar variable altogether?
If I can't keep the timer, how can I get around this so that I can have the timer table stored under the same name and with the same scope, but born anew?
I try reproduce your problem but got
updating
15:28:47.324 table: 0091F958
15:28:47.324 table: 0772C590
15:28:47.324 WARNING: timer.resume( timerId ) ignored because timerId was not paused.
My code:
main.lua
local composer = require( 'composer' )
composer.gotoScene( 'test' )
test.lua
local composer = require( "composer" )
local scene = composer.newScene()
local timerVar
local delay = 1000
local function timerFunction()
end
local function update()
print("updating")
print(timerVar)
timer.cancel(timerVar)
timerVar = timer.performWithDelay(delay, timerFunction, 0)
print(timerVar)
end
function scene:create( event )
local sceneGroup = self.view
timerVar = timer.performWithDelay(delay, timerFunction, 0)
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
update()
timer.resume(timerVar)
elseif ( phase == "did" ) then
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
timer.pause(timerVar)
elseif ( phase == "did" ) then
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene
Try install latest stable version of Corona.
This is not technically a direct answer to my question, but it did solve my problem, so I'll put my workaround here for future users. I was able to avoid cancelling the timer altogether by simply changing the delay of the timer. this is accomplished as simply as:
timerVar._delay = newDelay
Note the underbar before the delay variable, it's easy to miss.
This allowed me to update the delay without making a new timer.

Why is collision occurring without any contact with other objects? (Corona SDK composer)

Here is my code. I'm making a 3 lane car racing game. Here's the screen shot.
On collision of the red car with any of the other cars I'm going into the restart.lua file where on tap of the screen I'm coming back to the game.lua file (code shown below). The problem occurs when control shifts back from the restart.lua to game.lua.
What happens is that the collision event occurs for no reason causing the game to continuously glitch and shift between restart and game.lua . Does anyone know why this is happening and how it could be stopped?
local composer = require( "composer" )
local scene = composer.newScene()
local pix=0
local no=0
local physics=require "physics"
physics.start()
local x=2000
local prevP=0
local prevS=5
local count=1
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called
-- -----------------------------------------------------------------------------------------------------------------
-- Local forward references should go here
-- -------------------------------------------------------------------------------
local opp1
local opp
local sceneGroup
local roadCopy
local road
local car
function removeAllListeners(self)
self._functionListeners = nil
self._tableListeners = nil
end
local function touchScreen(event)
print("touchScreen")
if event.x < dw/2 then
if car.current==car.posB then
car.x=car.posA
car.current=car.posA
elseif car.current == car.posC then
car.x=car.posB
car.current=car.posB
end
else
if car.current==car.posB then
car.x=car.posC
car.current=car.posC
elseif car.current == car.posA then
car.x=car.posB
car.current=car.posB
end
end
end
local function removeListeners()
removeAllListeners(road)
removeAllListeners(roadCopy)
removeAllListeners(opp)
removeAllListeners(car)
road:removeEventListener("tap",touchScreen)
roadCopy:removeEventListener("tap",touchScreen)
car:removeEventListener( "collision",onLocalCollision)
composer.removeScene( "game")
composer.gotoScene( "restart")
end
function onLocalCollision( event )
print( "collision" )
removeListeners()
end
local function moveRoad(self,event)
if self.y > dh-20 then
self.y=20
pix=pix+10
if pix > 100 then
road.speed=road.speed+1
roadCopy.speed=roadCopy.speed+1
pix=0
else
end
else
self.y=self.y+self.speed
pix=pix+1
if pix > 100 then
road.speed=road.speed+1
roadCopy.speed=roadCopy.speed+1
pix=0
else
end
end
if road.speed>30 or roadCopy.speed>30 then
road.speed=30
roadCopy.speed=30
end
end
local function moveoppcar(self, event)
--to move the incoming cars
if(self.y==nil)then
return
end
if(self.y>dh) then
Runtime:removeEventListener( "enterFrame", self )
self:removeSelf()
self=nil
randomobject1()
else
self.y=self.y+self.speed+roadCopy.speed
end
end
function randomobject1(event)
--to randomly generate the incoming cars
math.randomseed( os.time() )
opp=display.newRect( center-dw/4.75, 30, dw/8, dw/4.5 )
local position = math.random(1, 3)
local cartype = math.random(1, 3)
if(position==prevP) then
position=(prevP+2)%3
end
if position==1 then
opp.x=center-dw/4.75
end
if position == 2 then
opp.x=center
end
if position == 3 then
opp.x=center+dw/4.75
end
if cartype == 1 then
opp.fill={type="image", filename="car_green_1.png"}
end
if cartype == 2 then
opp.fill={type="image", filename="car_blue_1.png"}
end
if cartype == 3 then
opp.fill={type="image", filename="car_red_1.png"}
end
opp.speed=math.random(1,10)
sceneGroup:insert(opp)
prevP=position
prevS=opp.speed
local r=opp.width
physics.addBody(opp,"static",{density=1, bounce=0.1, friction=0.2,radius=5})
opp.gravityScale=0
opp.enterFrame=moveoppcar
Runtime:addEventListener("enterFrame", opp)
end
-- "scene:create()"
function scene:create( event )
print("Scene:create")
sceneGroup = self.view
---------ROAD---------
road=display.newRect( 0, 0, dw, dh )
road.fill={type="image",filename="road.png"}
road.anchorX,road.anchorY=0,0
road.contentHeight=dh
sceneGroup:insert(road)
road.speed=5
roadCopy=display.newRect( 0, 0, dw, dh )
roadCopy.fill={type="image",filename="road.png"}
roadCopy.anchorX,roadCopy.anchorY=0,1
roadCopy.contentHeight=dh
sceneGroup:insert(roadCopy)
roadCopy.speed=5
----PLAYER CAR--------
car=display.newRect( dw/2, dh-10, dw/8, dw/4.5 )
car.fill={type="image", filename="player.png"}
car.anchorY=1
center=dw/2
car.posA=center-dw/4.75
car.posB=center
car.posC=center+dw/4.75
car.current=car.posB
sceneGroup:insert(car)
physics.addBody(car,"dynamic",{density=1, bounce=0.1, friction=0.2})
car.gravityScale=0
--[[randomobject1()
randomobject1()]]
end
-- "scene:show()"
function scene:show( event )
if(event.phase=="did") then
print("Scene:show")
addList()
randomobject1()
randomobject1()
end
end
function addList()
--adding event listeners
car.collision = onLocalCollision
car:addEventListener( "collision", car )
road.enterFrame=moveRoad
Runtime:addEventListener( "enterFrame", road)
roadCopy.enterFrame=moveRoad
Runtime:addEventListener( "enterFrame", roadCopy)
road:addEventListener("tap",touchScreen)
roadCopy:addEventListener("tap",touchScreen)
end
-- "scene:hide()"
function scene:hide( event )
print("Scene:hide")
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 )
print("Scene:destroy")
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- 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
You have to remove physics bodies manually., I suggest to create a group and keep all the physics bodies inside that., and remove it manually when screen hides.

how to collide objects in corona labs

I'm trying to make a game in Corona SDK where a player runs and survives through obstacles by either jumping over them or killing them using bullets. The problem is that the game ends even if the player fires the bullet. But it should be over only if the player collides with the obstacles.
This is my code till now!!
--local screen_adjustment = 1
local physics = require "physics"
physics.start()
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
function scene:createScene( event )
local screenGroup=self.view
background=display.newImage("rsz_islands1-background-animation.png")
background:setReferencePoint(display.BottomLeftReferencePoint)
background.x=-50
background.y=330
background.speed=math.random(2,6)
screenGroup:insert(background)
background1=display.newImage("rsz_islands1-background-animation.png")
background1:setReferencePoint(display.BottomLeftReferencePoint)
background1.x=500
background1.y=330
background1.speed=math.random(2,6)
screenGroup:insert(background1)
rightArrow=display.newImageRect("right_arrow.png",50,100)
rightArrow:setReferencePoint(display.BottomLeftReferencePoint)
rightArrow.x=370
rightArrow.y=290
screenGroup:insert(rightArrow)
upArrow=display.newImageRect("up_Arrow.png",100,50)
upArrow:setReferencePoint(display.BottomLeftReferencePoint)
upArrow.x=250
upArrow.y=240
screenGroup:insert(upArrow)
stone=display.newImageRect("stone.png",100,50)
stone:setReferencePoint(display.BottomLeftReferencePoint)
stone.x=math.random(500,1500)
stone.y=280
stone.speed=math.random(2,6)
physics.addBody(stone,"static",{bounce=0,friction=0,})
screenGroup:insert(stone)
base = display.newRect(0,0,450,3)
base.x=-50
base.y=282
base:setReferencePoint(display.BottomLeftReferencePoint)
base:setFillColor(255,255,255)
--physics.addBody(base,"static",{bounce=0.1,friction=0.1})
physics.addBody(base,"static",{bounce=0,friction=1.0,density=1.0})
screenGroup:insert(base)
base.myName="base1"
local sheetData = {
width=65,
height=75,
numFrames=4,
sheetContentWidth=130,
sheetContentHeight=150
}
local mySprite = graphics.newImageSheet("imageSheet.png",sheetData)
local sequenceData = {
name="normalRun",
frames={1,2,3,4},
time=500,
loopcount=0
}
animation = display.newSprite(mySprite,sequenceData)
if(animation.x<50) then
animation.x=50
end
animation.y=245
physics.addBody(animation,{bounce=0.3,friction=1.0,density=1.0,radius=35})
animation:play()
screenGroup:insert(animation)
animation.myName="animation1"
local sheetTigerData = {
width=100,
height=57,
numFrames=8,
sheetContentWidth=400,
sheetContentHeight=120
}
local myTigerSprite
graphics.newImageSheet("tigerImageSheet.png",sheetTigerData)
local sequenceTigerData = {
name="normalRun",
frames={1,2,3,4,5,6,7,8},
time=500,
loopcount=0
}
tigeranimation = display.newSprite(myTigerSprite,sequenceTigerData)
--tigeranimation.x=700
tigeranimation.x=math.random(500,1500)
tigeranimation.y=255
tigeranimation.speed=math.random(2,6)
physics.addBody(tigeranimation,"static",{bounce=0,friction=.2})
tigeranimation:play()
screenGroup:insert(tigeranimation)
local sheetBirdData = {
width=60,
height=40,
numFrames=6,
sheetContentWidth=180,
sheetContentHeight=80
}
local myBirdSprite =
graphics.newImageSheet("birdimagesheet.png",sheetBirdData)
local sequenceBirdData = {
name="normalRun",
frames={1,2,3,4,5,6},
time=500,
loopcount=0
}
birdanimation = display.newSprite(myBirdSprite,sequenceBirdData)
birdanimation.x=math.random(500,2500)
birdanimation.y=math.random(100,200)
birdanimation.speed=2
birdanimation.initY=birdanimation.y
birdanimation.amp=50
birdanimation.angle=math.random(1,360)
physics.addBody(birdanimation,"static",{friction=.2,bounce=0})
birdanimation:play()
screenGroup:insert(birdanimation)
local sheetCatData = {
width=100,
height=48,
numFrames=8,
sheetContentWidth=200,
sheetContentHeight=200
}
local myCatSprite =
graphics.newImageSheet("rsz_runningcat.png",sheetCatData)
local sequenceCatData = {
name="normalRun",
frames={2,1,4,3,6,5,8,7},
time=500,
loopcount=0
}
catanimation = display.newSprite(myCatSprite,sequenceCatData)
catanimation.x=tigeranimation.x+math.random(110,1500)
catanimation.y=255
catanimation.speed=math.random(2,6)
physics.addBody(catanimation,"static",{bounce=0,friction=.2})
catanimation:play()
screenGroup:insert(catanimation)
gameSound=audio.loadStream("gamesound.mp3")
audio.play(gameSound)
gunshot=audio.loadStream("gunshot3.mp3")
end
function scrollBack( self,event )
if self.x<-590 then
self.x=500
audio.play(gameSound)
else
self.x=self.x-3
audio.play(gameSound)
end
end
function moveTiger( self,event )
if self.x<-100 then
self.x=math.random(1500,4000)
self.y=255
self.speed=math.random(3,6)
audio.play(gameSound)
else
self.x=self.x-self.speed
audio.play(gameSound)
end
end
function moveCat( self,event )
if self.x<-100 then
self.x=tigeranimation.x+math.random(800,2000)
self.y=255
self.speed=math.random(3,6)
else
self.x=self.x-self.speed
end
end
function moveStone( self,event )
if self.x<-100 then
self.x=math.random(1000,5000)
self.y=280
self.speed=math.random(3,6)
else
self.x=self.x-self.speed
end
end
function moveBird( self,event )
if self.x<-100 then
self.x=math.random(500,2500)
--self.x=300
self.y=math.random(100,200)
self.speed=2
self.amp=math.random(25,75)
self.angle=math.random(1,360)
--self.y=280
--self.speed=math.random(3,6)
else
self.x=self.x-self.speed
self.angle=self.angle+.1
self.y=self.amp*math.sin(self.angle)+self.initY
end
end
function fireLasers()
blaster = display.newImageRect( "bullet.png", 40, 15 )
physics.addBody(blaster,"dynamic")
blaster.x = animation.x+50
blaster.y = animation.y-10
--blaster.collided=false
if(animation.x<50) then
animation.x=50
end
transition.to( blaster, { time=1000, x=500} )
audio.play(gunshot)
end
function handleFireButton( event )
if ( event.phase == "began" ) then
-- Fire the weapon
fireLasers()
elseif ( event.phase == "ended" ) then
-- Stop firing the weapon
fireLasers()
end
return true
end
positionInAir = false
function jump(event)
if(event.phase == "began" and positionInAir==false) then
--playerInAir = true
--animation:setLinearVelocity( 0, 1 )
animation:applyForce(0,-1000,animation.x,animation.y)
positionInAir=true
--physics.addBody(animation,"dynamic")
--print("touch")
end
return true
end
function onCollision( event )
if(event.object1.myName == "base1" and event.object2.myName == "animation1")
then
positionInAir = false;
-- base:removeSelf()
end
end
local function onManCollide(event)
if ( event.phase == "began" ) then
storyboard.gotoScene("restart","fade",400)
audio.stop()
end
end
function scene:enterScene( event )
background.enterFrame=scrollBack
Runtime:addEventListener("enterFrame",background)
background1.enterFrame=scrollBack
Runtime:addEventListener("enterFrame",background1)
stone.enterFrame=moveStone
Runtime:addEventListener("enterFrame",stone)
tigeranimation.enterFrame=moveTiger
Runtime:addEventListener("enterFrame",tigeranimation)
birdanimation.enterFrame=moveBird
Runtime:addEventListener("enterFrame",birdanimation)
catanimation.enterFrame=moveCat
Runtime:addEventListener("enterFrame",catanimation)
animation.collision=onManCollide
animation:addEventListener("collision",onManCollide)
rightArrow:addEventListener( "touch", handleFireButton )
--Runtime:addEventListener( "touch", handleFireButton )
--Runtime:addEventListener( "enterFrame", handleFireButton )
--upArrow:addEventListener("touch", jump)
--Runtime:addEventListener( "collision", onCollision )
--Runtime:addEventListener( "touch", onScreenTouch )
--rightArrow:addEventListener( "touch", handleFireButton )
end
function scene:exitScene( event )
Runtime:removeEventListener("enterFrame",background)
Runtime:removeEventListener("enterFrame",background1)
Runtime:removeEventListener("enterFrame",stone)
Runtime:removeEventListener("enterFrame",tigeranimation)
Runtime:removeEventListener("enterFrame",birdanimation)
Runtime:removeEventListener("enterFrame",catanimation)
Runtime:removeEventListener("enterFrame",onManCollide)
rightArrow:removeEventListener( "touch", handleFireButton )
upArrow:removeEventListener("touch", jump)
Runtime:removeEventListener( "collision", onCollision )
end
function scene:destroyScene( event )
end
--local myGroup=display.newGroup()
--physics.addBody(background,"static",{bounce=0.1,friction=0.9})
--physics.addBody(background1,"static",{ bounce=0.1,friction=0.9})
scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene",scene)
return scene
You need to ask a specific question, what you have said is quite vague. Does the game end each time the player fires the bullet? Also you should consider migrating from storyboard to composer.

First scene won't disappear when second scene is called. Lua

I am trying to call my second scene and have the first disappear completely. The objects on the second scene are appearing, but they are overlapping those on the first scene. I've tried calling the scene.hide() and scene.destroy() functions manually and I've also tried adding objects to a group. Oddly enough, some of the objects weren't visible when added to a group, but others were. Please help! Also, sorry only some of the code is in the separate boxes, but I don't know how to fix it!
function scene:create( event )
local sceneGroup = self.view
display.setStatusBar( display.HiddenStatusBar )
local group = display.newGroup()
-------Set up green buttons
local greenButton3=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton3.x=display.contentCenterX
greenButton3.y=(970)
greenButton3.button = 1
local greenButton2=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton2.x=display.contentCenterX
greenButton2.y=(870)
greenButton2.button = 2
local greenButton1=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton1.x=display.contentCenterX
greenButton1.y=(770)
greenButton1.button = 3
--------SET UP RED BUTTONS
local redButton1=display.newImageRect("images/red-dim.png",520,60)
redButton1.x=display.contentCenterX
redButton1.y=(40)
local redButton2=display.newImageRect("images/red-dim.png",520,60)
redButton2.x=display.contentCenterX
redButton2.y=(140)
local redButton3=display.newImageRect("images/red-dim.png",520,60)
redButton3.x=display.contentCenterX
redButton3.y=(240)
---------------TEXT
---Questions
local question1green=display.newText("How are you feeling?", 0,0,native.systemFontBold, 40)
question1green.x=display.contentCenterX
question1green.y=(705)
local question1red=display.newText("The other person is feeling...", 0,0,native.systemFontBold, 40)
question1red.x=display.contentCenterX
question1red.y=(300)
question1red.rotation = 180
---Answers
--Green
local answer1green=display.newText("Sad",0,0,native.systemFontBold, 40)
answer1green.x=display.contentCenterX
answer1green.y=(773)
local answer2green=display.newText("Angry",0,0,native.systemFontBold, 40)
answer2green.x=display.contentCenterX
answer2green.y=(870)
local answer3green=display.newText("I don't know",0,0,native.systemFontBold, 40)
answer3green.x=display.contentCenterX
answer3green.y=(973)
--Red
local answer1red=display.newText("Sad",0,0,native.systemFontBold, 40)
answer1red.x=display.contentCenterX
answer1red.y=(238)
answer1red.rotation = 180
local answer2red=display.newText("Angry",0,0,native.systemFontBold, 40)
answer2red.x=display.contentCenterX
answer2red.y=(141)
answer2red.rotation = 180
local answer3red=display.newText("Confused",0,0,native.systemFontBold, 40)
answer3red.x=display.contentCenterX
answer3red.y=(38)
answer3red.rotation = 180
--------CREATE EMPTY BODY OUTLINE
local bodyOutline=display.newImageRect("images/bodyoutline.png", 380,560)
bodyOutline.x=display.contentCenterX
bodyOutline.y=display.contentCenterY-10
bodyOutline.rotation = 270
--------move text to front after new buttons are created
function moveTextToFront (event)
answer1green:toFront()
answer2green:toFront()
answer3green:toFront()
answer1red:toFront()
answer2red:toFront()
answer3red:toFront()
end
------------------------------------------------------------
--When the Buttons are Clicked
------------------------------------------------------------
local function buttonClicked (event)
print("here")
--[[function destroyAll (event)
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
print("destroyed")
question1green:removeSelf()
question1green = nil
question1red:removeSelf()
question1red = nil
answer1green:removeSelf()
answer1green = nil
answer2green:removeSelf()
answer2green = nil
answer3green:removeSelf()
answer3green = nil
answer1red:removeSelf()
answer1red = nil
answer2red:removeSelf()
answer2red = nil
answer3red:removeSelf()
answer3red = nil
--]]
--end
--destroyAll()
------------------------------------------------------------
--Start of Results Creation (SETUP)
------------------------------------------------------------
function createGray1 (event)
local gray1=display.newImageRect("images/grayRSU.png",520,60)
gray1.x=display.contentCenterX
gray1.y=(970)
end
function createGray2 (event)
local gray2=display.newImageRect("images/grayRSU.png",520,60)
gray2.x=display.contentCenterX
gray2.y=(870)
end
function createGray3 (event)
local gray3=display.newImageRect("images/grayRSU.png",520,60)
gray3.x=display.contentCenterX
gray3.y=(770)
end
function createGray4 (event)
local gray4=display.newImageRect("images/grayUD.png",520,60)
gray4.x=display.contentCenterX
gray4.y=(240)
end
function createGray5 (event)
local gray5=display.newImageRect("images/grayUD.png",520,60)
gray5.x=display.contentCenterX
gray5.y=(140)
end
function createGray6 (event)
local gray6=display.newImageRect("images/grayUD.png",520,60)
gray6.x=display.contentCenterX
gray6.y=(40)
end
function firstClicked (event)
local greenClicked1=display.newImageRect("images/green-clicked.png",520,60)
greenClicked1.x=display.contentCenterX
greenClicked1.y=(970)
local redClicked1=display.newImageRect("images/red-clicked.png",520,60)
redClicked1.x=display.contentCenterX
redClicked1.y=(240)
end
function secondClicked (event)
local greenClicked2=display.newImageRect("images/green-clicked.png",520,60)
greenClicked2.x=display.contentCenterX
greenClicked2.y=(870)
local redClicked2=display.newImageRect("images/red-clicked.png",520,60)
redClicked2.x=display.contentCenterX
redClicked2.y=(140)
end
function thirdClicked (event)
local greenClicked3=display.newImageRect("images/green-clicked.png",520,60)
greenClicked3.x=display.contentCenterX
greenClicked3.y=(770)
local redClicked3=display.newImageRect("images/red-clicked.png",520,60)
redClicked3.x=display.contentCenterX
redClicked3.y=(40)
end
------------------------------------------------------------
--End of Results Creation (SETUP)
------------------------------------------------------------
--local function sceneDone (event)
--end
-------------------------
--transition to next scene IN THIS FUNCTION
if (event.target.button == 1) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
firstClicked()
createGray2()
createGray3()
createGray5()
createGray6()
--composer.removeScene("firstscene")
end
if (event.target.button == 2) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
secondClicked()
createGray1()
createGray3()
createGray4()
createGray6()
--composer.removeScene("firstscene")
end
if (event.target.button == 3) then
greenButton1:removeSelf()
greenButton1 = nil
greenButton2:removeSelf()
greenButton2 = nil
greenButton3:removeSelf()
greenButton3 = nil
redButton1:removeSelf()
redButton1 = nil
redButton2:removeSelf()
redButton2 = nil
redButton3:removeSelf()
redButton3 = nil
thirdClicked()
createGray1()
createGray2()
createGray4()
createGray5()
--composer.removeScene("firstscene")
end
moveTextToFront()
composer.gotoScene("secondscene")
end
greenButton1:addEventListener("touch", buttonClicked)
greenButton2:addEventListener("touch", buttonClicked)
greenButton3:addEventListener("touch", buttonClicked)
end --end of images and stuff
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
print("hidden2")
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
--
-- INSERT code here to pause the scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == "did" then
print("hidden")
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
if playBtn then
playBtn:removeSelf() -- widgets must be manually removed
playBtn = nil
end
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene
I can't see in your code that you are adding your object into the sceneGroup (self.view). Make sure you insert them into that group for example scneeGroup:insert( greenButton3 ).
When changing scene there is no need to hide the objects in the sceneGroup as they will be hidden automatically by Corona.
You have to add all the display objects in to the sceneGroup.
function scene:create( event )
local sceneGroup = self.view
display.setStatusBar( display.HiddenStatusBar )
local group = display.newGroup()
-------Set up green buttons
local greenButton3=display.newImageRect("images/green-unclicked.png", 520,60)
greenButton3.x=display.contentCenterX
greenButton3.y=(970)
greenButton3.button = 1
sceneGroup :insert(greenButton3)
end
try adding all the text objects everything in to the sceneGroup.

Images doesn´t appear in storyboard Corona SDK

I am doing a game in corona SDK, but I have this little problem.
I have a menu with a button. If I press it, it sends me to the first level of my game.
When I pass the final level, the game return me to the menu. Bur, if I start playing the first again, my images doesn´t appear.
The images are balls, and to pass the level, you have to eliminate all the balls. To do this, I use:
ball:removeSlef()
ball = nil
But, I don´t think that this is the problem, because I eliminate this lines, and it doesn´t work.
The images are create in scene:createScene function, and insert in the Group.
I short the code of the first level to be understood.
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local physics = require "physics"
physics.start(); physics.pause()
physics.setGravity( 0, 0 )
local cont = 0
local bur = {}
function eliminar1( event )
if (cont == 0) and (event.phase == "began") then
event.target:removeSelf()
bur[1] = nil
cont = cont + 1
end
end
function eliminar2( event )
if (cont == 1) and (event.phase == "began") then
bur[2]:removeSelf()
bur[2] = nil
cont = cont + 1
end
end
function eliminar3( event )
if (cont == 2) and (event.phase == "began") then
bur[3]:removeSelf()
bur[3] = nil
storyboard.gotoScene( "levels.1.level2" )
end
end
function scene:createScene ( event )
local screenGroup = self.view
for i = 1,3 do
bur[i] = display.newImage("irudiak/"..i..".png")
bur[i]:translate(math.random(0,280), math.random(0,400) )
physics.addBody( bur[i], {bounce = 0.3 } )
bur[i]:setLinearVelocity(math.random(-50,50), math.random(-50,50) )
screenGroup:insert(bur[i])
end
bur[1]:addEventListener("touch", eliminar1)
bur[2]:addEventListener("touch", eliminar2)
bur[3]:addEventListener("touch", eliminar3)
end
function scene:enterScene( event )
local screenGroup = self.view
physics.start()
end
function scene:exitScene( event )
local screenGroup = self.view
physics.stop()
end
function scene:destroyScene( event )
local screenGroup = self.view
package.loaded[physics] = nil
physics = nil
end
return scene
createScene is ran only first time when you gotoScene. Every next time only willEnterScene and enterScene are played. To play createScene again you have to remove it (storyboard.removeScene() I guess). Or you can move some stuff you need to willEnterScene. For more detailed info you can watch this: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Resources