Corona pass parameter in gotoScene - lua

I refer to corona website reference using these to pass parameters to another scene.
main.lua
local options =
{
effect = "slideLeft",
time = 800,
params = { var1 = "custom", myVar = "another" }
}
storyboard.gotoScene( "notificationPage", options )
then on my other scene
notificationPage.lua
function scene:enterScene( event )
local group = self.view
local params = event.params
print( params.var1 )
print( params.myVar )
end
it return error attempt to index local 'params' (a nil value). why is that ? and how do i do it correctly?

It should be placed in "createScene" instead of "enterScene".
And as you may read in Corona Docs "This library is planned for deprecation. If you are starting a new project, you should use the composer.* scene management library instead."

It's in the 'createScene' event handler only, try this code:
function oScene:createScene( oEvent )
local oGroup = self.view
local aParams = oEvent.params
if aParams then
print (aParams.var1)
print (aParams.myVar)
end

You should use scene:create function:
function scene:create( event )
local sceneGroup = self.view
local params = event.params
print( params.var1 )
print( params.myvar )
end
you can download scene template from coronalabs.com

Related

string passed as an argument turns into a table. lua

I have a function that whenever I pass a string into, it turns into a table and I don't know why. Any help is very appreciated. Here's my code:
function gotoSkip(para)
print(para)
print(type(para))
--scene:removeEventListener( "create", scene )
--composer.gotoScene(para)
end
function scene:create( event )
local sceneGroup = self.view
background = display.newImageRect(sceneGroup,"images/white.jpg",768,1500)
background.x = display.contentCenterX
background.y = display.contentCenterY
local skipButton = display.newText(sceneGroup, "Skip", 600, 1300, nativeSystemFont, 60)
skipButton:setFillColor(0,0,256)
skipButton:addEventListener("tap",gotoSkip, "StringExample")
end
Here is the result of the two print statements respectively:
table: 0D27FB88 (or some other memory address)
table
addEventListener function only gets 2 arguments, so you should remove that third argument.
skipButton:addEventListener("tap", gotoSkip)
If you wanna pass parameter to a function from an Event Listener, there are 2 ways;
1-) You can set a property for event.target;
function gotoSkip(event)
print( event.target.para)
print( type(event.target.para) )
end
local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)
skipButton.para= "StringExample"
skipButton:addEventListener("tap", gotoSkip)
2-) You can use a sub function;
function gotoSkip(para)
print( para )
print( type(para) )
end
local skipButton = display.newText("Skip", 400, 400, nativeSystemFont, 60)
function subFunc()
gotoSkip("StringExample")
end
skipButton:addEventListener("tap", subFunc)
You can remove its listener like this;
skipButton:removeEventListener("tap", subFunc)
Also nativeSystemFont isn't a valid constant unless you defined it. Correct one is native.systemFont.

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.

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.

Corona Sdk - Is there a way to call and group:insert() an object from another lua file?

As stated in the title I would like to not only call an object from an external Lua file, but I would also like to group:insert() this object into my Menu page with the properties given to it in the external lua file. Is this possible and/or efficient? I would just really like to make sure data isn't repeated through out my project.
EDIT
Here's my code so far:
The group:insert() function is throwing me an error stating it was expecting a table and that I might have been trying to call a function in which case i should use ":" instead of "."
This is menu.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require "widget"
local m = require ("myData")
local menuFunction = require("menuFunction")
local menuSwipe
-- =======================
-- menuSwipe()
-- =======================
menuSwipe = function(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator > 0) then
menuDown(m.invisiBar, event)
else
--m.layerInfo = layers
transition.to( menuFunction.menuBar, { x = menuFunction.menuBar.x, y = 0, time = 200 } )
--transition.to( layers, { x = menuFunction.menuBar.x, y = h, time = 100 } )
m.invisiBar = display.newRect( 0,0,w,25,6)
m.invisiBar.alpha = 0
m.menuActivator = 1
end
end
end
-- ++++++++++++++++++++++
-- menuDown()
-- ++++++++++++++++++++++
function menuDown(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator == 1) then
transition.to( menuFunction.menuBar, { x = m.menuInfo.x, y = h*.964, time = 200 } )
--transition.to( group, { x = 0, y = 0, time = 10 } )
m.menuActivator = 0
end
end
end
function scene:createScene( event )
local group = self.view
group:insert( menuFunction.menuBar ) -- *** ERROR occurs here
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
This is menuFunction.lua:
local m = require("myData")
local menu = require ("menu")
local w = display.contentWidth
local h = display.contentHeight
local menuFunction = {}
--menuBar
menuFunction.menuBar = display.newImage( "images/menuBar1.png")
menuFunction.menuBar.x = w*(1/2)
menuFunction.menuBar.y = h*1.465
menuFunction.menuBar.height = h
menuFunction.menuBar:setReferencePoint(display.TopLeftReferencePoint)
menuFunction.menuBar.touch = menu.menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
This is the exact error message:
ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
message**
Does this happen every time this code is called, or does it by any chance work the first time and then crashes? In your case, code could work the first time you enter the scene, but the second time you do, it may crash [if you remove scenes in between].
When you do a 'require' of a file, its contents are executed and returned value is saved in the global packages table. When you require the same file again, the returned value is taken from the global packages table instead, the code is not executed again.
So if you by any chance require this file in one spot of your app, and then call :removeSelf() and nil the reference of the menuBar, the display object will be removed and its reference will cease to exist, and calling the require again, will not recreate the object. Fully removing a scene will also remove the display objects.
So what you wanted to achieve is very sensible [contrary to what #Schollii says], but your "module" should allow creation of multiple objects if you want to get rid of them during runtime.
I'm not going to correct your code, just a simple example of how you can achieve this:
-- menu.lua
local menuCreator = {}
menuCreator.newMenu = function(params)
local menu = display.newGroup()
-- create your menu here
return menu
end
return menuCreator
Now anytime you do:
local menuCreator = require("menu.lua")
you will be able to call:
local menu = menuCreator.newMenu(someParams)
and get yourself a nice new menu wherever you need.
If it's not shown all the time on screen, it may be better to create a new one whenever you need it, and then remove it from the memory.
There are several issues with this, and none of them seem related to your error but fixing them will either also fix the error or make the cause of the error more obvious. Please fix following and update:
Although Lua allows it, don't use circular includes, where A includes B which includes A. Instead have menu require menuFunction and then call a creation function in menuFuntion:
-- menuFunction.lua
local m = require("myData")
-- require("menu") -- BAD! :)
local w = display.contentWidth
local h = display.contentHeight
local menuBar = display.newImage( "images/menuBar1.png")
menuBar.x = w*(1/2)
menuBar.y = h*1.465
menuBar.height = h
menuBar:setReferencePoint(display.TopLeftReferencePoint)
local menuFunction = { menuBar = menuBar }
function createMenuBar(menuSwipe)
menuFunction.menuBar.touch = menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
end
-- menu.lua
function createScene(event)
local mf = require('menuFunction')
mfFunction = mf.createMenuBar(menuSwipe)
group:insert(menuFunction.menuBar)
end
Secondly out of the four calls to group:insert() the first 3 refer to objects that are not shown in the code and don't see relevant to problem, they should be removed or if you think relevant, comment why their code now shown, or show their code.

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