OnComplete and display object removal

I’m wondering whether I need to remove temporary display objects I show on screen as scores.

I’m using this code:

[lua] local msg = display.newText( “+” … addScore, 0, 0, nil, 12 )
msg:setTextColor( 255, 255, 136, 255 )
local msggroup = display.newGroup()
msggroup:insert( msg, true )
msggroup:translate( enemyList[enemyID].x, enemyList[enemyID].y )
transition.to(msg, {time=3000, alpha=0})[/lua]

Should I remove these objects even though they’ve been turned invisible? I’m reposting these temporary score numbers every few seconds.

If they should be removed, what would be the appropriate way?

[lua]transition.to(msg, {time=3000, alpha=0, onComplete=remove:self()}[/lua]

Thanks. [import]uid: 1560 topic_id: 2123 reply_id: 302123[/import]

Also, is there a test to determine whether an object exists? I’m getting a few errors from trying to remove an object that’s already been removed. [import]uid: 1560 topic_id: 2123 reply_id: 6349[/import]

Turning an object invisible (setting alpha to 0) doesn’t free up the memory used by the object. You need to object.removeSelf() to free up the memory.

If you set an object to nil after removing it, you can check for nil later to detect if it’s been removed. (You don’t need to set the object to nil after freeing the object to remove the texture memory used by the object.)

-Tom [import]uid: 7559 topic_id: 2123 reply_id: 6354[/import]

Thanks, so is
[lua]transition.to(msg, {time=3000, alpha=0, onComplete=remove:self()}[/lua]
the right way to do that?

It seems I can’t put object.removeSelf() in a separate line after the transition because it will remove the object before the transition is complete. [import]uid: 1560 topic_id: 2123 reply_id: 6355[/import]

You can remove the object after the transition using:
transition.to( msg, {time = 3000, alpha = 0}, function() msg.removeSelf() end )

All display and timer functions happen after the “chunk” executes so any lines after the transition.to will happen before the transition starts. If you need something delayed you would use another transition.to or a timer.performWithDelay method and with the onComplete set to the method you want to execute after the delay.

-Tom [import]uid: 7559 topic_id: 2123 reply_id: 6365[/import]

Sorry Noob here. First post!

I’m trying the above code to display a score message and not finding much success. If I set the finish alpha on the transition to something like .75 the message is never destroyed. Is there something I’m missing?

[code]
function dispDestroyedEnemy( xPos, yPos, addScore)

local tempScore = ui.newLabel{
bounds = { display.contentWidth - 300, 10, 100, 24 }, – align label with right side of current screen
text = “” … addScore,
font = “Trebuchet-BoldItalic”,
textColor = { 255, 225, 102, 255 },
size = 32,
align = “right”
}
tempScore.x = xPos
tempScore.y = yPos

transition.to( scoreDisplay, {time = 3000, alpha = 0.0}, function() scoreDisplay.removeSelf() end )
end
[/code] [import]uid: 5641 topic_id: 2123 reply_id: 6373[/import]

Hi bdpluim,

i use this simple function for destroy the object

local function DestroyObj(Obj)  
 Obj:removeSelf()  
end  
  
transition.to( scoreDisplay, {time = 3000, alpha = 0, onComplete=DestroyObj(scoredisplay)})  

This works 100%

Marco

[import]uid: 7518 topic_id: 2123 reply_id: 6375[/import]

This doesn’t work for me. Here’s my code:

local function killCirle(t)  
 t:removeSelf()  
end  
  
...  
  
transition.to( t, { alpha=0.25, time=500, onComplete=killCirle(t) } )  

The object immediately disappears without running the transition.

I’ve also tried this:

transition.to( t, { alpha=0.25, time=500}, function() t.removeSelf() end )  

…the transition works (it fades) but does not get removed.

Any ideas? [import]uid: 8353 topic_id: 2123 reply_id: 6629[/import]

I should probably mention that this code is in an onTouch event and t is a local event target. Is that the problem? (falling out of scope) [import]uid: 8353 topic_id: 2123 reply_id: 6630[/import]

Hi, try this code

local function RemoveObj(self)  
 local obj=self  
 obj:removeSelf()  
end  
  
transition.to(obj,{time=100, alpha=0, onComplete=RemoveObj})  

Marco
[import]uid: 7518 topic_id: 2123 reply_id: 6668[/import]

By the way Marco I never got to thank you. Your code worked beautifully [import]uid: 5641 topic_id: 2123 reply_id: 7059[/import]

I’ve tried all the above functions and ain all of them the effect is removed before it get animated/transition.

  
function RemoveObj(self)  
 local obj=self  
 print(obj)  
 obj:removeSelf()  
end  
  
transition.to(bollHitt, {time=2600, alpha=0.5, xScale=2, yScale=2, transition=easing.linear, onComplete=RemoveObj(bollHitt)})  
  

The print print(obj) is shown in the terminal as fast as the animation is triggered. Even if i set delay:30000 in the transition the function is triggered when the transition is…
[import]uid: 28895 topic_id: 2123 reply_id: 21917[/import]

For the onComplete parameter, you can’t give an parameter to the function name. It always have to be without parentesis:

[lua]transition.to(bollHitt, {time=2600, alpha=0.5, xScale=2, yScale=2, transition=easing.linear, onComplete=RemoveObj})[/lua] [import]uid: 5712 topic_id: 2123 reply_id: 21923[/import]

Thanks, i see.

But how do i then send the right object to the remove function?

  
-- Raderar objekt  
function RemoveObj(self)  
 local obj=self  
 obj:removeSelf()  
end  
  
-- Visar en anstöt kraft  
function hitForce(xIn, yIn)  
  
 -- Boll träff  
 local bollHitt = display.newImage( "bollTraff.png", true )  
 bollHitt.x = 160  
 bollHitt.y = 160  
 bollHitt.xScale = 0.2  
 bollHitt.yScale = 0.2  
 bollHitt.alpha = 0  
 g:insert( bollHitt )  
  
 -- Visar en markör vart man träffade på bollen  
 bollHitt.alpha = 1  
 bollHitt.xScale = 0.1  
 bollHitt.yScale = 0.1  
  
 bollHitt.x = xIn  
 bollHitt.y = yIn  
  
 transition.to( bollHitt, {time = 600, alpha = 0.5, xScale=2, yScale=2, transition=easing.linear, onComplete=RemoveObj})  
  
end  
  

[import]uid: 28895 topic_id: 2123 reply_id: 21932[/import]

that code will remove the BollHit on completion of the transition [import]uid: 6645 topic_id: 2123 reply_id: 21938[/import]

It did not the firt 100 times i tried but now it does. Strange. Must have made some changes. :slight_smile:

Tanks any how.

Another question

function RemoveObj(self)  
 local obj=self  
 obj:removeSelf()  
 obj = nil  
end  

If i don’t put obj = nil obj still have a table reference “number”. Does that acclaimer memory? [import]uid: 28895 topic_id: 2123 reply_id: 21941[/import]