Multiple Objects deleting

So when an object is deleted and there is only one, everything works fine. However, when multiple are deleted, everything goes wrong.

[lua]local function note(index)

local note = display.newImage(“quarter.png”)
function noteDelete()
note:removeSelf()

end
note:scale(".5",".5")
if notes[index].rot == true then
note:rotate(“180”)
end
note.x=“750”
note.y= notes[index].posy
freq1 = notes[index].freq
top1 = notes[index].top

function check(event)
f = r:getTunerFrequency()
txt.text = f
if f < freq1 + 20 and f > freq1 - 20 or f < top1 + 20 and f > top1 - 20 then
print(“hit”)
noteDelete()
end
return true
end

Runtime:addEventListener(“enterFrame”, check)

local function move()
note.x = note.x - 4
end
timer.performWithDelay(20, move, -1)

return note
end[/lua]

How can I make it so there all the notes do not freeze and lots of errors appear? I have all the note in a table. Is there any way I could pass in the note table index and somehow delete that one? Or is there an easy fix. Thanks! [import]uid: 59735 topic_id: 20419 reply_id: 320419[/import]

Well I see lots of issues and potential issues.

First I’m not sure how safe it is having a “local note” inside of a function named “note”. It’s probably okay based on how scoping works internally, but it certainly creates confusion trying to read the code.

You create a singular “note” from a display.newImage, but I never see you add it to the table notes{}

Lines 8, 10 and 12 you’re passing strings to things that expect numbers. Lua should cover you by converting on the fly, but you probably should keep values in whatever format they are supposed to be in, if nothing else for code readability.

In line 4 you do a removeSelf on note, but you’re missing the subsequent:

note = nil

which tells the garbage collector that it can reclaim the memory, without it, the memory could get lost and a memory leak created.

This line:

Runtime:addEventListener(“enterFrame”, check)

You’re telling your app to 30 times per second call check() and within check() you’re checking “r”'s tunerFrequency 30 times a second and if your conditions are met, you delete “note”. If those conditions are true 1/30th of a second later (which they likely are), you try to delete note again and again and again. You never stop the “enterFrame” event or have a test inside of check to make sure that note exists so it spews errors after errors because you’re trying to delete something that doesn’t exist very rapidly.
[import]uid: 19626 topic_id: 20419 reply_id: 79952[/import]

thank you, sorry I am still learning. I have added checks like

[lua]if note then
noteDelete()
end[/lua]

But should I delete the runtime listener? It is working pretty good, as I want all of the notes to abide by it. However, there will be points when there is 0, 1, or even 2 notes on the screen. Right now, If there is two notes, the note will stop moving, and the app wont work.

Thanks [import]uid: 59735 topic_id: 20419 reply_id: 79964[/import]

One tip, use

display.remove(note)

instead of note:removeSelf() [import]uid: 84637 topic_id: 20419 reply_id: 80175[/import]

Okay, I switched to display.remove(note), but only most recently spawned note will remove itself! [import]uid: 59735 topic_id: 20419 reply_id: 80202[/import]

Keep in mind that the only real difference between :removeSelf and display.remove(object) is that display.remove() just wraps an “if” test around the removeSelf() call to make sure it’s not nil.

Other than preventing some crashes, it’s not going have any behavior changes in your program.
[import]uid: 19626 topic_id: 20419 reply_id: 80204[/import]

Thanks robmiracle. So as I spawn the notes, It will work great. However, if I spawn 2 notes, the one spawning first becomes inaccesable or something. Calls to delete it or change it dont work. Is there someway I can delete it via the table so I dont delete all notes, but just the note is table index 4 or something? [import]uid: 59735 topic_id: 20419 reply_id: 80238[/import]

Add your created notes to a table, so you can access them all.

noteTable = {}
noteTable[#noteTable+1] = note

[import]uid: 84637 topic_id: 20419 reply_id: 80908[/import]