Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Remove object on touch - Gideros Forum

Remove object on touch

SPDAppsSPDApps Member
edited January 2012 in Suggestions & requests
hi im new to this could someone tell me how to remove a box2d object on touch thanks

Likes: Karriz

Dislikes: DarkPixel, sdouglas

Tagged:
+1 -1 (+1 / -2 ) Share on Facebook

Comments

  • atilimatilim Maintainer
    Hi,

    You need:
    1. destroy body with b2.World:destroyBody and
    2. remove the sprite from stage with Sprite:removeChild

    Dislikes: Yan

    +1 -1 (+0 / -1 ) Share on Facebook
  • Ooooh thanks
  • um i cant figure out how to do it could you show me a code example?
  •  
    stage:addEventListener(Event.TOUCHES_BEGIN , onTouchesBegin)
     
    function onTouchesBegin(event)
    	world:destroyBody(yourObjectBody)
    	stage:removeChild(yourObjectSprite)
    end
    add event listner to the stage...

    then in onTouchesBegin function (fired only when you touch the screen of you device)
    remove the bodyobject and the relative sprite...

    hope it helps.

    Ciao Gianluca.

    Likes: Karriz

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
    +1 -1 (+1 / -0 ) Share on Facebook
  • atilimatilim Maintainer
    Open "Sleeping bodies" example at Physics section and add these to the end of main.lua:
    local function onMouseDown(event)
    	for i=1,stage:getNumChildren() do
    		local sprite = stage:getChildAt(i)
    		local body = sprite.body
    		if sprite:hitTestPoint(event.x, event.y) then
    			stage:removeChild(sprite)
    			world:destroyBody(body)
    			break
    		end
    	end
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
  • yes... atilim example is much better... :P

    because"Event.TOUCHES_BEGIN" work only on device not in the pc... ;;)

    ciao ciao

    Gianluca.

    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
  • atilimatilim Maintainer
    edited January 2012
  • thanks guys [-O<
  • Hi all. Atilim, I put this
    "local function onMouseDown(event)
    for i=1,stage:getNumChildren() do
    local sprite = stage:getChildAt(i)
    local body = sprite.body
    if sprite:hitTestPoint(event.x, event.y) then
    stage:removeChild(sprite)
    world:destroyBody(body)
    break
    end
    end
    end

    stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)"

    to Sleeping Bodies as you said. It doesn't work and shows the following message:
    "main.lua:90: bad argument #1 to 'destroyBody' (b2Body expected, got nil)
    stack traceback: main.lua:90: in function "

    What could be wrong here? I stuck with the same think in my project.
    Thanks.
  • @paulseau it seems not all stage elements has bodies, so you would also need to check for body, as
    local function onMouseDown(event)
    	for i=1,stage:getNumChildren() do
    		local sprite = stage:getChildAt(i)
    		local body = sprite.body
    		if sprite:hitTestPoint(event.x, event.y) and body then
    			stage:removeChild(sprite)
    			world:destroyBody(body)
    			break
    		end
    	end
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
Sign In or Register to comment.