#1

Hey everyone!

Im making a space inavaders clone for my A level NEA project.
I have got to the point where I want to spawn waves of aliens, I know i have to use a table for this but I am unsure how, is there a resource I can learn from in order to be able to do this?

Following on from this, i would be unsure as to how to detect when the waves hits the side borders of the screen.

Any help would be much appreciated!
Thanks

0 Likes

#2

Using a table is a really good idea, we can probably use something like a 2d array in order to hold our aliens.

``````local aliens = { -- save Ids of the aliens here
{ }, -- row one
{ }, -- row two
-- etc
}
``````

Within the rows themselves, I would store the aliens in order of positions where index 1 = the left most alien and index [x] (I’ll use 10 for simplicity) represents the right most alien.

Since the goal with the aliens is to move them all together for the most part, you should be able to simply check the left and right most alien x position.

``````local leftPosition = 5
local rightPosition = 1075
-- comment: alien size needs to be incorporated into the left and right positions as well

for _,row in ipairs(aliens) do
if #row > 0 then -- check to make sure that there are items in the table
if go.get_position(row[1]).x =< leftPosition then
-- move down and start moving right
elseif go.get_position(row[#row]).x >= rightPosition then
-- move down and start moving left
end
end
end
``````

Use of table.remove() can be used to shift down the positions so that we are only looking at the left and right most aliens

Here’s another forum post regarding tiles, since this is somewhat similar since it’s talking about coordinates

1 Like

#3

Hello!

Thank you so much for this post its really been informative and helpful,

In order to learn how to properly use this table, is there any resources you recommned, or should i just play around and see what i can do?

0 Likes

#4

Here are some resources from Programing in Lua

Basic Tables: https://www.lua.org/pil/2.5.html

Table Constructors: https://www.lua.org/pil/3.6.html

Using Tables: https://www.lua.org/pil/11.1.html (The entire chapter 11)

If you haven’t already, giving programing in lua a read is definitely something you should do! We have a lot of useful techniques there.

1 Like