Hi!
Welcome to the forums!
The line:
does the following:
It iterates over ALL values in the bullet table (the table you create in line two of bullet.lua).
In this table, you then put values like width, height, speed etc. And then you also put function values into the table (3, to be exact: bullet.spawn, bullet.draw, bullet.move). So now you have three function values in the table "bullet", which is no problem so far. But then when you iterate over the bullets: What you're trying to do is move every bullet you've created - which fails because there's not only bullets in the table, but also functions.
If you've ever written code in any object-oriented language, you can think of it like this: The way you use the table "bullets" makes it something like a class. But then you also insert the objects into that class ( using the call table.insert(bullet, ...) ) and so now bullet is a mix of class and instance (this comparison only holds to some extend. To make bullet a real "class", some things are still missing - but I hope this works well as an example).
So, how to fix it:
The easiest way I can think of is this:
Create a new table "bulletList" or similar and insert the objects into that list. At the same time, leave all the "class values", the functions etc in the old "bullet" table. I'll walk you through it:
- Under bullet = {}, add another line:
bulletsList = {}
- In line 9, change the "bullet" to "bulletList" (so you're adding the new bullet to the "bulletList" table instead of adding it to the "bullet" table)
- In line 13 and in line 20, change the "pairs(bullet)" to "pairs(bulletList)" since we want to draw the instances of bullets, not the class.
Couple of other remarks:
- When all of this is done, the game won't run yet since you seem to be doing the same mistake in other files as well (enemy.lua is the next one that pops up). This should be solvable by using the same trick: add enemyList = {} to enemy.lua and insert enemies into that list.
- You don't need the functions DRAW_BULLET and UPDATE_BULLET. Instead, you can simply call bullet.draw() and bullet.move() in the other files, since "bullet" is a global variable (in Lua, everything that isn't specifically declared as local is automatically global. Ugly, but sadly true.)
- It's considered good practice to make everything you can local. Making the bullet table local would require a bit of code restructuring (and wouldn't do you much good since you need it as a global in main.lua anyways) but bulletList could be made local - since you only need this table in bullet.lua. So just prepend the line "bulletList = {}" with "local", to get "local bulletList = {}".
Also keep this in mind when adding more code - make sure to make everything local that doesn't need to be global. Otherwise you can run into nasty stuff like functions modifying each other's variables.
Hope this helps!
P.S.
I've never seen spoiler tags here, I don't think they've been added to the forums. So no talking about season 3 or 4 of Game of Thrones! I haven't watched them yet...