Removing from table

Maybe i want to sleep, but still i have a probably stupid question)
i’m trying to spawn a 5 “fires” and one of them is transitioning to x = 300
and i’m trying to remove it upon passing x=200, but it doesnt work, so please help me, what i done wrong?

[lua]local fires = {}

for i=1,5 do

local fire = display.newRect(0,0,20,10)
fire.x = 10
fires[i] = fire
fire.y = math.random(10,300)
transition.to(fires[1],{time=3000, x = 300})
end

local function collect()

for i=1, #fires do
if fires[i].x > 200 then
fires[i]:removeSelf()
end
end
end

Runtime:addEventListener(“enterFrame”, collect)[/lua]

its kinda work, but terminal is cursing me with errors)

any help is good) [import]uid: 16142 topic_id: 16705 reply_id: 316705[/import]

Hey @ darkconsoles, I edited yours a bit. And, you probably want to shoot the fire at random time, remove the enterFrame event listener after all 5 fires are removed.

EDIT: actually, instead of doing the loop, you may look into making the object transparent when it hits x = 200, and then, on completion of the transition, remove self instead of using the collect function
[lua]local fires = {}
for i=1,5 do

local fire = display.newRect(0,0,20,10)
fires[i] = fire
fires[i].x = 10;
fires[i].y = math.random(10,300)
transition.to(fires[i],{time=3000, x = 300})
end

local function collect()

– you probably want to go backward (i.e., remove from the max number of fires until #fires reaches 0).
– I just don’t remember the syntax off the top of my head

for i=1, #fires do
if fires[i].x > 200 then
fires[i]:removeSelf()
end
end
end

Runtime:addEventListener(“enterFrame”, collect) [/lua]

Naomi
[import]uid: 67217 topic_id: 16705 reply_id: 62531[/import]

This should work:

[code]
local fires = {}

for i=1,5 do
fires[i] = display.newRect(0,0,20,10)
fires[i].x = 10
fires[i].y = math.random(10,300)
end
transition.to(fires[1],{time=3000, x = 300})

local function collect()
for i=1, #fires do
if fires[i].x > 200 then
fires[i]:removeSelf()
table.remove(fires, i)
break
end
end
end

Runtime:addEventListener(“enterFrame”, collect)
[/code] [import]uid: 23649 topic_id: 16705 reply_id: 62548[/import]

Cool, @jeremyapplebaum12! So, I can get out of a loop anytime with the break then. I think I can make a good use of it. Thanks! [import]uid: 67217 topic_id: 16705 reply_id: 62569[/import]

Ya, you can, I hardly use it but I can see instances where it would be useful. I think you can get out of any if statement or function with it as well. [import]uid: 23649 topic_id: 16705 reply_id: 62604[/import]

Thanks guys. Was about to ask this question myself [import]uid: 26289 topic_id: 16705 reply_id: 62615[/import]

thanks very much to all of you
funny, but i got it working with different method only a couple of minutes after posting a problem)

i solved it like this:
[lua]local fires = {}

for i=1,5 do

local fire = display.newRect(0,0,20,10)
fire.x = 10
fires[i] = fire
fire.y = math.random(10,300)
transition.to(fires[2],{time=3000, x = 300})
end

local function collect()
for i,v in pairs(fires) do
if fires[i].x > 200 then
fires[i]:removeSelf()
fires[i] = nil
print(i)
end
end
end

Runtime:addEventListener(“enterFrame”, collect)[/lua]

can anyone tell me, is it good method or maybe its better to use something different? [import]uid: 16142 topic_id: 16705 reply_id: 62647[/import]

another question, if you dont mind
how can i detect collision between “enemy” and “fire” without using physics at all? [import]uid: 16142 topic_id: 16705 reply_id: 62655[/import]

Hey @darkconsoles, I’ve seen forum thread(s) about detecting collision without physics, but I don’t remember which post(s). If you search using collision as key word, you might stumble into them somehow.

About the “for i,v in pairs(fires) do”, I can understand what “i” would represent, but what is “v” doing there? What does it represent? (I haven’t had a chance to read that big article at http://www.lua.org/pil/7.2.html yet… hope you’d pardon my ignorance.)

Naomi [import]uid: 67217 topic_id: 16705 reply_id: 62677[/import]

Naomi,

I think the “v” there means VALUES.
Lets wait for the PROS.

Regards, [import]uid: 89165 topic_id: 16705 reply_id: 62680[/import]

Hey @darkconsoles, I just scanned through what I bookmarked, and here’s a thread that might be helpful to achieve collision detection without physics: http://developer.anscamobile.com/forum/2011/08/28/how-detect-which-object-closet-player

I’m sure I’ve seen other thread, but I don’t seem to have bookmarked them.

Naomi [import]uid: 67217 topic_id: 16705 reply_id: 62681[/import]

for the “v” thing i can give you an example)
[lua]local T = {“one”, “two”, “three”}

for i,v in pairs(T) do
print(i,v)
end[/lua]

i is an index in table, v is a value [import]uid: 16142 topic_id: 16705 reply_id: 62686[/import]

yes v is for value but you can use anything a,b. d,z. key,value it doesn’t matter their just variables

http://developer.anscamobile.com/code/jslua does simple collision detection [import]uid: 7911 topic_id: 16705 reply_id: 62689[/import]

Here’s a basic example of how you would detect a collision without physics with the code that’s in place:

[code]

local fires = {}

local wall = display.newRect(100,0,20,480)

for i=1,5 do
fires[i] = display.newRect(0,0,20,10)
fires[i].x = 10
fires[i].y = math.random(10,300)
end
transition.to(fires[1],{time=3000, x = 300})

local collect = function()
for i=1, #fires do
if fires[i].x > 200 then
fires[i]:removeSelf()
table.remove(fires, i)
break
end
end
end

local Collide = function()
for i = 1, #fires do
if fires[i].x > wall.x then
print (“fires”, i, “hit the wall”)
end
end
end

Runtime:addEventListener(“enterFrame”, Collide)
Runtime:addEventListener(“enterFrame”, collect)

[/code] [import]uid: 23649 topic_id: 16705 reply_id: 62698[/import]

@darkconsoles and @jstrahan, thanks for the explanation. Yessss, I totally get it now.

@jstrahan, I’ll book mark the link for simple collision detection. It sounds great.

EDIT: Hey, Rodrigo, I didn’t notice your post above. Thanks goes to you too. I appreciate all the help I receive.

Naomi [import]uid: 67217 topic_id: 16705 reply_id: 62691[/import]

Ah, clever, @jeremyapplebaum12.

Cheers,
Naomi [import]uid: 67217 topic_id: 16705 reply_id: 62699[/import]

one issue with the code above, the x reference on each object
it would not detect collision until the x reference points collide so if its top right collision would not happen until object one was on top of object two. you have to check left side object one against right side object two plus check right side object one against left side object two same for top and bottom
but the above code would work just not until objects on top of each other check my link above it detects all four sides for collision its abit crude i think but works i am planning to have a much simpler version up later next week [import]uid: 7911 topic_id: 16705 reply_id: 62702[/import]

@jstrahan What I gave was just an idea or sample of how to do it but you are right, the objects would be on top of each other but a collision was registered. An easy way to prevent them from over lapping would be to do something like:

 if( fires[i].x + 1) \> wall.x then  

However I think in almost all cases where collisions are needed it’s probably a lot easier and less stressful to just use physics. [import]uid: 23649 topic_id: 16705 reply_id: 62767[/import]