One object at a time - please someone help!

Okay, simple logic here eludes me for some reason…

All I want is to click a button and have a physics body object appear on screen (like a box), when I click the the close button I want the object to disappear and create a new object in it’s place.

Sounds simple, but I’ve tried everything in my expertise and this logic doesn’t seem to be as simple as I’m thinking, or is it?

Anyone? Bueller? Bueller?

[import]uid: 9046 topic_id: 6816 reply_id: 306816[/import]

Bueller Bueller

Post your code to see what you doing

c. [import]uid: 24 topic_id: 6816 reply_id: 23795[/import]

Sure thing Carlos - thanks for the help!

[lua]function newthing()

physics.pause()
if (thing) then --here I’m attempting to remove it but no go
thing.parent:remove( thing )
end

local thing = display.newImage(“thing.png”)
thing.x=180
physics.addBody( thing, “dynamic”, physicsData:get(“thing”))
localGroup:insert(thing)
transition.to(thing, { time = 100, xScale = .60, yScale = .60, transition = easingx.easeoutElastic }) – “pop” animation
return false
end

newthing()[/lua] [import]uid: 9046 topic_id: 6816 reply_id: 23806[/import]

try

[lua]local thing

function newthing()

physics.pause()
if (thing~=nil) then
thing:removeSelf()
end

thing = display.newImage(“thing.png”)
thing.x=180
physics.addBody( thing, “dynamic”, physicsData:get(“thing”))
localGroup:insert(thing)
transition.to(thing, { time = 100, xScale = .60, yScale = .60, transition = easingx.easeoutElastic }) – “pop” animation
return false
end

newthing()[/lua]

without the rest of your code though it’s hard to tell [import]uid: 6645 topic_id: 6816 reply_id: 23811[/import]

No, sorry - that didn’t do it either. I still end up up two objects on the screen. The only other part of the code I didn’t put is the button itself that fires off the newthing event

[lua] --new thing on screen
local gobtn = display.newRect( 0, 0, 100, 20 )
gobtn:setFillColor(90,90,90)
gobtn.x=350
gobtn.y=50
gobtn:addEventListener( “tap”, newthing )[/lua] [import]uid: 9046 topic_id: 6816 reply_id: 23814[/import]

it worked fine for me so you must have a scope problem. notice i defined a local copy of “thing” above the function [import]uid: 6645 topic_id: 6816 reply_id: 23832[/import]