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 …
}
thanks
This is how I would do it:
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
}
});
it doesn’t works.
i have remplace the lines below to can compile:
animFrames.pushBack(AnimationFrame::create(sprites->getSpriteFrameByName(“frame3.png”), 1, {{“triggerEvent”, Value(true)}}));
for
ValueMap myValueMap;
myValueMap.insert(std::pair<char,Value>(“triggerEvent”, Value(true)));
animFrames.pushBack(AnimationFrame::create(sprites->getSpriteFrameByName(“frame3.png”), 1,myValueMap));*
But i never receive a notification
Director::getInstance()->getEventDispatcher()->addCustomEventListener(AnimationFrameDisplayedNotification, [this](EventCustom event) {
// Waiting for a notification
}*
What i’m doing wrong ?
First of all, you made a typo:
[this](EventCustom event)
should be
[this](EventCustom* event)
But this should have given you a compiler error…
Other than that, all should be fine. I copied your code into a test project and it worked.
I had to change
std::pair("triggerEvent", Value(true))
to
std::pair<std::string,Value>("triggerEvent", Value(true))
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()
And please post your code with proper markup:
```
code
```
will give you
code
It’s so much more convenient to read 