Move the function “movelevel1” out of scene:create() to the main chunk. In other words:
local function movelevel1(event) ... end function scene:create( event ) ... end
Rob
Move the function “movelevel1” out of scene:create() to the main chunk. In other words:
local function movelevel1(event) ... end function scene:create( event ) ... end
Rob
If I moved it, then:

MyGridlvl1 is in function code.
You need to learn about “Scope”. Please read this tutorial:
http://coronalabs.com/blog/2011/09/21/tutorial-scopes-for-functions/
and watch this video:
https://www.youtube.com/watch?v=b12YEOIry60
Rob
Thanks… but I don’t understand anything.
Can you explain me how it’s working? Example my function.
Thanks a lot, I’m noob.
For me to explain this to you, I will have to basically write, word for word what was in the tutorial I posted above. Here is another video that might help:
http://www.youtube.com/watch?v=2ATlcGP2zMY
Watch this, go back re-read the tutorial and try to get a grasp on what scope means. Simply put, depending on where you create/delcare things determines where they are visible. Corona is also a one pass system, which means that you can use something before you declare it. That is I can’t call my function before I declare it:
doSomething()
local function doSomething()
print(“Hello World”)
end
results in an error. When Lua tries to run doSomething(), it is nil. It has not been defined yet, and you get that error. On the other hand:
local function doSomething()
print(“Hello World”)
end
doSomething()
Will result in “Hello World” being printed because now when you try and run “doSomething()”, it’s been defined and Lua knows what to do with it.
Rob
OK. I must pre-declare myGridlvl1 or scene:create?
If myGridlvl1 has to be after the first time you use it, then you would pre-declare it. The easiest thing is to move function toward the top before you use them.
Rob
My code:
function scene:create( event ) ... end local function movelevel1(event) ... end function scene:show( event ) ... end
In function scene:show is
Runtime:addEventListener( "enterFrame", movelevel1 )
In function scene:create is
local myGridlvl1 = display.newRect (160, 100, 1000, 22) myGridlvl1:setFillColor (0,0,0) local myGrid2lvl1 = display.newRect (160, 350, 1000, 22) myGrid2lvl1:setFillColor (0,0,0)
And there’s still the same error. I tried to pre-declare myGridlvl1 and myGrid2lvl1 before function movelevel1 and it’s not working.
At the very top of your scene, do:
local myGridlvl1
local myGrid2lvl1
Then in create scene, leave the “local” off.
Rob
Another:
My Collision event isn’t working. Here is code:
local myCircle1 function scene:create( event ) myCircle1 = display.newImage( “circle.png”, 90, 225, 35 ) myCircle1.ID = “Circle1” myCircle1.collision = Collision myGridlvl1 = display.newRect (160, 100, 1000, 22) myGrid2lvl1 = display.newRect (160, 350, 1000, 22) myGridlvl1.ID = “Crash1” myGrid2lvl1.ID = “Crash1” function endGamelevel1() local endtext1 = display.newText (“You lost!”, 160, 50, font, 32) endtext1:setFillColor (0,0,0) timer.cancel(timer1) Runtime:removeEventListener(“enterFrame”, movelevel1) button1:removeEventListener(“tap”, tap) myCircle1:removeEventListener(“collision”, myCircle1) local function restart (event) composer.gotoScene( “restart1” ) end local restartbutton1 = display.newImage(“restart.png”, 150, 200, 100, 100) restartbutton1:addEventListener( “tap”, restart ) end end local function Collision(self, event) if (event.phase == “began”) then if (self.ID == “Circle1” and event.other.ID == “Crash1”) then endGamelevel1 () end end end function scene:show( event ) myCircle1:addEventListener(“collision”, myCircle1) end
Composer.goto don’t working. Code:
function scene:create( event ) local function level2 (event) composer.gotoScene(“tolvl2”) end end function scene:show( event ) local timer1 = timer.performWithDelay( 5000, level2) end
Thanks.
PS. Rob, you’re great 
You really need to learn more about scope. Consider this block of your code:
function scene:create( event ) local function level2 (event) composer.gotoScene("tolvl2") end end function scene:show( event ) local timer1 = timer.performWithDelay( 5000, level2) end
Since you are trying to call “level2”, level2 has to be visible to the scene:show() function. But the level2 function is inside scene:create() and declared local. Therefore it’s only visible inside of scene:create() and scene:show() cannot access it. You need to do something like this:
local function level2 (event) composer.gotoScene("tolvl2") end function scene:create( event ) -- other scene create things... end function scene:show( event ) local timer1 = timer.performWithDelay( 5000, level2) end
By moving the function outside of the scene:create() function it becomes visible to the entire module.
local myGridlvl1 local myGrid2lvl1 local myCircle1 local function Collision(self, event) if (event.phase == "began") then if (self.ID == "Circle1" and event.other.ID == "Crash1") then endGamelevel1 () end end end function endGamelevel1() \<code\> end
Where I must put this code?
myCircle1.ID = "Circle1" myGridlvl1.ID = "Crash1" myGrid2lvl1.ID = "Crash1"
If it’s in main chunk is error.
After you create it.
myCircle1 = display.newCircle(…)
myCircle1.ID = “Circle1”
When Circle1 touch Crash1 there’s nothing happen.
Code:
function endGamelevel1() local endtext1 = display.newText ("You lost!", 160, 50, font, 32) endtext1:setFillColor (0,0,0) timer.cancel(timer1) Runtime:removeEventListener("enterFrame", movelevel1) button1:removeEventListener("tap", tap) myCircle1:removeEventListener("collision", myCircle1) local function restart (event) composer.gotoScene( "restart1" ) end local restartbutton1 = display.newImage("restart.png", 150, 200, 100, 100) restartbutton1:addEventListener( "tap", restart ) end local function Collision(self, event) if (event.phase == "began") then if (self.ID == "Circle1" and event.other.ID == "Crash1") then endGamelevel1 () end end end function scene:create( event ) local physics = require "physics" physics.setGravity( 0, 0 ) myCircle1 = display.newImage( "circle.png", 90, 225, 35 ) myCircle1.ID = "Circle1" physics.addBody( myCircle1, {radius=38.5} ) myCircle1.collision = Collision myGridlvl1 = display.newRect (160, 100, 1000, 22) myGridlvl1:setFillColor (0,0,0) myGrid2lvl1 = display.newRect (160, 350, 1000, 22) myGrid2lvl1:setFillColor (0,0,0) myGridlvl1.ID = "Crash1" myGrid2lvl1.ID = "Crash1" physics.addBody( myGridlvl1, "static") physics.addBody( myGrid2lvl1, "static") end
What is wrong?
print out the values of self.ID and event.other.ID and see if it’s what you expect.
myCircle1.ID = “Circle1”
myGridlvl1.ID = “Crash1”
myGrid2lvl1.ID = “Crash1”
When myCircle1 touched myGridlvl1 or myGrid2lvl1 then nothing happens. After touch It should be actived endGamelevel1 ().
Let me re-ask the question. Inside the collision handing function, what are the values of “self.ID” and “event.other.ID”. You need to make sure self is what you think it is and what event.other.ID is.
Rob
In my other project the same code is working, maybe because I don’t use scene:create or something.
EDIT: In my opinion everything is OK. Do you know how I can do that in another way?
Do you know how I can do that in another way?
Put in print statements. Print out the values of self.ID and event.other.ID. Post those results here so I can see what’s going on.
Rob