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/
Related
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.
recently I was coding a new game when I ran across a problem of which I cannot seem to be able to fix.
This is the code :
function newPower()
rand = math.random( 100 )
if (rand < 80) then
powerup = display.newImage("power.png");
powerup.class = "powerup"
powerup.x = 60 + math.random( 160 )
powerup.y = -100
physics.addBody( powerup, { density=0.9, friction=0.3, bounce=0.3} )
powerup:addEventListener( "touch", handlePowerTouch )
end
end
local function handlePowerTouch( event )
if event.phase == "began" then
currentScore = currentScore * 2
currentScoreDisplay.text = string.format( "%06d", currentScore )
event.target:removeSelf()
return true
end
end
local function spawnpowers()
-- Spawn a new powerup every second until canceled.
spawnPower = timer.performWithDelay( 1000, newPower, -1 )
end
Any help fixing this issue would be greatly appreciated!
The issue I'm having is when I click "run" or "play" the game starts working then crashes and displays this message:
addEventListener: listener cannot be nil: nil stack traceback:
?: in function 'addeventListener'
game.lua63: in function'_listener' <-- i have given you game.lua:63 above.
Thanks
powerup:addEventListener( "touch", handlePowerTouch )
Here handlePowerTouch is nil as the function definition follows after this line.
Move your function definition in front of that line, then it should work.
Btw, is there any reason why you have so many global variables? You should use local variables wherever possible.
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.
I'm a beginner in Corona developping and i've to problem to identify the collision between 3 objects. In fact, this is my code :
The first object is my player :
player = display.newImageRect("ballon.png", 150,170 )
player.anchorX = 0.5
player.anchorY = 0.5
player.x = display.contentCenterX - 450
player.y = display.contentCenterY+250
physics.addBody(player, "static", {density=0.1, bounce=0.1, friction=0.1,})
player.myName="player"
screenGroup:insert(player)
The second object is created within a function :
function addWindSlow(self,event)
height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windslow = display.newImage( "wind-neg.png")
windslow.x = display.contentWidth
windslow.y = height
windslow.speed = 4
windslow.myName="windslow"
physics.addBody(windslow, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windslow) end
function addWindFast(self,event)
height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
windfast = display.newImage( "wind-pos.png")
windfast.x = display.contentWidth
windfast.y = height
windfast.speed = 4
windfast.myName="windfast"
physics.addBody(windfast, "static", {density = 0.1, bounce = 0, isSensor = false})
elements:insert(windfast) end
And finally, the thirth object is the same but called "addWindFast() with a different image"
So i would like to know once my player is on collision with the windslow, i need to do an action... and if it's the object windfast, another action. That's my code :
function onCollision( event )
if ( event.phase == "began" ) then
if event.other.windslow and event.other.isVisible==true then
print("windslow has been touch !")
end
end
end
I found something about the pre-collision, I'll use it once i got the information how to "identify" the object...I'll need to prevent the collision and avoid it if the player is going to touch the windfast or windslow.
We thank you for all dude !
It looks like windslow and windfast are defined outside of onCollision event handler so you should compare to those:
function onCollision( event )
if ( event.phase == "began" ) then
if event.other == windslow and event.other.isVisible==true then
print("windslow has been touch !")
end
if event.other == windfast and event.other.isVisible==true then
print("windfast has been touch !")
end
end
end -- (missing from your post but probably in your code)
The above assumes that you have registered for collision events on your player. You could also register a different collision handler on each wind:
windslow:addEventListener("collision", onCollisionSlow)
windfast:addEventListener("collision", onCollisionFast)
In that case the two collision handlers could just check if event.other == player. The advantage of that is you are separating the code for each one. You could of course do the same with your player handler:
function onCollisionSlow(other)
print('collided with slow')
end
function onCollisionFast(other)
print('collided with fast')
end
collisionHandlers = {
[windslow] = onCollisionSlow,
[windfast] = onCollisionFast,
}
function onCollision( event )
if ( event.phase == "began" ) then
collisionFunc = collisionHandlers[event.other]
if collisionFunc ~= nil and event.other.isVisible then
collisionFunc(event.other)
end
end
end
You could identify the other object with the myName property you've specified.
For example:
function onCollision( event )
if ( event.phase == "began" ) then
if (event.other.myName == "windslow" and event.other.isVisible==true) then
print("windslow has been touched!")
elseif (event.other.myName = "windfast" and event.other.isVisible==true) then
print("windfast has been touched!")
end
end
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.