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!