How to alternate the Zorder for each Sprite frame in Animation

How to alternate the Zorder for each Sprite frame in Animation


Hello every one. I have 5 sprite frame example sprite1.png, sprite2.png, sprite3.png, sprite4.png, sprite5.png

I would like to make animation using those sprite. But i want to alternate the ZOrder for each sprite Frame. Example with sprite1.png frame it have ZOrder =1, but with sprite2.png it have ZOrder = 2, and sprite3.png have ZOrder = 1…

Have any way to implement this


I think, the best way is to subclass Animate, overriding updateupdate(float) and/or using _frameDisplayEvent


This it, how I would do it:

	Vector<AnimationFrame*> frames(5);
ValueMap userinfo;

userinfo["zorder"] = 1;
frames.pushBack( AnimationFrame::create(SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite1.png"), 1, userinfo));
userinfo["zorder"] = 3;
frames.pushBack( AnimationFrame::create(SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite2.png"), 1, userinfo));
userinfo["zorder"] = 1;
frames.pushBack( AnimationFrame::create(SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite3.png"), 1, userinfo));
userinfo["zorder"] = 3;
frames.pushBack( AnimationFrame::create(SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite4.png"), 1, userinfo));
userinfo["zorder"] = 1;
frames.pushBack( AnimationFrame::create(SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite5.png"), 1, userinfo));

auto animation = Animation::create(frames, 0.5f); // or whatever delay u like

auto listener = EventListenerCustom::create(AnimationFrameDisplayedNotification,
											[this](EventCustom *event){
												AnimationFrame::DisplayedEventInfo * info = static_cast<AnimationFrame::DisplayedEventInfo*>(event->getUserData());

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);



Sorry about the bad formatting…
Not used to posting from my tablet…


And my tablet doesn’t like editing either. So just a little beauty editing: you can delete this from the lambda function, I just had it in there for logging purposes…



Thank for your guide, i will try this. It a good idea.

I have one more question.
I my game have some different animation.

But have only one animation that I need the alternate the ZOrder of the frame.
If i using AnimationFrameDisplayedNotification, the Notification will affect for all another animation.

My question is how to just use the AnimationFrameDisplayedNotification a specified animation, others animation not affect?

Can i set enable AnimationFrameDisplayedNotification for animation that i want to alternating ZOrder, and disable AnimationFrameDisplayedNotification for animations that i don’t want alternating ZOrder?


No, it won’t affect other animations.
AnimationFrameDisplayedNotification is only triggered, if userInfo is set in an AnimationFrame.


Thanks this was really helpful