Removing objects

I have an app where when a musical notes pitch is detected, the note deletes itself. There are multoiple notes spawning, All in a table. I want to delete whichever note is detected.

[lua]local function note(index)

local note = display.newImage(“quarter.png”)
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].top
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”)
remove(self)
end
return true
end

Runtime:addEventListener(“enterFrame”, check)

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

return note
end[/lua]

Right now, the remove self does not work at all. I tried replacing that with note:removeSelf() but that causes all the notes to remove. How can I only delete one? [import]uid: 59735 topic_id: 20120 reply_id: 320120[/import]

  1. you are using remove without context. You need to have either display.remove( note ) or note:removeSelf()

  2. You are using self without context. In order to use self you need to have it wrapped inside a function name [lua]note.delete = function(self)[/lua] or [lua]function note:delete().[/lua] [import]uid: 54716 topic_id: 20120 reply_id: 78629[/import]

To add to what was said above, if your object is a display object

use

display.remove as opposed to removeSelf [import]uid: 84637 topic_id: 20120 reply_id: 78943[/import]

silly me. Thanks a lot. All working. Also, Can you explain self functions? Can you pass self? [import]uid: 59735 topic_id: 20120 reply_id: 79427[/import]

silly me. Thanks a lot. All working. Also, Can you explain self functions? Can you pass self? [import]uid: 59735 topic_id: 20120 reply_id: 79428[/import]

Hi: i needddd HELPPPPPPPPP:
i have a lot of monsters named equal like this
--------------------CODE-----------------------

function crear_m1 ()
local monstruo = display.newImage(“images/monstruos/monstruo1.png”)
physics.addBody(monstruo, {bounce=0.3, radius = 35, friction=0.5})

local function killm1 (event)
event.target:removeSelf()
end
monstruo:addEventListener (“touch”, killm1)
end
carreta:addEventListener(“touch”, crear_m1)

---------------------DESCRIPTION---------------------
when i touch an element i create monsters, when i touch a monster created it removeSelf But it delete the properties of the other mosnter created so it is a pain…CAN YOU HELP ME PLEASE I AM SO STOCK… [import]uid: 110762 topic_id: 20120 reply_id: 79770[/import]

Can you post more code? If it is effecting other monsters, we need to see more code. Also, please wrap your code in lua tags so that it is more readable. [import]uid: 54716 topic_id: 20120 reply_id: 79792[/import]

-----------------code---------------
[lua]local carreta= display.newImage(“carreta.jpg”)

function crear_m1 ()
local monstruo = display.newImage(“images/monstruos/monstruo1.png”)
monstruo.x= math.random()*960
monstruo.y= math.random()*640
monstruo.myName= “mon”
physics.addBody(monstruo, {bounce=0.3, radius = 35, friction=0.5})

carreta:addEventListener(“collision”, carreta)
function gameLoop(event)

monstruo:setLinearVelocity(
SPEED * (carreta.x - monstruo.x),
SPEED * (carreta.y - monstruo.y)
)
monstruo.rotation=0
end

Runtime:addEventListener(“enterFrame”, gameLoop)
local function killm1 (event)
event.target:
physics.addBody(monstruo, {bounce=0.3, radius = 35, friction=0.5})
end
monstruo:addEventListener (“touch”, killm1)
end
carreta:addEventListener(“touch”, crear_m1)[/lua] [import]uid: 110762 topic_id: 20120 reply_id: 79937[/import]

I still can’t see anything suspicious since your code only describes creating one monstrous, especially since you forgot to post it with the removal code. And what do you mean by delete properties of the other monsters, do they disappear, just stop moving, or… [import]uid: 54716 topic_id: 20120 reply_id: 80371[/import]

ok, i am a noob and i just want with a timer to create a lot of monsters that attack my hero, when y touch then y want they to die, so y write a code that generate a lot of monsters the problem is that all the monsters have the same propertyes and have instance name so when i removeSelf one of this little guys the others mosnters created have no more the physics [import]uid: 110762 topic_id: 20120 reply_id: 80660[/import]

Post more code please. [import]uid: 54716 topic_id: 20120 reply_id: 80954[/import]

[lua]---- disegnatore daniel guevara varela
display.setStatusBar(display.HiddenStatusBar)
------no se esconda mi aplicacion-------
system.setIdleTimer(false)
------REQUIRES-----
require “sprite”
local ui = require(“ui”)
local movieclip = require(“movieclip”)
local physics = require (“physics”)
physics.start()
– Functions
local gameListeners = {}
local SPEED = 0.2
– ALMAS–
local carreta
local monstruo1
–OBJETOS–
local barriles
local piedras

---------FONDOS-----------
local mapa = display.newImage(“images/escenarios/floor.jpg”)
mapa.x=760
transition.to( mapa, {x=460,time=10000} )
transition.to( mapa, {y=340,time=10000} )

------Variables-----------
foreground = display.newGroup()
------------------MOVIECLIPS
carretalevel1 = {}
for i = 2,61 do
table.insert( carretalevel1, “images/Carreta/level1/standar (” … i … “).png” )
end
cambio = carretalevel1
carreta = movieclip.newAnim( cambio )
foreground:insert( carreta )
carreta.x = display.contentWidth / 2
carreta.y = display.contentHeight / 2
carreta:play{ startFrame=2, endFrame=61, loop=0 }

