Using Peach Pellen's spectacular and clear tutorial, I implimented scene changes with the director class. But when I attempt to go from my menu page (as Peach calls it) to the level1.lua of my game, get this error:
level1.lua:65: attempt to index global 'backButton' (a nil value)
I've tried the function and the button and the listener in a separate file, so I know those parts are correct. It was suggested that I post the offending code. Hopefully someone knowledgable will have a bit of insomnia this eve and want to tell me what I am doing wrong.
Sure would appreciate some help, I'm going crazy :) I apologize in advance for what is probably horrible looking code. I am brand new at this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | module(..., package.seeall) function new() local localGroup = display.newGroup() local background = display.newImage ("farmbg.jpg") localGroup:insert(background) ---------------------------------------------- -- The images -- ---------------------------------------------- local donkey = display.newImage ("donkey.png") donkey.x = math.random( 40, 280) donkey.y = math.random( 40, 440) localGroup:insert(donkey) local tail = display.newImage ("tail.png") tail.x = math.random( 20, 300) tail.y = math.random( 20, 460) localGroup:insert(tail) ---------------------------------------------- -- drag and show the back button ---------------------------------------------- function tail:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus(event.target) self.markX = self.x self.markY = self.y elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "ended" then local backButton = display.newImage ("playButton.jpg") backButton.x = 160 backButton.y = 440 localGroup:insert(backButton) end return true end local function pressBack(event) if event.phase == "ended" then director:changeScene ("menu") end end backButton:addEventListener( "touch", pressBack) tail:addEventListener( "touch", tail ) ---------------------------------------------------------------------------------------- return localGroup end |
First thing I would try is to add "local backButton" above line #25 (which would effectively forward reference it), and remove the "local" from line 40.
EDIT:
I would also try moving line 56 (the listener) to line 44 (right below the backButton is defined). Actually... if you move the listener from line 56 to line 44, you probably don't need to forward reference it.
Thanks Naomi. I get this error when I try putting the listener on line 44:
Runtime error
assertion failed!
stack traceback:
[C]: ?
[C]: in function 'assert'
?: in function 'getOrCreateTable'
?: in function 'addEventListener'
..._n94x89txqf21sm0000gn/T/TemporaryItems/25/level1.lua:44: in function <..._n94x89txqf21sm0000gn/T/TemporaryItems/25/level1.lua:25>
?: in function <?:215>
as Naomi has kind of seen it,
your backButton is local to the elseif statement block, so the moment you are out of that block (i.e. after end) backButton is no longer available. so as she has suggested, make the backButton local to the scope of the function by placing it about after line 25.
you get the error on line 44, because it cannot find the function pressBack which is defined from line 49 onwards, just remove local from line 40 and add local backButton just after line 25.
cheers,
?:)
I tried this, but maybe it's not what you meant (probably) cause it didn't work :) The back button still doesn't work and this way the dragging does not work. I also tried leaving the listener outside the if else but then I get the same runtime error...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | module(..., package.seeall) function new() local localGroup = display.newGroup() local background = display.newImage ("farmbg.jpg") localGroup:insert(background) ---------------------------------------------- -- The images -- ---------------------------------------------- local donkey = display.newImage ("donkey.png") donkey.x = math.random( 40, 280) donkey.y = math.random( 40, 440) localGroup:insert(donkey) local tail = display.newImage ("tail.png") tail.x = math.random( 20, 300) tail.y = math.random( 20, 460) localGroup:insert(tail) ---------------------------------------------- -- drag and show the back button ---------------------------------------------- function tail:touch( event ) backButton = display.newImage ("playButton.jpg") backButton.x = 160 backButton.y = 440 localGroup:insert(backButton) backButton:addEventListener( "touch", pressBack) if event.phase == "began" then display.getCurrentStage():setFocus(event.target) self.markX = self.x self.markY = self.y elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "ended" then end return true end local function pressBack(event) if event.phase == "ended" then director:changeScene ("menu") end end tail:addEventListener( "touch", tail ) ---------------------------------------------------------------------------------------- return localGroup end |
it must look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function tail:touch( event ) local backButton = display.newImage ("playButton.jpg") backButton.x = 160 backButton.y = 440 localGroup:insert(backButton) local function pressBack(event) if event.phase == "ended" then director:changeScene ("menu") end end backButton:addEventListener( "touch", pressBack) if event.phase == "began" then display.getCurrentStage():setFocus(event.target) self.markX = self.x self.markY = self.y elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "ended" then end return true end tail:addEventListener( "touch", tail ) |
Hey darkconsoles I pasted the code exactly as you wrote but still the button to return is not clickable! If I put the function, the button and the listener outside the ifelse and comment out if else, it works fine. I am obviously missing something, any thoughts? Thanks for the scope tutorial suggestion, I'm going over it again now.
@khincker, it's all about the order of things. I just noticed you also need to move the pressBack function above the line where it is called.
So try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | module(..., package.seeall) function new() local localGroup = display.newGroup() local background = display.newImage ("farmbg.jpg") localGroup:insert(background) ---------------------------------------------- -- The images ---------------------------------------------- local donkey = display.newImage ("donkey.png") donkey.x = math.random( 40, 280) donkey.y = math.random( 40, 440) localGroup:insert(donkey) local tail = display.newImage ("tail.png") tail.x = math.random( 20, 300) tail.y = math.random( 20, 460) localGroup:insert(tail) local function pressBack(event) if event.phase == "ended" then director:changeScene ("menu") end end ---------------------------------------------- -- drag and show the back button ---------------------------------------------- function tail:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus(event.target) self.markX = self.x self.markY = self.y elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y elseif event.phase == "ended" then local backButton = display.newImage ("playButton.jpg") backButton.x = 160 backButton.y = 440 localGroup:insert(backButton) backButton:addEventListener( "touch", pressBack) end return true end tail:addEventListener( "touch", tail ) ---------------------------------------------------------------------------------------- return localGroup end |
BTW, I didn't test it. And if the above doesn't give you any runtime error, but if it doesn't run like you want it to, then you've got logic problem (i.e., functions not working the way you envision it to work). That's something you'd have to solve. If you can't, you probably should start a new thread asking for help in figuring out how to write a function you need.
Solved. Thank you all SO much for your help. One huge problem was a set focus issue, where much of the code was actually working the way it was supposed to (thanks to your help) but I couldn't make the button active because focus was elsewhere. I am very much a baby at this, but I will get better, and hope one day to be able to help others as you all are helping those of us new to Corona. What a great community!
Hey, @khincker, glad to hear it worked!!
I totally agree that we've got a really great community here. Let's keep helping each other. I suggest subscribing to forum, and read up on different posts whenever you can. I found many gems by simply coming across it, and I found many answers to my questions before I posted them.
Naomi
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Timer/Stopwatch Not Accurate/Precise - Slow
www.Apple.com is down right now due to iPhone 4S announcement event ?
Overhead on the Android package
A listener for each display object or...?
TableView 1.8.1 Problem
Streaming from Vimeo
Would it be possible to create a video chat app with Corona?
Game runs fine .268, crashes the simulator in .484
Apple phasing out UDID - how does this affect Corona?