Why is my object.index not working?

So, here’s what I’m trying to do: I’ve got little images drawn on a map for level progression through our game, using circ.index and am expecting it behave like circ.x, circ.y where each object has it’s own index, but what i’m seeing is different. Within the for loop, it prints each index, as i expect it to, however whenever i click on circles, the print only provides the last index generated.

Where am I going wrong?

for i=1,#levelTable do -- print(i.. " " .. levelTable[i]["xCoord"].. "," .. levelTable[i]["yCoord"]) local xCoord, yCoord = levelTable[i]["xCoord"],levelTable[i]["yCoord"] local config = levelTable[i]["index"] -- local selection = --print( xCoord ..",".. yCoord ) circ = display.newImage( "img/character.png" ) circ:scale(.9,.9) circ.x = xCoord circ.y = yCoord+580 circ.index = config print( circ.index) -- circ.id = levelTable[i]["selection"] levelSelectors:insert( circ ) end

Try changing line 40

circ = display.newImage( "img/character.png" )

to be

local circ = display.newImage( “img/character.png” )

Without the local keyword, circ is a global variable which gets overwritten in each iteration of the for loop.

How then, do i pass the object to the touch event for detection? i tried

 function levelSelectors:touch(event,circ) local index = circ.index print( "touching Circle " .. index) return true end

but it gives me an illegal global error. Thanks again you guys are a ton of help to my newbish self!

Hmm, I’m assuming levelSelectors is a displayGroup, and you’re inserting the circ objects which is the actual button that user will touch to select the level?

If that’s the case you need to give each circ object it’s own touch listener. So your code would look something like this:

local circ = display.newImage( "img/character.png" ) circ:scale(.9,.9) circ.x = xCoord circ.y = yCoord+580 circ.index = config circ.touch = function(self, event) print(self.index, event) return true end circ:addEventListener("touch")

By the way, there are TONS of ways to add event listeners to display objects (in case you’re not familiar with the syntax I used above). I’ll admit I still don’t know them all yet :smiley:

Ok, that seems to be moving me in the right direction :smiley:

Two some other questions, both all sort of related:

First, the print statement is basically just the “proof of concept” it’s working, correct?

Secondly, assuming I’m right on point 1, any additional code i’d need would go where that print statement is now, correct? (likely in a function, if i’m smart…which is sometimes questionable)

Yes, you are correct on both points. There may be a better method, but I like to write my touch listeners outside of the scene:create() function.

So my code would look something like:

local scene = composer.newScene() --declare some variables here local function circTouch(self, event) local circ = self --this is the circ object that the user touched --code to handle touch return true end function scene:create(event) --some code here for i=1,#levelTable do --blah blah local circ = display.newImage( "img/character.png" ) circ:scale(.9,.9) circ.x = xCoord circ.y = yCoord+580 circ.index = config circ.touch = circTouch circ:addEventListener("touch") --blah blah blah end --some more code end --rest of the scene

Hope that makes sense!

I had been as well, but I think it was an effort to get circ local

Hey, sorry to come back to this Vince, but on line 27 wouldnt that be circ.touch = circTouch(self,event) ?

I’m fairly certain that if you include the parentheses lua interprets that as you wanting to execute circTouch right there in the for loop, which will set circ.touch = true (because circTouch returns true).

By not including the parentheses you’re just setting it equal to the reference for that function so that it can be called later on.

Are you experiencing an error?

I actually hadn’t tried it, it just looked different than I was expecting

edit: Nope, everything is working as I expect it to, awesome again, thanks for the help

Try changing line 40

circ = display.newImage( "img/character.png" )

to be

local circ = display.newImage( “img/character.png” )

Without the local keyword, circ is a global variable which gets overwritten in each iteration of the for loop.

How then, do i pass the object to the touch event for detection? i tried

 function levelSelectors:touch(event,circ) local index = circ.index print( "touching Circle " .. index) return true end

but it gives me an illegal global error. Thanks again you guys are a ton of help to my newbish self!

Hmm, I’m assuming levelSelectors is a displayGroup, and you’re inserting the circ objects which is the actual button that user will touch to select the level?

If that’s the case you need to give each circ object it’s own touch listener. So your code would look something like this:

local circ = display.newImage( "img/character.png" ) circ:scale(.9,.9) circ.x = xCoord circ.y = yCoord+580 circ.index = config circ.touch = function(self, event) print(self.index, event) return true end circ:addEventListener("touch")

By the way, there are TONS of ways to add event listeners to display objects (in case you’re not familiar with the syntax I used above). I’ll admit I still don’t know them all yet :smiley:

Ok, that seems to be moving me in the right direction :smiley:

Two some other questions, both all sort of related:

First, the print statement is basically just the “proof of concept” it’s working, correct?

Secondly, assuming I’m right on point 1, any additional code i’d need would go where that print statement is now, correct? (likely in a function, if i’m smart…which is sometimes questionable)

Yes, you are correct on both points. There may be a better method, but I like to write my touch listeners outside of the scene:create() function.

So my code would look something like:

local scene = composer.newScene() --declare some variables here local function circTouch(self, event) local circ = self --this is the circ object that the user touched --code to handle touch return true end function scene:create(event) --some code here for i=1,#levelTable do --blah blah local circ = display.newImage( "img/character.png" ) circ:scale(.9,.9) circ.x = xCoord circ.y = yCoord+580 circ.index = config circ.touch = circTouch circ:addEventListener("touch") --blah blah blah end --some more code end --rest of the scene

Hope that makes sense!

I had been as well, but I think it was an effort to get circ local

Hey, sorry to come back to this Vince, but on line 27 wouldnt that be circ.touch = circTouch(self,event) ?

I’m fairly certain that if you include the parentheses lua interprets that as you wanting to execute circTouch right there in the for loop, which will set circ.touch = true (because circTouch returns true).

By not including the parentheses you’re just setting it equal to the reference for that function so that it can be called later on.

Are you experiencing an error?

I actually hadn’t tried it, it just looked different than I was expecting

edit: Nope, everything is working as I expect it to, awesome again, thanks for the help