Log In  


I have created a marching line of invaders that move and animate left and right, dropping down a few rows each time they reach the opposite edge of the screen, It's working great!

So then I added a function that will fire a shot from one of the aliens but when I iterate through the table I always get the last one in the table for my x, y starting position of the invaders shot.

Is there a way to iterate through a table picking a random entry to generate some variables from? I have tried several things but can't seem to figure out how to do this as we only have: add, all, count, del, foreach and pairs.

Thanks,

Brian



A useful function is rnd(), for 'random'.

If the parameter supplied to rnd is a 1-indexed table, it will return a random entry from 1 to #table.

In your case, replace the for loop with:

if not invader_fired_a then
  local i_f_a = rnd(inv_r_a)
  inv_a_sx = i_f_a.x
  etc.
  etc.
end

Thanks, I will try that.


1

That totally did it, I could not figure out how to integrate rnd with a table iteration to save my life - Thanks again!

Brian



[Please log in to post a comment]