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

P#88796 2021-03-10 17:33

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
P#88797 2021-03-10 17:58

Thanks, I will try that.

P#88801 2021-03-10 18:32
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

P#88802 2021-03-10 18:38

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 07:27:40 | 0.006s | Q:12