How to remove an object when clicked?

Hi everybody, I currently have this code which spawns a new Crate:

local function spawnCrate(event) local crate = display.newImage("images/crate.png") crate.x = event.x; crate.y = event.y; --crate.xScale = 0.5; crate.yScale = 0.5; physics.addBody( crate, { density=15, friction=0.5, bounce=0.001} ) localGroup:insert(crate) end

but I’d like to remove an individual crate when it’s clicked. How would I go about doing this?

Thanks. [import]uid: 68047 topic_id: 11832 reply_id: 311832[/import]

first you need to take off your .png from “imagens” because if you will compile for android, you will get a error @_@

well… i dont know if i’m right but…

[code] local function spawnCrate(event)
local crate = display.newImage("
crate.png")
crate.x = event.x; crate.y = event.y;
–crate.xScale = 0.5; crate.yScale = 0.5;
physics.addBody( crate, { density=15, friction=0.5, bounce=0.001} )
localGroup:insert(crate)
end

local removeBox

removeBox = function(e)
if e.phase == “ended” then
e:removeSelf()
end
end

crate:addEventListener( “touch”, removeBox )
[/code]

maybe this works [import]uid: 23063 topic_id: 11832 reply_id: 43101[/import]

Are you sure that would work? You never called which object to remove, unless your adding a call for your new function onto his original function. [import]uid: 25216 topic_id: 11832 reply_id: 43135[/import]

i guess it will works
take a shot ^^
i cant try right now [import]uid: 23063 topic_id: 11832 reply_id: 43137[/import]

Yeah man. I tried it out. It didn’t work. I changed some of the code around though, to my preferred function method. The game still ran though, so that code wasn’t wrong or any thing. Weird. [import]uid: 25216 topic_id: 11832 reply_id: 43138[/import]

@DLuksa,
Hey…

The code from Dhennrich did not work for you for the following reasons.

  1. crate is a variable that is local to the function spawnCrate so when you are out of that function, the EventListener is not assigned to it as the variable is nil. so declare “local crate” outside of the function spawnCrate(event)

  2. You might be running this as the code instead of using director or whatever, so remove the localGroup:insert(crate) this is not required and can cause errors

  3. You have declared all functions but not called any, so call spawnCrate() as the last line of your code.

  4. In the removeBox function, change the line e:removeSelf() with e.target:removeSelf() as e is the event object not the display object.

  5. Put the line create:addEventListener inside of the function spawnCrate than outside.

in case you are confused, here’s the same rearranged.

[lua]local physics=require(“physics”)
physics.start()

local function spawnCrate ()
local crate = display.newImage(“crate.png”)
physics.addBody(crate,{density=15, friction=0.5, bounce=0.001})

local function removeBox (e)
if “ended” == e.phase then
e.target:removeSelf()
end
end

crate:addEventListener( “touch”, removeBox)
end

spawnCrate()[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11832 reply_id: 43154[/import]

Yes Jay, that’s kind of like the code I came to last night before checking this thread again. However, it’s a bit glitchy and I’ll record a video why, but I’ll also see if your slightly different code fixes it…

local function spawnCrate(event) local crate = display.newImage("images/crate.png") crate.x = event.x; crate.y = event.y; --crate.xScale = 0.5; crate.yScale = 0.5; physics.addBody( crate, { density=15, friction=0.5, bounce=0.001} ) localGroup:insert(crate) local function rmvCrate(event) if event.phase == "moved" then display.remove(crate) end end crate:addEventListener("touch", rmvCrate) end

Thanks. [import]uid: 68047 topic_id: 11832 reply_id: 43158[/import]

Thanks Jay! I understand now. I cannot try the code, as I am using my iPad, but I will soon. I have a question though; what is [lua]nil[/lua]? I’m brand new so please try to bare with my noobieness lol [import]uid: 25216 topic_id: 11832 reply_id: 43163[/import]

Jay, the part of your code which is if “ended” == e.phase then… etc won’t work because it clashes with the spawn crate function (this is the big bug I’m having) so for the time being I’ve used “moved” so when you slide across a crat it removes it. This is the cleanest method I’ve found so far, because with “ended” it just recreates the crate in a slightly different position. [import]uid: 68047 topic_id: 11832 reply_id: 43168[/import]

George,
There is some more code that you are talking about than the one I have posted, now I cannot see what you are doing as you have not posted your code, this code that I have posted, will work, it will spawn one crate and on clicking that crate, it will remove it. That’s all…

So, you need to post you code to see what else you are doing that could be messing things up, anything after that I give up and leave you to resolve it, if you insist that the code snippet it faulty without looking at where else it could be causing the bug.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11832 reply_id: 43170[/import]

Err, you’ve not understood me properly =/ I wasn’t saying your snippet was faulty, I was saying mine was. I know your code works, but it won’t work when applied to mine. The whole point of my code is for a crate to spawn when the user clicks. Your code just spawns the crate and allows it to be removed.

What I was saying was if I applied your remove code (which is almost identical to the one I already created) it clashes with the my spawnCrate function in terms of event handling (ended, moved, cancelled, began, etc.)

Here’s the code that spawns and removes the crate, for me, which is a bit buggy: local function spawnCrate(event) local crate = display.newImage("images/crate.png") crate.x = event.x; crate.y = event.y; --crate.xScale = 0.5; crate.yScale = 0.5; physics.addBody( crate, { density=15, friction=0.5, bounce=0.001} ) localGroup:insert(crate) local function rmvCrate(event) if event.phase == "moved" then event.target:removeSelf() end end crate:addEventListener("touch", rmvCrate) end

Listener: background:addEventListener("tap", spawnCrate)

Thanks. [import]uid: 68047 topic_id: 11832 reply_id: 43171[/import]

George, and that is exactly what I am trying to say, that something outside of this code is creating the issue.

Are you using director? you are using the localGroup:insert(create)

The culprit is your listener which was outside of the code :slight_smile:

background:addEventListener(“tap”,spawnCrate)

the crate is overlaid on top of the background, so when you tap on the crate, the tap goes through and also triggers the background tap event, which spawn a new crate.

Instead create a button that will spawn and you will see that it will work fine.
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11832 reply_id: 43174[/import]

So, I need to change the background event listener so it’s no longer on the background? But then how do I make it so the crate spawns at the user’s finger position?

If it’s a button, do you mean one that is invisible and fills the whole screen?

Yeah, I am using director.

Thanks! [import]uid: 68047 topic_id: 11832 reply_id: 43175[/import]

George, is this for testing or part of your larger project?
if it is for testing, just use a rect like

code
.
.

at the bottom place this code
[lua]local rect = display.newRect(10,10,150,50)
rect:setFillColor(128,0,0)
rect:addEventListener(“tap”, spawnCrate)[/lua]

if you want the whole screen to be a button, where you can spawn by tapping anywhere, then try setting the eventHandler on Runtime rather than on the background.

cheers,

?:slight_smile:
[import]uid: 3826 topic_id: 11832 reply_id: 43177[/import]

I did the rect thing, but it still has the same problem when spawning crates in the rectangle. This is for a the larger thing, and I still get exactly the same problem with runtime, and it also causes another problem with runtime to do with the menu and a crate spawning straight after the menu screen is exited.

Any other ideas? Because Runtime doesn’t work either. This is really frustrating ! :wink:

Thanks. [import]uid: 68047 topic_id: 11832 reply_id: 43179[/import]

Mate,
I can only help you so much when I am blindfolded…

I understand that you are trying to do something more and the issue lies there, coz if you place a button to spawn and then click on the crate to remove, it just works.

So there is something else that you are doing or some code is doing that is causing the problem.

I guess you need to get a little paid advice, there are a few on this forum that will be happy to do that for you.

I could, but then my rates for that would be quite high,

so, I guess this is where we part ways

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11832 reply_id: 43180[/import]

Alrighty then?

Thanks for your help so far, I suppose.
[import]uid: 68047 topic_id: 11832 reply_id: 43181[/import]

Well, here’s a video of the bug anyway. The crate readds itself like it’s moving up and down when all I want to do it remove it. Sometimes it works, sometimes it doesn’t.

http://www.youtube.com/watch?v=zjSHOQtesCg [import]uid: 68047 topic_id: 11832 reply_id: 43182[/import]

George,
just like the green and the red buttons, create another one on the left hand side of the screen that says spawn crate or you can have a bar on top of the screen if getting the x co-ordinate is important for you.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11832 reply_id: 43183[/import]

The problem is the whole mechanic of the game pivots on the idea of placing crates where you like…

Is it even possible to get around this problem?

Thanks again! [import]uid: 68047 topic_id: 11832 reply_id: 43190[/import]