Best way to "deactivate" a Display Object?

This question is mostly aimed at the Corona Staff as they’re the only ones who really know what goes under the hood and can give me a proper answer.

I was wondering what’s the best way to “deactivate” a Display Object without removing it completely. In other words, I would like to keep the object in memory, but tell Corona to do absolutely nothing with it for now, aka not draw it or give it ANY cpu time. At least until I activate it again.

Currently I am doing this:

[lua]object.isVisible = false

timer.performWithDelay(5, function() object.isBodyActive = false end)  – Assuming it has a physical body. The timer is there since disabling the body immediately sometimes causes errors, especially during collisions

display.getCurrentStage():insert(object)  – Remove it from its parent group, if it has one[/lua]

Then when I want to activate it again I do this:

[lua]object.isVisible = true

timer.performWithDelay(10, function() object.isBodyActive = true end)

parentGroup:insert(object)[/lua]

Am I doing it right, or is there a better way? And if I am, are all 3 actions necessary?

P.S.

It would be nice if Corona simply had a object:deactivate() method

Move it off screen and remove any listeners attached to it. But I’m not staff

Hi @sportyspice,

As @jstrahan suggest, I would move the object off screen, and of course make sure there are no animations happening on it (sprite OR transition). Additionally, you should remove all collision listeners from it, and if you want absolutely no attention given to the object at all in regards to the physics engine, I would suggest removing the physics body entirely (unless re-adding it when you reactive the object would be a nuisance).

Hope this help,

Brent

Thanks for the replies.

Re-adding  the physics body is not a nuisance code wise, it’s actually very simple. But wont removing/re-adding it constantly take more cpu time than just keeping it? Some of my objects are being deactivated and then reactivated very quickly, I’m talking about an interval of just a few frames. Others take longer.

Hi @sportyspice,

Yes, in this case you should retain the physics bodies. The performance hit will be greater by re-adding them versus any potential (and tiny) performance hit that the physics engine might take in considering non-active established objects.

Brent

Alright, thanks Brent.

I have one last question regarding Display Objects and performance.

The next 2 code snippets should take the exact same time to execute (we’re talking about 1/1000000 seconds difference probably)

[lua]someVariable = 5[/lua]

[lua]someVariable = 22

someVariable = 5[/lua]

My question is: Is this also true for Display Objects?

Some examples:

[lua]object.x = 5 [/lua]

vs

[lua]object.x = 22

object.x = 5[/lua]

[lua]object:translate(30,30)[/lua]

vs

[lua]object:translate(5,20)

object:translate(25,10)[/lua]

[lua]object.alpha = object.alpha * 0.25[/lua]

vs

[lua]object.alpha = object.alpha * 0.5

object.alpha = object.alpha * 0.5[/lua]

And so on… I’m sure you get the idea.

Well, if you can do the same thing in one call instead of two, obviously that’s better. But realistically, the only way this might be noticeable is if you did this multi-change thing like 10,000 times in a gameplay-critical loop. So, I wouldn’t worry about it. There are better places to focus your performance-tweaking efforts. :slight_smile:

Brent

Move it off screen and remove any listeners attached to it. But I’m not staff

Hi @sportyspice,

As @jstrahan suggest, I would move the object off screen, and of course make sure there are no animations happening on it (sprite OR transition). Additionally, you should remove all collision listeners from it, and if you want absolutely no attention given to the object at all in regards to the physics engine, I would suggest removing the physics body entirely (unless re-adding it when you reactive the object would be a nuisance).

Hope this help,

Brent

Thanks for the replies.

Re-adding  the physics body is not a nuisance code wise, it’s actually very simple. But wont removing/re-adding it constantly take more cpu time than just keeping it? Some of my objects are being deactivated and then reactivated very quickly, I’m talking about an interval of just a few frames. Others take longer.

Hi @sportyspice,

Yes, in this case you should retain the physics bodies. The performance hit will be greater by re-adding them versus any potential (and tiny) performance hit that the physics engine might take in considering non-active established objects.

Brent

Alright, thanks Brent.

I have one last question regarding Display Objects and performance.

The next 2 code snippets should take the exact same time to execute (we’re talking about 1/1000000 seconds difference probably)

[lua]someVariable = 5[/lua]

[lua]someVariable = 22

someVariable = 5[/lua]

My question is: Is this also true for Display Objects?

Some examples:

[lua]object.x = 5 [/lua]

vs

[lua]object.x = 22

object.x = 5[/lua]

[lua]object:translate(30,30)[/lua]

vs

[lua]object:translate(5,20)

object:translate(25,10)[/lua]

[lua]object.alpha = object.alpha * 0.25[/lua]

vs

[lua]object.alpha = object.alpha * 0.5

object.alpha = object.alpha * 0.5[/lua]

And so on… I’m sure you get the idea.

Well, if you can do the same thing in one call instead of two, obviously that’s better. But realistically, the only way this might be noticeable is if you did this multi-change thing like 10,000 times in a gameplay-critical loop. So, I wouldn’t worry about it. There are better places to focus your performance-tweaking efforts. :slight_smile:

Brent