Sprite subclass destructor not destroying touch events

Hi, this is what I do:

1- I subclass Sprite to my_awesome_sprite to add some properties and touch events.
2- I create a new sprite:

auto super_sprite = my_awesome_sprite::create();

Then I do some other stuff, and then I ended up not using the super_sprite (so no added as a child to node/layer etc…). Then the function ends and super_sprite is destroyed by the system (I added a log on the destructor and got confirmation)

So, the object has been destroyed right? Right. But not its touch events… The touch event still triggers even the object no longer exists and this causes a error and crash.

So my question is: do I have to destroy the touch events myself in the destructor? how can I do that?

Thanks!

I wrote this a long while back, but it should help

http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners

Hi, yes I tried this code some time ago but does not compile… there are some errors.

Still my issue is not creating the touch events… the problem is they are not destroyed when the object is destroyed. Is that a normal behaviour? Do I have to take care of them myself? How can I do it? How can I destroy the events?

Thanks,

Yes, I wrote it some time ago…

to be safe you can always remove the events yourself. When I destroy an object they go away for me…

http://cocos2d-x.org/docs/programmers-guide/8/index.html#removing-events-from-the-dispatcher

Use ui::ImageView instead? It wraps up all that jazz for you :smiley:

1 Like

Thanks!! I believe this may work. I will read it.

I guess if the object is properly attached to another cocos object and later removed this wont happen right? Maybe this happens only because I didnt add it as a child to a scene or another cocos node?

I will do some tests and post my results. Thanks!

Interesting idea. I haven’t thought of this.

I found another thread about this:

Someone mentions this: “if you add it with fixed priority, then you will need to remove the event listener manually.” Actually this is what I’m doing… I use “addEventListenerWithFixedPriority”

So thats why I have to remove the listener myself (I did it and it works fine now). Is this behaviour explained anywhere in the documentation?

I just read the API http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d2/d1b/classcocos2d_1_1_event_dispatcher.html#ae5877239d4ae52648513ea729c4b2b60

And there is no information about it… do you know where is this explained in detail?

Thanks!

I don’t know if this is explained in detail anyplace. I will add something to the docs.

I will try. Thanks! :smile: