I'm Lost

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 :slight_smile: I apologize in advance for what is probably horrible looking code. I am brand new at this.

[lua]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[/lua] [import]uid: 96383 topic_id: 16669 reply_id: 316669[/import]

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. [import]uid: 67217 topic_id: 16669 reply_id: 62279[/import]

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>
?: in function <?:215>
[import]uid: 96383 topic_id: 16669 reply_id: 62282[/import] </…_n94x89txqf21sm0000gn>

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,

?:slight_smile: [import]uid: 3826 topic_id: 16669 reply_id: 62284[/import]

I tried this, but maybe it’s not what you meant (probably) cause it didn’t work :slight_smile: 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…

[lua]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[/lua] [import]uid: 96383 topic_id: 16669 reply_id: 62290[/import]

it must look like this:
[lua]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 ) [/lua]

when you create local object inside of a function you must assign proper function to it inside its block, so that with your button you also creates corresponding function
its all about function scopes, you might want to read tutorials about it, its in blog section of the ansca site i believe

and personally i dont understand why you need to use
[lua]function tail:touch[/lua]
function like that, you can do it all in simple local function, if you dont intend to use modules or classes, you better do all things local, it saves memory and prevents leaks [import]uid: 16142 topic_id: 16669 reply_id: 62317[/import]

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. [import]uid: 96383 topic_id: 16669 reply_id: 62353[/import]

@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:

[lua]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[/lua]
[import]uid: 67217 topic_id: 16669 reply_id: 62398[/import]

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. [import]uid: 67217 topic_id: 16669 reply_id: 62404[/import]

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! [import]uid: 96383 topic_id: 16669 reply_id: 62422[/import]

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 [import]uid: 67217 topic_id: 16669 reply_id: 62429[/import]