Check displayObj's alive

 Hi everyone.

 I wonder if Corona has a function that checks if a displayed object is alive or not.

 Sample: I create a ship, and a timer to make it fire. But it can be destroyed by other ships’ fires. So how can I check if the ship is still alive in my timer call? I can save timer id to each ship, then cancel it when a ship is being destroyed. But I want to try another way.

 And one more question, why doesn’t Corona auto cancel object transition when it’s destroyed? Because not every obj is destroyed with removeSelf function, so I can’t check to stop its transition. In my sample, if my ship is a group, with a light having transition. The transition has a complete function to recall itself. When I remove my ship, this light is removed as well, but it doesn’t call its removeSelf function. It will have an error when light transition recalls it.

 Now I must save all ship transId to cancel them. If I have a method to check if object’s alive, I can use that in transition complete function to avoid errors. And it would be great if Corona auto cancel ( or doesn’t call transition complete ) object transition if it’s destroyed.

To check if a character (player, ship, asteroid, anything) is “alive” you should have a variable on it, maybe called “.isAlive” and set it to true.

When performing transitions you will need to check the .isAlive and cancel the transition if you want to remove the object. To do that you should set the .isAlive to false and then call transition.cancel( myShip.transitiionId ) - when you create the transition store its ID on the ship with myShip.transitionId = transition.to( myShip, { … } )

For Corona to “auto cancel” would be a lot of work on their side. Work which is not very difficult for us to do. It is proper for us to do that work as it is game logic, not SDK logic.

 Thank horacebury. 

 But it seems that you didn’t fully get my question.

 I can save timer id to each ship, then cancel it when a ship is being destroyed. But I want to try another way.

 I know that I can save transition Id and cancel it :).  And about your answer, please read my sample again. Of course, I can set isAlive to false when I remove objects. But what if I remove its parent instead? I must check all its children, set isAlive to false, and repeat with its children’s children v.v.v.

 I just want another simple way to make transition ( or timer ) check if its object is alive or not.

When the onComplete listener triggers, a parameter is passed to it which holds a reference to the object it’s related to. You can then check that the object still exists. I’m having a hard time visualizing your exact use case, but you can probably do something like

transition.to(..., onComplete = function(obj) if(obj) then someFunctionThatRetriggersTheTransition() end end)

 Thanks. But the problem is when obj’s removed, its reference still be not nil, but that will be error if you do something with it :frowning: . So obj is not nil, but someFunctionThatRetriggersTheTransition may have error 

 I want to use your code, if Corona has a isAlive function like that:

transition.to(..., onComplete = function(obj) if( obj.isAlive() ) then someFunctionThatRetriggersTheTransition() end end)

If you want to create a collection of transitions all related to a particular object or parent object you could use transition tags and simply cancel the whole list of transitions by naming all their tags:

http://docs.coronalabs.com/daily/api/library/transition/to.html#params.tag-optional

 Thank horacebury again.

 I just want to get rid of transition.cancel, and let itself check if its object’s alive. Creating check function when creating transition is more simple than remembering to check and cancel transition everytime something is removed.

 btw, how about timer? How can I avoid errors when timer calls if obj is removed?

Timer doesn’t have tags, unfortunately, but it should not be difficult to implement a list of timer IDs. You can add your own functions to the timer library to help provide that.

Take a look at the finalize event: http://docs.coronalabs.com/daily/api/event/finalize/index.html

It allows you to listener for display objects being removed from the system. From there you should be able to create some code which can be added to any of your display objects to listen for being removed and then cancel any transitions or timers. The only gotcha is that if you remove a parent group the finalize of child objects are not fired, so you would need each child to know which parent object to listen to and then clean itself up. This is also good Object Oriented practice.

I usually check for the removeSelf function to check whether a display object’s dead.

[lua]

transition.to(killerFrog,{x=victim.x,y=victim.y,onComplete=function(obj)

  if not obj.removeSelf then

    return

  end

  obj:eat(victim)

})

[/lua]

That’s a bad example, but the first thing that came to my mind

Oh, nice. Thank you. 
I have been using checking obj._proxy with the same effect :slight_smile: . Just wanna know if Corona has an official method to do that.
Bwt, thank Rob for posting this topic on Corona Blog, so everyone can see and share their ideas.

To check if a character (player, ship, asteroid, anything) is “alive” you should have a variable on it, maybe called “.isAlive” and set it to true.

When performing transitions you will need to check the .isAlive and cancel the transition if you want to remove the object. To do that you should set the .isAlive to false and then call transition.cancel( myShip.transitiionId ) - when you create the transition store its ID on the ship with myShip.transitionId = transition.to( myShip, { … } )

For Corona to “auto cancel” would be a lot of work on their side. Work which is not very difficult for us to do. It is proper for us to do that work as it is game logic, not SDK logic.

 Thank horacebury. 

 But it seems that you didn’t fully get my question.

 I can save timer id to each ship, then cancel it when a ship is being destroyed. But I want to try another way.

 I know that I can save transition Id and cancel it :).  And about your answer, please read my sample again. Of course, I can set isAlive to false when I remove objects. But what if I remove its parent instead? I must check all its children, set isAlive to false, and repeat with its children’s children v.v.v.

 I just want another simple way to make transition ( or timer ) check if its object is alive or not.

When the onComplete listener triggers, a parameter is passed to it which holds a reference to the object it’s related to. You can then check that the object still exists. I’m having a hard time visualizing your exact use case, but you can probably do something like

transition.to(..., onComplete = function(obj) if(obj) then someFunctionThatRetriggersTheTransition() end end)

 Thanks. But the problem is when obj’s removed, its reference still be not nil, but that will be error if you do something with it :frowning: . So obj is not nil, but someFunctionThatRetriggersTheTransition may have error 

 I want to use your code, if Corona has a isAlive function like that:

transition.to(..., onComplete = function(obj) if( obj.isAlive() ) then someFunctionThatRetriggersTheTransition() end end)

If you want to create a collection of transitions all related to a particular object or parent object you could use transition tags and simply cancel the whole list of transitions by naming all their tags:

http://docs.coronalabs.com/daily/api/library/transition/to.html#params.tag-optional

 Thank horacebury again.

 I just want to get rid of transition.cancel, and let itself check if its object’s alive. Creating check function when creating transition is more simple than remembering to check and cancel transition everytime something is removed.

 btw, how about timer? How can I avoid errors when timer calls if obj is removed?

Timer doesn’t have tags, unfortunately, but it should not be difficult to implement a list of timer IDs. You can add your own functions to the timer library to help provide that.

Take a look at the finalize event: http://docs.coronalabs.com/daily/api/event/finalize/index.html

It allows you to listener for display objects being removed from the system. From there you should be able to create some code which can be added to any of your display objects to listen for being removed and then cancel any transitions or timers. The only gotcha is that if you remove a parent group the finalize of child objects are not fired, so you would need each child to know which parent object to listen to and then clean itself up. This is also good Object Oriented practice.

I usually check for the removeSelf function to check whether a display object’s dead.

[lua]

transition.to(killerFrog,{x=victim.x,y=victim.y,onComplete=function(obj)

  if not obj.removeSelf then

    return

  end

  obj:eat(victim)

})

[/lua]

That’s a bad example, but the first thing that came to my mind

Oh, nice. Thank you. 
I have been using checking obj._proxy with the same effect :slight_smile: . Just wanna know if Corona has an official method to do that.
Bwt, thank Rob for posting this topic on Corona Blog, so everyone can see and share their ideas.