Removing/adding items in a table using collision -- Please help!

I searched the forum and couldn’t find a good example for this. I created a set number of objects (stars) on the screen. I want to remove each object individually when my character touch each one. If anyone can help me figure this one out I would really appreciate it. I’ve been able to create the stars/objects, however, I have not been able to remove (each one) when my character collide with them. here is a sample of my code. Just to be clear, what i am looking for is that once the stars are created, my character would collide with star1 and it would disappear, then collide with star2 and star2 would disappear and so on. Thanks again.

numOfImages = 20 -- define how many images on what on screen  
   
 local function displayNumber()  
   
 letterHolder = {} -- initialize array  
  
   
for i=1,numOfImages do -- start for loop to go from 1 to 10  
  
 letterHolder[i]= display.newImageRect( "images/glow\_star.png", 20, 20 )   
 letterHolder[i].x = frog.x-- display the rectangle in a random x position on screen  
 letterHolder[i].y = frog.y-- display the rectangle in a random y position on screen  
  
 physics.addBody(letterHolder[i],"kinematic")  
 localGroup:insert(letterHolder[#letterHolder])  
 --letterHolder[i]: removeSelf()  
 --letterHolder[i]=nil  
 --letterHolder[#letterHolder].myName= "star"  
  
   
  
  
 local function onCollision(event)  
 if (event.phase == "ended") then  
 --print("collided")  
  
 print("collides")  
 --letterHolder[#letterHolder]:removeSelf()   
 letterHolder[i]:removeSelf()   
 end  
 end  
 --letterHolder[#letterHolder]:addEventListener("tap", onCollision)  
 girl:addEventListener("collision", onCollision)  
  
  
 end  
  
end  
timer.performWithDelay(600, displayNumber, numOfImages)  
  

Error message: attempt to call method removeSelf a nil value. Not sure if this code would remove stars individually either. thanks again for the help. [import]uid: 104131 topic_id: 33807 reply_id: 333807[/import]

It looks like you’re creating 20 stars each time your delay timer is called.
And you call it 20 times… ? And create a local table letterHolder each time… ?

Anyhow, try this… untested…

[code]
numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random?
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“kinematic”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms
function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )
end

end
function removeStar ( star )

star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

[/code] [import]uid: 186251 topic_id: 33807 reply_id: 134372[/import]

@SAM3DUS, Thank you for your help. I tried your code and it creates the stars randomly in the frog.x and frog.y location, however, my character (girl) is not detecting any collision. I even add a print statement in the onStarCollision function and it did not print. I also tried moving the onStarCollision function before the displayNumber function and still no star was removed. Do you have any other suggestion? I know that I am so close to figure this out. I just can’t get it. Thanks again for your help.

[code]

girl.myName=“player” – I added this
numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random? --frog moves randomly on the screen.
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“static”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms

function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

print(“collied”)
– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )
end

end

function removeStar ( star )
print(“collied”)
star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

[/code] [import]uid: 104131 topic_id: 33807 reply_id: 134386[/import]

Yes, move the onStarCollision function before the displayNumber function
and probably move the removeStar before the onStarCollision

> girl.myName=“player” – I added this

Ok, but in onStarCollision, you are checking for event.other.name so change it to
girl.name=“player”

A good check for that sort of problem is to put a print in onStarCollision before the ‘if’
Then you can tell if the function is working and then whether it is filtering in the ‘if’ as expected. [import]uid: 186251 topic_id: 33807 reply_id: 134390[/import]

@sam3dus. Thank you so much. You are a life saver! Here is the code that works for me. It’s seem like the biggest problem, which you correctly pointed out was that I was using "girl.myName=“player” instead of “girl.name=“player””. And I commented out the comment “self.contact.isEnabled = false” which was giving me this error “Attempt to index contact an nil value”. My last question is that if i want to check if all the stars have been removed, how would I check it. For example if all stars are removed print finished. I am sure this topic will help out many people thanks to you… Thanks again for your help.

[code]

numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

girl.name=“player”

function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

print(“collied”)
– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )

end

end

function removeStar ( star )
print(“collied”)
star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random?
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“static”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms

[/code] [import]uid: 104131 topic_id: 33807 reply_id: 134423[/import]

It looks like you’re creating 20 stars each time your delay timer is called.
And you call it 20 times… ? And create a local table letterHolder each time… ?

Anyhow, try this… untested…

[code]
numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random?
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“kinematic”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms
function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )
end

end
function removeStar ( star )

star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

[/code] [import]uid: 186251 topic_id: 33807 reply_id: 134372[/import]

@SAM3DUS, Thank you for your help. I tried your code and it creates the stars randomly in the frog.x and frog.y location, however, my character (girl) is not detecting any collision. I even add a print statement in the onStarCollision function and it did not print. I also tried moving the onStarCollision function before the displayNumber function and still no star was removed. Do you have any other suggestion? I know that I am so close to figure this out. I just can’t get it. Thanks again for your help.

[code]

girl.myName=“player” – I added this
numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random? --frog moves randomly on the screen.
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“static”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms

function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

print(“collied”)
– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )
end

end

function removeStar ( star )
print(“collied”)
star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

[/code] [import]uid: 104131 topic_id: 33807 reply_id: 134386[/import]

Yes, move the onStarCollision function before the displayNumber function
and probably move the removeStar before the onStarCollision

> girl.myName=“player” – I added this

Ok, but in onStarCollision, you are checking for event.other.name so change it to
girl.name=“player”

A good check for that sort of problem is to put a print in onStarCollision before the ‘if’
Then you can tell if the function is working and then whether it is filtering in the ‘if’ as expected. [import]uid: 186251 topic_id: 33807 reply_id: 134390[/import]

@sam3dus. Thank you so much. You are a life saver! Here is the code that works for me. It’s seem like the biggest problem, which you correctly pointed out was that I was using "girl.myName=“player” instead of “girl.name=“player””. And I commented out the comment “self.contact.isEnabled = false” which was giving me this error “Attempt to index contact an nil value”. My last question is that if i want to check if all the stars have been removed, how would I check it. For example if all stars are removed print finished. I am sure this topic will help out many people thanks to you… Thanks again for your help.

[code]

numOfImages = 20 – define how many images on what on screen
letterHolder = {} – initialize array

girl.name=“player”

function onStarCollision( self, event )

if event.other.name == “player” then – assumes your character has name=player
self.contact.isEnabled = false – stop collision checks

print(“collied”)
– create a small delay before removing to allow collision to end
– but since you’re using kinematic, will the collision end ?
– if this is a problem, then make the stars static and check for collisions via your player
local myclosure = function() return removeStar(self) end
timer.performWithDelay( 5, myclosure, 1 )

end

end

function removeStar ( star )
print(“collied”)
star.isBodyActive = false
local star_index = table.indexOf(letterHolder, star)
table.remove( letterHolder, star_index )
display.remove(star)
star = nil
– afaik the eventListener is auto removed when the object is destroyed
print( "#stars: ", #letterHolder )

end

function displayNumber()

local star = display.newImageRect( “images/glow_star.png”, 20, 20 )
star.x = frog.x-- display the rectangle in a random x position on screen – what is frog and how is it random?
star.y = frog.y-- display the rectangle in a random y position on screen
physics.addBody(star,“static”)

letterHolder[#letterHolder+1] = star

star.collision = onStarCollision
star:addEventListener( “collision”, star )

end
timer.performWithDelay(600, displayNumber, numOfImages) – create a star every 600 ms

[/code] [import]uid: 104131 topic_id: 33807 reply_id: 134423[/import]