diff --git a/spine-cocos2dx/example/Classes/ExampleLayer.cpp b/spine-cocos2dx/example/Classes/ExampleLayer.cpp index 60fa3dc9b..51028c6a4 100644 --- a/spine-cocos2dx/example/Classes/ExampleLayer.cpp +++ b/spine-cocos2dx/example/Classes/ExampleLayer.cpp @@ -49,7 +49,7 @@ bool ExampleLayer::init () { skeletonNode = SkeletonAnimation::createWithFile("spineboy.json", "spineboy.atlas", 0.6f); skeletonNode->setMix("walk", "jump", 0.2f); skeletonNode->setMix("jump", "run", 0.2f); - skeletonNode->setAnimationListener(this, animationStateEvent_selector(ExampleLayer::animationStateEvent)); + skeletonNode->setAnimationListener(&ExampleLayer::animationStateEvent, this); // skeletonNode->timeScale = 0.3f; skeletonNode->debugBones = true; diff --git a/spine-cocos2dx/src/spine/SkeletonAnimation.cpp b/spine-cocos2dx/src/spine/SkeletonAnimation.cpp index a2fadabe9..719818e72 100644 --- a/spine-cocos2dx/src/spine/SkeletonAnimation.cpp +++ b/spine-cocos2dx/src/spine/SkeletonAnimation.cpp @@ -63,9 +63,6 @@ SkeletonAnimation* SkeletonAnimation::createWithFile (const char* skeletonDataFi } void SkeletonAnimation::initialize () { - listenerInstance = 0; - listenerMethod = 0; - ownsAnimationStateData = true; state = spAnimationState_create(spAnimationStateData_create(skeleton->data)); state->context = this; @@ -117,11 +114,6 @@ void SkeletonAnimation::setMix (const char* fromAnimation, const char* toAnimati spAnimationStateData_setMixByName(state->data, fromAnimation, toAnimation, duration); } -void SkeletonAnimation::setAnimationListener (Ref* instance, SEL_AnimationStateEvent method) { - listenerInstance = instance; - listenerMethod = method; -} - spTrackEntry* SkeletonAnimation::setAnimation (int trackIndex, const char* name, bool loop) { spAnimation* animation = spSkeletonData_findAnimation(skeleton->data, name); if (!animation) { @@ -153,7 +145,7 @@ void SkeletonAnimation::clearTrack (int trackIndex) { } void SkeletonAnimation::onAnimationStateEvent (int trackIndex, spEventType type, spEvent* event, int loopCount) { - if (listenerInstance) (listenerInstance->*listenerMethod)(this, trackIndex, type, event, loopCount); + if (listener) listener(this, trackIndex, type, event, loopCount); } } diff --git a/spine-cocos2dx/src/spine/SkeletonAnimation.h b/spine-cocos2dx/src/spine/SkeletonAnimation.h index dffc40550..9ad1995ae 100644 --- a/spine-cocos2dx/src/spine/SkeletonAnimation.h +++ b/spine-cocos2dx/src/spine/SkeletonAnimation.h @@ -38,8 +38,7 @@ namespace spine { class SkeletonAnimation; -typedef void (cocos2d::Ref::*SEL_AnimationStateEvent)(spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount); -#define animationStateEvent_selector(_SELECTOR) (SEL_AnimationStateEvent)(&_SELECTOR) +typedef std::function AnimationStateListener; /** Draws an animated skeleton, providing an AnimationState for applying one or more animations and queuing animations to be * played later. */ @@ -62,7 +61,10 @@ public: void setAnimationStateData (spAnimationStateData* stateData); void setMix (const char* fromAnimation, const char* toAnimation, float duration); - void setAnimationListener (cocos2d::Ref* instance, SEL_AnimationStateEvent method); + template void setAnimationListener (_Rx _Farg0::* const type, _Arg0&& target) { + this->listener = std::bind(type, target, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5); + } + spTrackEntry* setAnimation (int trackIndex, const char* name, bool loop); spTrackEntry* addAnimation (int trackIndex, const char* name, bool loop, float delay = 0); spTrackEntry* getCurrent (int trackIndex = 0); @@ -76,8 +78,7 @@ protected: private: typedef SkeletonRenderer super; - cocos2d::Ref* listenerInstance; - SEL_AnimationStateEvent listenerMethod; + AnimationStateListener listener; bool ownsAnimationStateData; void initialize ();