Code Q About Creating And Removing An Object On Every Tap

Hello,

Just want to ask if my follow example code looks ok to you, or not. What i want to do is whenever you tap the screen, i want to render a display-object on the x,y position, after which it then automatically fades out. To try and minimize any possible memory leaks, i would like to remove each display-object after it has completely faded out.

Here’s what i have so far (simplified for this post)-

local function tapScreen(event) if event.phase == "began" then --1. create object on each tap local myObject = event.target myObject = display.newSprite( iconsSheet, iconsSequence ); myObject.x = event.x; myObject.y = event.y; myObject:setSequence( "myIcon" ); myObject:play() localGroup:insert(myObject) --2. auto fade out each object myTrans = transition.to(myObject, {time=100, alpha=0, onComplete=removeMyObject}) --3. remove each object afterwards function removeMyObject() display.remove(myObject) --does this actually remove each object? end end screen:addEventListener( "touch", tapScreen)

Does this look ok? Am i inserting and removing each object correctly?

Without the onComplete=removeMyObject and the sub function, visually it works fine on screen (in simulator). This code will be part of a scene, so on scene change at the end, i will then nil the object - i take it i don’t need to add a myObject=nil inside the above sub function?

Finally, one last question about the above code - for the sub function, if i add a “local” in front so that it reads “local function removeMyObject()” the onComplete doesn’t work but do/should i then initially declare this at the start of the scene/file, such as “local removeMyObject = {}”?

Appreciate any feedback, thanks for reading!

  1. Taps don’t use event phases the same way. A tap is a tap, there’s no began/ended. (event.numTaps tells you the # of taps though)

  2. “Touches” work pretty much as you coded.

  3. I’m really not sure why you are trying to overwrite event.target (event.target is usually the object you touched)

  4. You call the right remove command but you do want to nil it afterward unless it’s part of the scene. (It doesn’t hurt to be clean though)

  5. Forward referencing is a good idea. You don’t have to say the variable is anything; just say “local myVariable” or whatever it is.

Here’s how I might write that code:

local function touchScreen(event) if event.phase == "ended" then -- Make the object local balloon = display.newSprite(iconsSheet, iconsSequence) balloon.x = event.x balloon.y = event.y balloon:setSequence("myIcon"); balloon:play() localGroup:insert(balloon) -- Remove the object gracefully balloon.transition = transition.to(balloon, { time = 100, alpha = 0, onComplete = function() display.remove(balloon) balloon = nil end } ) end end screen:addEventListener( "touch", touchScreen)

The basic methodology is:

  1. Be clear on what your objects/variables are

  2. You can use enclosures for quick, self-important stuff

Hi richard9,

Thank you for the reply back and your own solution - interesting and helpful for me, especially the onComplete part with the (sub) function, that is good to know! And yeah, when i said tap, i meant figuratively speaking when you tap the screen on a real device - in the code i was planning on using the touch rather then tap!

Cheers,

  1. Taps don’t use event phases the same way. A tap is a tap, there’s no began/ended. (event.numTaps tells you the # of taps though)

  2. “Touches” work pretty much as you coded.

  3. I’m really not sure why you are trying to overwrite event.target (event.target is usually the object you touched)

  4. You call the right remove command but you do want to nil it afterward unless it’s part of the scene. (It doesn’t hurt to be clean though)

  5. Forward referencing is a good idea. You don’t have to say the variable is anything; just say “local myVariable” or whatever it is.

Here’s how I might write that code:

local function touchScreen(event) if event.phase == "ended" then -- Make the object local balloon = display.newSprite(iconsSheet, iconsSequence) balloon.x = event.x balloon.y = event.y balloon:setSequence("myIcon"); balloon:play() localGroup:insert(balloon) -- Remove the object gracefully balloon.transition = transition.to(balloon, { time = 100, alpha = 0, onComplete = function() display.remove(balloon) balloon = nil end } ) end end screen:addEventListener( "touch", touchScreen)

The basic methodology is:

  1. Be clear on what your objects/variables are

  2. You can use enclosures for quick, self-important stuff

Hi richard9,

Thank you for the reply back and your own solution - interesting and helpful for me, especially the onComplete part with the (sub) function, that is good to know! And yeah, when i said tap, i meant figuratively speaking when you tap the screen on a real device - in the code i was planning on using the touch rather then tap!

Cheers,