Help removing from group

Hi,
I am trying to add objects to a group and then remove them as needed but I am having a hard time removing them.

Here is my code:

I initiate the group

onTarget = display.newGroup ()  

Then below I insert the newly created object when it collides with the scope:

if( event.phase == "began" and event.other.myName == "circle") then  
 onTarget:insert(newMo)  
 --onTarget[onTargetID] = newMo  
 --onTargetID = onTargetID + 1  
 --onTarget =newMo  
 newMo.killable = true  
 scope.scopeSnap()  
 --scope.scope.x = newMo.x Makes the physics engine crash  
 --scope.scope.y = newMo.y  
 end  
 if(event.phase == "ended" and event.other.myName == "circle") then  
 event:remove(newMo.onTarget)  
 --onTarget = nil  
 newMo.killable = false  
 scope.scopeUnsnap ()  
 end  

I am trying to remove it when it goes out of scope.

Thanks for your help. [import]uid: 8192 topic_id: 2205 reply_id: 302205[/import]

Use target:removeSelf(). See http://developer.anscamobile.com/content/application-programming-guide-graphics-and-drawing#Removing_Objects_Properly. [import]uid: 54 topic_id: 2205 reply_id: 6684[/import]

Unfortunately, if I do that, the object itself would be removed. I just want to remove it from the group. Let me be specific.

One of the objects collides with my scope (gun scope) when it does it becomes “onTarget” if it doesn’t collide with the scope it needs to be removed from the “onTarget” group. If the object is “onTarget” then I can send the command to kill it. By having this group it will allow me to have multiple objects on target at the same time, say two object that are overlapping and one becomes on target but the one behind stays on target.

If I use removeSelf() the object would be removed while in reality, the object just went of target.

[import]uid: 8192 topic_id: 2205 reply_id: 6710[/import]

An object is always in a group. When you create it is is already inside the stage group. So do either

  1. Create another group which you are using just as a placeholder and assign the object into it

or

  1. Get the stage group handler at the beginning when you create your first object (Parent of the object) and then assign it to that. [import]uid: 5712 topic_id: 2205 reply_id: 6727[/import]

@MikeHart.

  1. is exactly my strategy. I am just having problems with the syntax removing it from that group. In my case onTarget group [import]uid: 8192 topic_id: 2205 reply_id: 6730[/import]

You have your syntax wrong. Here is a sample of an onComplete transition handler function:

[lua]–****************************************************************
local onFlashDelete = function( target )
–****************************************************************
target.parent:remove( target )
end[/lua]

with your code it should be:

[lua]newMo.parent:remove(newMo)[/lua] [import]uid: 5712 topic_id: 2205 reply_id: 6750[/import]

Hi Mark this removes the object. I think I might be confusing you with my inexperience. When I insert an object into a group is it just putting a reference of that object or the actual object into the table? I thought it was just a reference. So when I remove them from a table I am just removing the reference.

What I am trying to do identify what objects are on target in the scope so that if I decide to kill them they get killed. Say there are 6 ships on screen and 2 of them are on Target. I press the trigger and those 2 ships die.

Say one of the ships goes out of target while the other one stays on target and then i press the trigger. Only that ship dies.

Now I thought one way to do that was to create a group or a table and insert a reference to what ship it is on target when it collides with the scope sensor and obviously remove it when it doesn’t collide. Maybe it’s not the way to do it though. [import]uid: 8192 topic_id: 2205 reply_id: 6753[/import]

Then you have to place(move) the object into another group. Like I said in another topic, objects are always inside a group. When they are created, they are already in the STAGE group. By inserting them into a user defined group, you move them to that group. This makes all sence when you think about the parent property. No matter how many times you insert an object inside groups, the parent property will always be the last group you have insert your object into it. An object can never be in more than one group at the same time. [import]uid: 5712 topic_id: 2205 reply_id: 6757[/import]

I see. It’s like a file in a folder. So what about a table? If I insert it in a table instead of a group?

for example

  
onTarget = {} -- Create a table  
  

Thank you very much for the explanation [import]uid: 8192 topic_id: 2205 reply_id: 6758[/import]