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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

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.

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

1
2
3
if note then
noteDelete()
end

One tip, use

display.remove(note)

instead of note:removeSelf()

Okay, I switched to display.remove(note), but only most recently spawned note will remove itself!

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.

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?

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

noteTable = {}

noteTable[#noteTable+1] = note

views:1642 update:2012/2/9 11:37:26
corona forums © 2003-2011