Page 2 of 3
Re: Moving the enemies
Posted: Sat Oct 01, 2011 4:09 pm
by cvirus
Thanks for this explanation and for the code, i didn´t try it yet, i'm trying to understand it first, but i don't understand this piece of code:
Code: Select all
for i = 1, #ammo do
ammo[i].x = ammo[i].x + (dt * 300)
end
If you declare ammo = {}, isn't this an empty table? how can i have as enemys as i want?
and this one too
Code: Select all
ammo[#ammo + 1] = { <-- this is like a counter to put in table ammo +1?
x = playerX, <-- put the bullet in the same coordinates as the player?
y = playerY
I'm sorry for this silly questions but i' a n00b to game programming.
Thanks
Re: Moving the enemies
Posted: Sat Oct 01, 2011 4:28 pm
by tentus
cvirus wrote:
Code: Select all
for i = 1, #ammo do
ammo[i].x = ammo[i].x + (dt * 300)
end
If you declare ammo = {}, isn't this an empty table? how can i have as enemys as i want?
Correct, it's an empty table, or in other words, a table with 0 entries. So that loop will happen a total of 0 times. If we had, say, 5 entries in the table, the loop would happen 5 times. If we had 500 entries, it would happen 500 times. The beauty of #ammo is that we can stick it in and then not worry about it.
To have as many enemies as you want, just extend the concepts we're using for ammo to the enemies.
cvirus wrote:
Code: Select all
ammo[#ammo + 1] = { <-- this is like a counter to put in table ammo +1?
Correct again. So if we have 0 entries in ammo so far, then 0+1=1, so it's like saying
ammo[1] = {
cvirus wrote:
Code: Select all
x = playerX, <-- put the bullet in the same coordinates as the player?
Yes, though in practice I would recommend doing something like
x = playerX + 10
Re: Moving the enemies
Posted: Sat Oct 01, 2011 4:54 pm
by cvirus
So to have a slot of bullets in ammo table a should do something like this to work:
Code: Select all
ammo = {30} <------ or 50 or 300 wrigth?
This way i have 30 shots, but this way when i reach the 30 bullets don't i have to do this?
Does this work?
#ammo or ammo[1] is the same thing? Or #ammo takes all values inside a table?
I get it the part of x = playerX + 10, this way the bullet will be shot in the middle of players image wright?
Thanks again.
Re: Moving the enemies
Posted: Sat Oct 01, 2011 6:01 pm
by tentus
cvirus wrote:So to have a slot of bullets in ammo table a should do something like this to work:
Code: Select all
ammo = {30} <------ or 50 or 300 wrigth?
No no no. That would make #ammo == 1. #ammo is
how many entries are in ammo. It's like this:
Code: Select all
ammo = {} -- 0 entries
ammo = { -- 1 entry
"string"
}
ammo = { -- 2 entries
"string",
1
}
Taking it a step further, entries don't have to be strings or numbers. They can also be tables (each table counts as one entry, regardless of how many entries it has). Like this:
Code: Select all
ammo = {} -- 0 entries
ammo = { -- 1 entry
bullet = {}
}
ammo = { -- 2 entries
bullet = {
"string"
},
bullet = {
1,
2
}
}
The code that I posted (in love.keypressed) inserts a table into the ammo table. The subtable contains the values for each bullet: in this case, just an X/Y coordinates. The for loops use those subtable values for updating: ammo
.x refers to the X value of whichever subtable you are currently on in the for loop.
Re: Moving the enemies
Posted: Sat Oct 01, 2011 6:42 pm
by cvirus
Ok, i think i got it, this piece of code:
is the part that is responsable for increase the number of items inside the table wright?
So I have to do the same at my var maxEnemies i think.
Here is my love file to see what i have done so far.
Re: Moving the enemies
Posted: Sat Oct 01, 2011 7:02 pm
by tentus
cvirus wrote:
is the part that is responsable for increase the number of items inside the table wright?
Yes, it's part of it.
cvirus wrote:
So I have to do the same at my var maxEnemies i think.
Not... quite. Replace #maxEnemies with #enemy, and pass a parameter into posEnemy to control how many enemies you want to create and position. Here, attached is what I mean. I also adjusted a few other little things.
Re: Moving the enemies
Posted: Thu Nov 03, 2011 3:45 pm
by cvirus
Hello coders, i have a one more question related to this piece of code:
Code: Select all
for i = 1, #ammo do
ammo[i].x = ammo[i].x + (dt * 300)
end
I have this code and to clear the items inside so the table doesn t get to big and slow i must clear those items as soon as they get off the screen, right?
so to do that i must:
Code: Select all
if bullet > 480 then
bullet.visible = false
table.remove(ammo[#ammo])
end
Is this way that works?
Thanks
Re: Moving the enemies
Posted: Thu Nov 03, 2011 10:35 pm
by Robin
or in this case (when you want to remove the last) just:
Whether it really is correct depends on the rest of the code, though.
Re: Moving the enemies
Posted: Fri Nov 04, 2011 8:17 pm
by cvirus
well, i did this:
instead of ammo i changed to shot, and this works if only i shot once at a time and let the bullet get off the screen, but if i shot more times in a row then it gives an error saying.
attempt to index field '?' (a nil value)
Code: Select all
for i = 1, #shot do
shot[i].y = shot[i].y + 2 <---- here is when it says that message
if shot[i].y > 480 then
shot[i].visible = false
table.remove(shot)
end
end
This works fine without the table.remove
Re: Moving the enemies
Posted: Fri Nov 04, 2011 9:08 pm
by Robin
Removing items of a list in a loop causes problems (plus you always remove the last item, instead of the current). Here's how it's usually done:
Code: Select all
for i = 1, #shot do
shot[i].y = shot[i].y + 2
if shot[i].y > 480 then
shot[i].visible = false
end
end
for i = #shot, 1, -1 do --traverse backwards to remove items without everything going to hell
if not shot[i].visible then
table.remove(shot, i)
end
end
In this case you might replace it by a single backwards loop. Anyway, this is a tricky thing you always need to keep in minds whenever you remove things in a loop.