function rotarc (event)
–carreta.rotation = carreta.rotation + 1

–carreta.x = event.x
–carreta.y = event.y
end
carreta:addEventListener(“touch”, rotarc)
carreta.rotation=-10

local carreta_area = display.newRect (0,440,320,80)
carreta_area:setFillColor(180,150,100)
carreta_area.x=display.contentWidth / 2
carreta_area.y=display.contentHeight / 2
carreta_area.isVisible = false
carreta_area.myName=“carreta_zona”
physics.addBody (carreta_area, “static”, {friction=0, bounce=0, density=1})
-------------------- SPRITES ---------
–[[
local herosheet = sprite.newSpriteSheet(“images/Carreta/level1/walker.png”, 400, 280)
local heroset = sprite.newSpriteSet (herosheet, 1, 16)
sprite.add ( heroset, “merchantwalk”, 1, 16, 600, 0 )
local hero = sprite.newSprite (heroset)
hero.x = display.contentWidth / 2
hero.y = display.contentHeight / 2

hero:prepare(“merchantwalk”)
hero:play()
–]]
–TIMERS—
local markTime = os.time()
local timePassed = os.time() - markTime
local badMonster
local monsterMarkTime = os.time()
local SpawnM1 = math.random()*1-0.2

local spawnMonster = function()
local monstruo = display.newImage(“images/monstruos/monstruo1.png”)
monstruo.x= math.random()*960
monstruo.y= math.random()*640
monstruo.myName= “mon”
physics.addBody(monstruo, {bounce=0.3, radius = 35, friction=0.5})
carreta:addEventListener(“collision”, carreta)
function gameLoop(event)
monstruo:setLinearVelocity(
SPEED * (carreta.x - monstruo.x),
SPEED * (carreta.y - monstruo.y)
)
monstruo.rotation=0

end
Runtime:addEventListener(“enterFrame”, gameLoop)
function killer (event)
if event.phase == “ended” then
event.target:removeSelf()
print “muere”
end
end
monstruo:addEventListener( “touch”, killer )

monsterMarkTime = os.time()
end

local gameListener = function( event )

local monsterSpawnCounter = os.time() - monsterMarkTime

– check if 10 seconds has passed since last spawn
if monsterSpawnCounter >= SpawnM1 then
spawnMonster()
end
end

– start game’s main enterFrame listener
Runtime:addEventListener( “enterFrame”, gameListener )
----------- MONSTRUOS--------

-------------COLISION-------------
carreta_area:addEventListener(“collision”, carreta_area)

function carreta_area:collision (event)
if event.other.myName == event.other.myName then
print “HITTTTTT POINTSSS!”
–score.setScore (score.getScore()+1)
–carreta.x=carreta.x-1

end
end[/lua] [import]uid: 110762 topic_id: 20120 reply_id: 81046[/import]

I see a problem, but I am not sure if that is your problem. With touch functions you usually want to have it return true, other wise everything else below your touch will also get hit. So if you have monster layered underneath your finger you could be deleting them all. So change your function to this:
[lua]function killer (event)
if event.phase == “ended” then
event.target:removeSelf()
print “muere”
end
–add true
return true
end
monstruo:addEventListener( “touch”, killer )[/lua]
Let me know what that changes. [import]uid: 54716 topic_id: 20120 reply_id: 81133[/import]

no change… look i see something, when i delete this it works and it let me remove the monsters (no bugs) the problem is that i really need that codeto make my monsters go to the center of the screen.

there is a way to make hte gravity central?
[lua]function gameLoop(event)
monstruo:setLinearVelocity(
SPEED * (carreta.x - monstruo.x),
SPEED * (carreta.y - monstruo.y)
)
monstruo.rotation=0

end
Runtime:addEventListener(“enterFrame”, gameLoop)[/lua]
thanks a lot ir8primates [import]uid: 110762 topic_id: 20120 reply_id: 81167[/import]

It seems this is a common problem when you combine enterFrame with a group of objects that use the command of removeself(). I’m having this same problem myself, and I’m pretty sure it’s because it screws up the Runtime event, which applies to all objects. There is even an ambiguous warning about using object removeself() with Runtime, and the solution is posted as eliminating the Runtime. However, doing this leaves all other objects without an eventlistener. So how do you remove an object that’s part of a larger group without screwing up it’s Runtime “enterFrame” event? I’ve searched these forums endlessly only to find that there is no posted solution on this issue. [import]uid: 136133 topic_id: 20120 reply_id: 120266[/import]

It seems this is a common problem when you combine enterFrame with a group of objects that use the command of removeself(). I’m having this same problem myself, and I’m pretty sure it’s because it screws up the Runtime event, which applies to all objects. There is even an ambiguous warning about using object removeself() with Runtime, and the solution is posted as eliminating the Runtime. However, doing this leaves all other objects without an eventlistener. So how do you remove an object that’s part of a larger group without screwing up it’s Runtime “enterFrame” event? I’ve searched these forums endlessly only to find that there is no posted solution on this issue. [import]uid: 136133 topic_id: 20120 reply_id: 120266[/import]