Hi guys,
I am using spritesheet method to create my animations and I wonder how I should do to run a function in a specified frame (For those who know AS3 I seek the equivalent of “addFramescript”). Or can get the current frame of my animation in my function update.
Ex: Each time my animation release the frame 3 I would like to execute a method “methodX.”
myAnimation-> addFramescript (frame3, methodX);
My :: Class methodX void ()
{
// here i execute something …
}
auto sprites = SpriteFrameCache::getInstance();
// use an array of AnimationFrames instead of SpriteFrames in order to be able to set userInfo
Vector<AnimationFrame*> animFrames;
//these are normal frames
animFrames.pushBack(AnimationFrame::create(sprites->getSpriteFrameByName("frame1.png"), 1, ValueMap()));
animFrames.pushBack(AnimationFrame::create(sprites->getSpriteFrameByName("frame2.png"), 1, ValueMap()));
// this frame will trigger our custom event
animFrames.pushBack(AnimationFrame::create(sprites->getSpriteFrameByName("frame3.png"), 1, {{"triggerEvent", Value(true)}}));
animation = Animation::create(animFrames, yourDelay);
yourSprite->runAction(Animate::create(animation));
// add an event listener with a lambda function
Director::getInstance()->getEventDispatcher()->addCustomEventListener(AnimationFrameDisplayedNotification, [this](EventCustom* event) {
auto userData = static_cast<AnimationFrame::DisplayedEventInfo*>(event->getUserData());
if (userData->target == yourSprite && userData->userInfo->at("triggerEvent").getType() == Value::Type::BOOLEAN && userData->userInfo->at("triggerEvent").asBool()) {
// do your stuff
}
});
Director::getInstance()->getEventDispatcher()->addCustomEventListener(AnimationFrameDisplayedNotification, [this](EventCustom event) {
// Waiting for a notification
}*
though, because Xcode didn’t like pair without a template argument list. But that could be different in your dev environment, you’re obviously using something different than I am, since you couldn’t use the initializer_list
Maybe post a little more code, so I can help you better.
For example create a new test project with only the animated sprite in it and post your entire HelloWorld::init()