Delete table display Instance without Transition.to

Hi all

I am practicing tables and iterations and was wondering the following:

How can I access each spawned instance of the ninja object, and specifically delete from game and the table, without the transition.to method?

How do you use an eventListener where each ninja instance is passed through the “self” parameter?

I hope my questions make sense to someone, I don’t know how to explain it better.

Thanks in advance!

K.

function kill(self)     display.remove(self) end     local ninjas = {} local idx = 0 for i = 1, 4 do   idx = idx + 1   local ninja = display.newImageRect(                 "Idle.png", 45,70     )   ninja.x = 100   ninja.y = math.random(display.actualContentHeight)   ninjas[idx] = ninja                                     ninjas[idx].idx = idx                                 --  if ninja.y \> 700 then --        kill() --      end              transition.to(ninja,                 {delay = math.random(5000,9000),                   time = 3000,                   x= display.actualContentWidth-200,                 onComplete= kill})

      

Hi @kdouglasgames,

I assume that your goal is to spawn ninjas, but if the random “y” value is > 700, then you want to skip that spawn. Correct?

If so, there’s no real purpose in even creating the ninja, assuming he’s just going to be killed (in code) before he ever does anything. :slight_smile:

Basically, I would pre-check where the ninja might be spawned and, if it’s > 700, then just skip that iteration entirely.

So basically something like this:

[lua]

for i = 1, 4 do

   idx = idx + 1

   local yRand = math.random( display.actualContentHeight )

   if yRand <= 700 then

      local ninja = display.newImageRect( “Idle.png”, 45,70 )

      ninja.x = 100

      ninja.y = yRand

      ninjas[idx] = ninja

      ninjas[idx].idx = idx                                 

   end

end

[/lua]

Hope this helps,

Brent

P.S. - If you’re not already doing so, remember to seed the random generator, somewhere near the top of your code, just once. This will ensure that the ninjas are actually randomly spawned. See here:

https://docs.coronalabs.com/api/library/math/randomseed.html

That did help, Thanks!!!

-Kned

Hi @kdouglasgames,

I assume that your goal is to spawn ninjas, but if the random “y” value is > 700, then you want to skip that spawn. Correct?

If so, there’s no real purpose in even creating the ninja, assuming he’s just going to be killed (in code) before he ever does anything. :slight_smile:

Basically, I would pre-check where the ninja might be spawned and, if it’s > 700, then just skip that iteration entirely.

So basically something like this:

[lua]

for i = 1, 4 do

   idx = idx + 1

   local yRand = math.random( display.actualContentHeight )

   if yRand <= 700 then

      local ninja = display.newImageRect( “Idle.png”, 45,70 )

      ninja.x = 100

      ninja.y = yRand

      ninjas[idx] = ninja

      ninjas[idx].idx = idx                                 

   end

end

[/lua]

Hope this helps,

Brent

P.S. - If you’re not already doing so, remember to seed the random generator, somewhere near the top of your code, just once. This will ensure that the ninjas are actually randomly spawned. See here:

https://docs.coronalabs.com/api/library/math/randomseed.html

That did help, Thanks!!!

-Kned