Removing Objects on Touch with Corona SDK

Welcome to another post by That’s So Panda! In today’s post, we’ll be learning how to remove objects by touching them with Corona SDK. I’ve seen a lot posts in the forum where developers just starting with Corona SDK asking how to remove objects based on a touch event. So, I’ve decided to write a post about it!

There are a couple of different ways to remove an on-screen object using a touch event and I’m just going to be covering one way to accomplish it. If you know or have seen of a better way, please let me know in the comments.

Just as a personal preference, I’m going to start including on-screen text that states what the code is supposed to do. We can add text by using Corona’s display.newText api.

sometext = display.newText("Removing Objects on Touch",0,0,native.systemFont, 16)
sometext.x = display.contentWidth*0.5
sometext.y = display.contentHeight - 16

Now that we know what the app is supposed to do, we need to write our object removal function. We do this by writing a function that accepts two parameters – self and event. The self parameter will be the object passed into the function and the event will tell us what phase the event is in.

function onCircleTouch(self, event)
  -- Insert code here
end

Now that we have written our function, we will write the code to remove the object. We can remove an object in Corona SDK by using the command objectName:removeSelf(). In our case, we are passing the self parameter into the function, so we’ll use self:removeSelf().

One catch with Corona is that you should never remove an object in the middle of a touch detection function. So, we get by this by wrapping the command self:removeSelf() inside of a timer with a delay of 1 millisecond.

function onCircleTouch(self, event)
 if(event.phase == "began") then
  timer.performWithDelay(1, function() self:removeSelf() end )
 end
end

Finally, we want want to make sure our function returns true to let our app know that the touch event has successfully executed.

function onCircleTouch(self, event)
 if(event.phase == "began") then
  timer.performWithDelay(1, function() self:removeSelf() end )
 end
return true
end

Now that we written our touch event, we can actually create our object (which is a circle) and set up the event listener. We accomplish this by pointing the .touch property to the function and adding the touch event listener to the object.

circle = display.newCircle(160,240,50)
circle.touch = onCircleTouch
circle:addEventListener("touch", circle)

And that’s it! You have just learned how to remove an object when it’s touched! Pretty easy, right? Just to make it even easier for you, here’s the full code for the object removal with Corona SDK.

sometext = display.newText("Removing Objects on Touch",0,0,native.systemFont, 16)
sometext.x = display.contentWidth*0.5
sometext.y = display.contentHeight - 16

function onCircleTouch(self, event)
 if(event.phase == "began") then
  timer.performWithDelay(1, function() self:removeSelf() end )
 end
return true
end

circle = display.newCircle(160,240,50)
circle.touch = onCircleTouch
circle:addEventListener("touch", circle)

Thanks for reading! If you have questions or comments, please let me know in the comments below.


2 Comments
  1. Great tutorial, really helped me out a lot with a code I’m working on, could the function use a self = nil
    Because I read everywhere for memory consumption it’s always a good idea to nil stuff you don’t need anymore.

    • Yes, nil out objects when they aren’t used. For small projects like this, it’s not that big of a deal, but it’s a good practice to follow.

Leave a Reply