Spawn objects in loop with tap-listeners

Hi Nail,

I gave that a go, when one of the ‘thingies’ is clicked in the simulator I get a runtime error: attempt to index local ‘event’ (a nil value).  Could it be that the listener isn’t passing the ‘event’ completely?

James.

not sure why, but I never use “tap”, I use “touch” as the event listener. Then in the listener function do like this…

if event.phase == "began then

elseif event.phase == “moved” then

elseif event.phase == “ended” then

end

This allows for different things to happen during different phases of the event.

Sorry, I’m just not that familiar with “tap”

Nail

You want to “return true” at the end of your event listener also.

function gc:positionTapped(event)

if event.phase == “began” then

 

elseif event.phase == “ended” then

  utils:log(sceneName, “positionSelected()”);

 

print("event.target.id == ", event.target.id);

end

return true

end

Hi Again,

Still no joy.  The posiitonTapped function now looks like this:

[lua]

function gc:positionTapped(event)

   utils:log(sceneName, “positionSelected()”);

   if event.phase == “began” then

   elseif event.phase ==“moved” then

   elseif event.phase == “ended” then

      print(self.id);

   end 

   

   return true;

   

end

[/lua]

When I click a thingy, the error is: Runtime error /Users/jnewburrie/code/gameCore.lua:90: attempt to index local ‘event’ (a nil value)

line 90 is:  “if event.phase == “began” then”

I don’t use the 3rd step variable in my  “for” iterations either, but by using “1” as you do, I don’t see why it won’t work correctly.

for i = 1, 6, 1

I just use:

for i = 1, 6 and skip the 3rd step variable

Nail

I just tried removing the ‘step’ variable. The ‘draw’ loop still works correctly, but the event still isn’t passing…

try adding local to thingy table

local thingy = {}

tried that, no joy. :frowning:

you are inserting gc.gameDG into the thingy[i] table.

comment out the line and see what happens.

–table.insert(thingy[i], gc.gameDG

still no joy :frowning:

arrrgh!

have you replaced “tap” with “touch” when adding the event listener?

it could be a scope issue.

try adding a function in the gc module with the spawn loop and call it from the module you are currently spawing the thingy’s from

you’ve got to replace the “:” with a “.” in the listener function

change:  function gc:positionTapped(event)

to:  function gc.positionTapped(event)

change it when you add the event listener also.

and give it a try

this works, your code is close.

I don’t know why the lua tags create 1 long line of code so I just posted it raw.

main.lua-----

local gc = require(“gc”)

display.setStatusBar( display.HiddenStatusBar )

local positionSpacing = 30

local thingy = {}

local gcGroup = display.newGroup()  --add a display group so you can remove the objects later

local function spawnThingys()
    for i=1,6 do
        
        thingy[i] = display.newCircle(0, 0, 10)
        
        thingy[i].x = (positionSpacing * i);
        
        thingy[i].y = 100;
        
        thingy[i]:setFillColor(0, 0, 255)
        
        thingy[i].name = (“thingy”…i);
        
        thingy[i].id = i;
        
        gcGroup:insert(thingy[i])
        
        print(thingy[i].name…" “…thingy[i].x…” index “…thingy[i].id); --for debug
        
        – insert thingy[i] into gc.gameDG so you can reference it I guess, you had it backwards
        
        table.insert(gc.gameDG, thingy[i])
        
        --here’s how you call the variable from the gc.gameDG table if you want to from that module
        
        print(“gc.gameDG.thingy[”…i…”].id == ", gc.gameDG[i].id)
        
        thingy[i]:addEventListener(“touch”, gc.positionTapped);
    end     
    return gc.gameDG
end

spawnThingys()

gc.lua----

local gc = {}
gc.gameDG = {}

–function gc:positionTapped(event)
    
 function gc.positionTapped(event)   
    
    if event.phase == “began” then
    elseif event.phase == “ended” then
        
        --utils:log(sceneName, “positionSelected()”);
        --print(self.id);
        print("event.target.id == ", event.target.id)

    end
    return true
    
end

Hope this helps

Nail

Hey Nail,

You found the answer a post-ago!

I changed the “:” with a “.” in the listener function:

function gc:positionTapped(event)

 

to:  

 

function gc.positionTapped(event)

 

Thanks for your help :slight_smile:

ya, I know.  I didn’t see the trouble at first because I know you can access gc:createThingy from another module, still not sure why the event listener had to have a “.” though.   Just the way it is I guess.

I posted the code because I believe you were inserting your table backwards and then the code goes on to show how to call the new inserted table variable.

It was a fun debugging session, I learned a couple of things, glad I could help.

GL

Nail