How to remove an object when clicked?

How about using a boolean flag?

and I hope that should solve your problem…

cheers,

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

Thanks. I’ll try solving it myself but thanks a whole bunch for trying to help!

Much gratitude,

George. [import]uid: 68047 topic_id: 11832 reply_id: 43193[/import]

yea… you can use a function or a button to say that you want to create a new crate instead remove it… something with “if”:

  
local spawnCrate  
local canSpawn = true  
local buttonCanSpawn  
local allowSpawn  
  
spawnCrate = function(e)  
 if canSpawn == true and e.phase == "ended" then  
 -- Code to spawn the crates  
 canSpawn = false  
 end  
  
end  
  
-- function of "buttonCanSpawn"  
allowSpawn = function(e)  
 if e.phase == "ended" then  
 canSpawn = true  
 end  
end  
-- Create the button to allow spawn  
buttonCanSpawn = display.newImageRect( "happyButton.png", 70, 50 )  
buttonCanSpawn.x = 370  
buttonCanSpawn.y = 70  
buttonCanSpawn:addEventListener( "touch", allowSpawn )  

dont know if you want this ^^ [import]uid: 23063 topic_id: 11832 reply_id: 43236[/import]

Hey, this code looks exactly like what I’m looking for, but I don’t exactly understand it all. Would you mind going through it and maybe adding more comments. I don’t understand it and I definitely don’t understand the button bit… It would probably be better as a function…

Basically I don’t see the remove part anywhere, I am kind of new to this so thanks for bearing with me.

Thanks though! I look forward to your ideas :slight_smile: [import]uid: 68047 topic_id: 11832 reply_id: 43238[/import]

well first to dont have complications you must create a “invisible square”, that you will use to touch and create the crates… this is to not use “Runtime:addEventListener( “touch”, spawnCrates )”
for dont make a mess with the allow function

let’s try:

local invSquare   
local spawnCrates   
local canSpawn = true  
local allowSpawn -- function for buttonAllow  
local buttonAllow -- button that you need to touch to allow of spawn crates  
invSquare = display.newRect( 240, 240, 300, 300 ) -- x, y, width, height  
invSquare.alpha = 0.01 -- change this to 1 so you can see the square size and apply X, Y, width, height as you wish  
  
-------- to now we have create the invisible square to catch the touch for call spawnCrate function  
spawnCrates = function(e)  
 if canSpawn == true and e.phase == "ended" then  
 -- i dont know how is your code to spawn the crates but you should put here  
  
 canSpawn = false -- you can only create 1 crate per time.. you will need to click again on button that allow you to create a another crate... we will call this button of "buttonAllow"  
  
 end  
end  
  
-- well... we to now we have add a invisible square and the spawnFunction, where the crates will be spawned inside her size (size of invisible square)  
allowSpawn = function(e) -- when you press in buttonAllow, this function will be called and we will allow the spawnCrates function... so spawnCrates will be allowed to create a new crate where you touch.  
 if e.phase == "ended" then  
 canSpawn = true  
 end  
end  
-- now we need to create a button to use the allowSpawn function.. YOU MUST CREATE THIS BUTTON OUTSIDE invSquare SIZE.. its like... your invSquare size ended in x = 360, your button must be \> than 360  
  
buttonAllow = display.newImageRect( "youImageHere.png", 70, 50 ) -- 70 = width, 50 = height  
buttonAllow.x = 450 -- see, must be \> than 360  
buttonAllow.y = 70  
  
-- now we need to add the listeners  
invSquare:addEventListener( "touch", spawnCrates )  
buttonAllow:addEventListener( "touch", allowSpawn )  
  

well i guess is this
sorry if I made ??a mistake in English :X [import]uid: 23063 topic_id: 11832 reply_id: 43250[/import]

sorry i have forget the remove function @_@

[code]

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

crate:addEventListener( “touch”, removeBox )

[/code] [import]uid: 23063 topic_id: 11832 reply_id: 43251[/import]

Hi. I’m still having some trouble with this one. Could someone help me?
[lua]function blueRemoveSelf (e)
if “ended” == e.phase then
e.target:removeSelf()
end
end

function blue()
blue = display.newImage( “blue.png” )
end

timer.performWithDelay( 0, blue, 1 )

function blueTouch(event)
if event.phase == “began” then
score = score + 1
scoreText.text = score
if event.phase == “ended” then
blue:blueRemoveSelf(e)
end
end[/lua]

On touch, the blue adds 1 to the score. I’ve added the event listener, etc. Now I just need to get rid of the object on touch! Oh, if it’s possible, I’d like to replace it with a sprite or a moving gif, but that’s another story. [import]uid: 25216 topic_id: 11832 reply_id: 44720[/import]

Maybe this will help you:
[lua]local scoreText=display.newText(“0”,20,50,nil,18)
local score=0
–[[
function blueRemoveSelf (e)
–if “ended” == e.phase then
e.target:removeSelf()
– end
end

]]–

function blue()
–local blueBtn
blueBtn= display.newImageRect( “blue1.png”,32,32)
blueBtn.alpha=0
transition.to(blueBtn,{time=1000,alpha=1})
blueBtn.x=60;blueBtn.y=100
return blueBtn
end

one_button=blue()

–timer.performWithDelay( 0, blue, 1 )

local function blueTouch(event)
if event.phase == “began” then
score = score + 1
scoreText.text = score

else
if (“ended”==event.phase) then
event.target:removeSelf()
end

end
end

one_button:addEventListener(“touch”,blueTouch)[/lua] [import]uid: 65047 topic_id: 11832 reply_id: 44749[/import]

I believe a relatively easy fix would be to “return true” from inside the remove crate function as the last thing you do.
[lua]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
return true
end
crate:addEventListener(“touch”, rmvCrate)
end[/lua]
Not sure if that’s your rmvCrate function or not, but returning true will eat the tap, and not let it pass to layers further behind. This will give you the functionality you’re after I think. [import]uid: 75335 topic_id: 11832 reply_id: 44913[/import]