• Runtimes
  • [C++ SFML] settting up the animation callback within a class

  • Изменено
Related Discussions
...

Hey guys, picked up Spine just a little while back and still getting the hang of it. Right now I'm trying to set up the animations for a character and I need to use the callback system to track when an animation completes. I took a look at the spine sfml example for this and that's no problem, simple callback function. What I need though is to have a class function be the callback so I can access my class members (or perhaps there's another method of doing things I just haven't encountered yet?). Any advice or help is welcome, this has been bogging me down for a few days now.

The callback gets AnimationState* state which has a void* rendererObject field. You can use this to have context for the event. Eg, you could put your instance in rendererObject, then in the callback cast rendererObject to your instance and call a method on in.

A similar approach can be used when setting a callback for spTrackEntry. If the spTrackEntry owns the data, eg if you allocate a struct for the entry to store various pointers, then you need to set the disposeTrackEntry function for the spAnimationState, shown here.

Both approaches are used by spine-cocos2d-iphone and spine-cocos2dx:
https://github.com/EsotericSoftware/spi ... on.cpp#L43

spine-cocos2dx uses C++11 lambas to make spAnimaitonState event callbacks easier. The end result looks like this:
https://github.com/EsotericSoftware/spi ... le.cpp#L52
Note how the spTrackEntry callback is done on line 73.

Probably you'd want to build a small system for however you choose to do callbacks, then use that in your game.

Ah thanks! Gave me just the answer I was looking for.


Nate написал

The callback gets AnimationState* state which has a void* rendererObject field. You can use this to have context for the event. Eg, you could put your instance in rendererObject, then in the callback cast rendererObject to your instance and call a method on in.

the SFML runtime has no reference to rendererObject, any that I can find at least. Anyhow I'm still stuck on this, I thought Lambdas would be my solution but I was wrong.

I got it figured out, my source was aout a month behind and didn't have the 'void* rendererObject' yet, so I was a little confused. Once I got it updated was no problem. Thanks Nate.