Jump to content

RemoveListener for Spine animations


Outfire
 Share

Recommended Posts

RemoveListener is works, but only in one way:

let spine // our spine animation
let log = () => console.log('nonsense');
let listener = {complete: log};

spine.state.addListener(listener);

And removes it like

spine.state.removeListener(listener);

It does not work if i do it like this:

let spine //our spine animation
let log = () => console.log('nonsense');

spine.state.addListener({complete: log});
// trying to remove
spine.state.addListener(log);
// or just like this
spine.state.addListener({complete: log});

Works fine if add and remove only the same object, but its long way.
Is there another way to do this?

 

image.png

Link to comment
Share on other sites

I like the idea.

This is the part in pixi-spine: https://github.com/pixijs/pixi-spine/blob/master/src/core/AnimationState.ts#L561

This is upstream, spine-ts: https://github.com/EsotericSoftware/spine-runtimes/blob/3.6/spine-ts/core/src/AnimationState.ts#L561

You can hack pixi-spine:

PIXI.spine.core.AnimationState.prototype.removeListener = function (...) { }

Please make PR or at least give me the code with the behaviour you want. I'm all hands for enhancing pixi-spine, we already have some things that distinguish us from spine-ts.

Link to comment
Share on other sites

Need some time to think about it.


I think about "spine.removeListener({complete: log})"
But we can add "log" twice or more in one "complete", and then our removeListener must remove all "log"s in "complete" or just the last one.


Maybe it is possible to do like in DOM addEventListener, when you add new listener on the same event with the same listener name it removes old one and push new one instead of it .

Link to comment
Share on other sites

Yes, it is my problem if I  forget that I already have listener with the same name.

I think variant - "spine.removeListener({complete: 'log'})" (event name and listener name) - is the best way and pretty obvious one.

And "spine.removeListener({complete: ['log', 'update'], start: 'playSound'})" to remove multiple events. 

Perhaps is it possible to upgrade  clearListeners function , so it takes event name as an argument and as result removes all listeners subscribed to this particular event?

Link to comment
Share on other sites

  • 3 weeks later...

I considered on how  to implement function to remove listeners, and here is the result.

The following code can be used to remove function from particular listener.

PIXI.spine.core.AnimationState.prototype.removeListenersByName = function (findListener) {
    this.listeners.forEach( listener => {
        for(let currentListenerName in listener) {
            for(let findListenerName in findListener) {
                if (currentListenerName === findListenerName 
                && listener[currentListenerName].name === findListener[findListenerName]) {
                    if(Object.keys(listener).length === 1) {
                        this.removeListener(listener);
                    } else {
                        delete listener[currentListenerName]
                    }
                }
            }
        }
    })
}

spine.addListener({ complete: foo, end: bar })

removeListenersByName({ complete: 'foo' })
removeListenersByName({ complete: 'foo', end: 'bar' })

But this solution has one problem.

spine.addListener({ complete: foo.bind(this)})

Then  listeners[0].complete.name will be 'bound foo', not 'foo'.This problem can be solved in the following way.

PIXI.spine.core.AnimationState.prototype.isBounded = function (listenerName, findName) {
    let splited = listenerName.split(' ');

    if (splited.length === 1) {
        return false;
    }

    return splited[0] === 'bound' && splited[1] === findName
}

PIXI.spine.core.AnimationState.prototype.removeListenersByName = function (findListener) {
    this.listeners.forEach( listener => {
        for(let currentListenerName in listener) {
            for(let findListenerName in findListener) {
                if (currentListenerName === findListenerName) {
                    let currentName = listener[currentListenerName].name;
                    let findName = findListener[findListenerName];

                    if (this.isBounded(currentName, findName)) {
                        findName = 'bound ' + findName;
                    }

                    if (currentName === findName) {
                        if(Object.keys(listener).length === 1) {
                            this.removeListener(listener);
                        } else {
                            delete listener[currentListenerName]
                        }
                    }
                }
            }
        }
    })
};

Some other issue can appear.

spine.addListener({complete: () => console.log('something') });


Here listeners[0].complete.name will be 'complete', as the name of the listener, to which function it was bound.
But it's just information.

To remove all functions from particular listener.

PIXI.spine.core.AnimationState.prototype.clearListenersByName = function (listenerName) {
    this.listeners.forEach( listener => {
        for(let key in listener) {
            if (key === listenerName) {
                if(Object.keys(listener).length === 1) {
                    this.removeListener(listener);
                } else {
                    delete listener[key]
                }
            }
        }
    })
}

spine.addListener({ complete: foo, end: bar })
spine.addListener({ complete: foo})

clearListenersByName('complete')

That is the result of my small expierence in js.:ph34r:

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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