Help removing certain display objects

Alright, I’m sure that the answer is a simple one I’m just missing, but here’s the problem:

I am drawing a rectangle to the screen through a function. Every time I call this function, I need the previous rectangle to be destroyed.

I had the code working fine by just moving one, but I don’t know how to resize physics bodies, so…

Anyways, here’s the code I have.

--handle reflector  
--display.remove( object )  
  
 local reflector = display.newRect(0,0,math.abs(calculatehypotenuse(xoffset,yoffset)),15,2)  
  
 reflector:setFillColor(0,0,0,255)  
  
 physics.addBody(reflector, {bounce=1})  
 reflector.bodyType = "static"  

I assumed that, because display.remove was before creating the shape, it would remove any existing reflectors. what it actually does is remove all the reflectors. Uncommenting it means that I draw reflector after reflector.

So how do I get rid of the earlier object? Is it stored in a table or something I can address? [import]uid: 48302 topic_id: 11799 reply_id: 311799[/import]

I’ll be straight with you, I didn’t know display.remove was a function.

I personally use something like

object\_reflector = {} -- create a kind of protoclass using tables  
function object\_reflector:new(...) --add a function to your class to create a new one  
 reflector = display.newRect(0,0,math.abs(calculatehypotenuse(arg[1],arg[2])),arg[3],arg[4])  
 reflector:setFillColor(0,0,0,255)  
 physics.addBody(reflector,{bounce=1})  
 reflector.bodyType = "static"  
 return reflector  
end  
  
local reflector1 = object\_reflector:new(xoffset,yoffset,15,2)  
--This makes a reflector, putting it in the variable called reflector1  
--if you want to replace it, try  
reflector1 = object\_reflector:new(xoffset,yoffset,20,4) --or whatever you want for your new one  
  

I’m not sure if that works, I don’t have time at the moment to try it :slight_smile:
If it doesn’t, tell me and I’ll come up with a fix for you [import]uid: 34945 topic_id: 11799 reply_id: 42971[/import]

Alright, so I implemented your code, but it seems to have the same behavior as before. It still creates a snake of new bodies whenever I click. I need there to be only one reflector on the screen at one time, the most recently created one. Maybe I’m missing how to do that?

Thanks for the help!

*edit* it just ocurred to me that I am going to have the same problem with ball collisions. I have something that should destroy balls spawned. How do I remove the ball on collision without getting rid of all the balls? [import]uid: 48302 topic_id: 11799 reply_id: 43063[/import]

Why does the display object need to be “destroyed”? Can you not just reuse the display object? [import]uid: 26769 topic_id: 11799 reply_id: 43078[/import]

I thought I could, but the reflector is resized each time I move it. I had the code working fine, but when I enabled hybrid physics mode, I noticed that my physics body wasn’t resizing. If you know a way to do that, then this is a moot problem. [import]uid: 48302 topic_id: 11799 reply_id: 43079[/import]

I see. I’m not yet familiar with physics in Corona so I can’t help on that score. Sorry. [import]uid: 26769 topic_id: 11799 reply_id: 43082[/import]

That’s okay. I just started playing with Corona, and this is the first real problem I’ve had. I just have to figure out how to handle display objects. Next up is modifying my function to add the display objects to a table, rather than a simple variable, or something.

We’ll have to see if that works. [import]uid: 48302 topic_id: 11799 reply_id: 43086[/import]

Using a table of objects will no doubt work, but I’m thinking that it shouldn’t be necessary to destroy/recreate objects just to change the physics stuff. Linked knowledge, as I said, so hopefully someone can pop on here and steer you right :slight_smile: [import]uid: 26769 topic_id: 11799 reply_id: 43089[/import]