Jump to content

Destroy object at the time when the sprite asociated is deleted


sebamawa
 Share

Recommended Posts

Hello friends,

First, as usual, excuse me for my english,

 

I need destroy a object (from a class) at the time when the sprite asociated (property of the object) is delete (with kill())

 

How I can do?

 

Example:

 



var obj = new ObjClass(); //obj.objectSprite is a property (a Sprite)

function update(){

game.physics.aracade.collide(obj.objectSprite, otherSprite, destroyObject, null);
}

function destroyObject(){

obj.objectSprite.kill();
//Here, I want to destroy the object that has 'this.objectSprite' as property (or attribute)
//The natural solution is do obj = null
}

Link to comment
Share on other sites

Hi, the collision calback can receive the two object that collide, so...

var obj = new ObjClass();  //obj.objectSprite is a property (a Sprite)function update(){     game.physics.aracade.collide(obj.objectSprite, otherSprite, destroyObject, null);}//Receive 2 params (Sprites)function destroyObject(firstSprite, secondSprite){     firstSprite.kill();     secondSprite.kill();}

Hope that helps!

Link to comment
Share on other sites

You could make your ObjClass extend Sprite and then override the default kill to include the destruction of the class object.

Your real problem is that you probably want to destroy references to the ObjClass object and those are in a different scope entirely.

I try to avoid these situations, but when they happen I use a kill flag in the ObjClass, which tells it's owner to kill it.

 

So if you have something like a tank which has a child turret, and for some reason the turret can get destroyed without telling the tank to blow it up... you can add a kill flag to the turret, and next time the tank tries to do anything with the turret you first check the kill flag.  If it's true, you know the turret is destroyed (the graphic has been removed already) so you can then call the turret.destroy() using the tank's own reference to it.

 

Hope that makes sense!

Link to comment
Share on other sites

You could make your ObjClass extend Sprite and then override the default kill to include the destruction of the class object.

Your real problem is that you probably want to destroy references to the ObjClass object and those are in a different scope entirely.

 

Exactly InsaneHero,

 

Yes, best idea is to extend the Sprite class.

 

Thanks friends, this is a good comunity.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...