I have some sprites, where the player character is facing to the right.
I can create an animation from those sprites just fine. The problem is, if I want the sprites to face to the left.
I do the following:
Sprite* p = Sprite::createWithSpriteFrameName("Jumping");
p->setPosition(Vec2(_visibleSize.width/2,_visibleSize.height/2));
this->addChild(p);
p->setFlippedX(true);
Vector<AnimationFrame*> animFrames;
float frameRate = 0.32f;
std::vector<std::string> frameNames = {"Running 0","Running 1","Running 2"};
for (int i =0; i<3;i++){
auto frameName = frameNames.at(i);
auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName);
ValueMap userInfo;
userInfo["frame_index"] = Value(i);
auto animFrame = AnimationFrame::create(spriteFrame, frameRate, userInfo);
animFrames.pushBack(animFrame);
}
auto animation = Animation::create(animFrames, frameRate);
auto animationAction = Animate::create(animation);
p->runAction(RepeatForever::create(animationAction));
p->setFlippedX(true);
The animation runs, but the animation still shows the player facing to the right. What is the problem ? Why doesn’t setFlippedX work in this case ?
I am using cocos2d-x 3.13.1. I can’t find any bug, so I assume I am doing something incorrectly.
still haven’t figured it out.
Let me try and test this today or tomorrow.
I’m a bit surprised, but my guess is that when each frame is applied with setSpriteFrame the flippedX is getting restored to false
? This shouldn’t happen.
For a workaround, for now, you should be able to put the sprite into a parent node and flip the parent.
Node *p = Node::create();
p->setFlippedX(true);
p->setPosition(Vec2(_visibleSize.width/2,_visibleSize.height/2));
this->addChild(p);
Sprite* pChild = Sprite::createWithSpriteFrameName("Jumping");
p->addChild(pCharacter);
Vector<AnimationFrame*> animFrames;
float frameRate = 0.32f;
std::vector<std::string> frameNames = {"Running 0","Running 1","Running 2"};
for (int i =0; i<3;i++){
auto frameName = frameNames.at(i);
auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName);
ValueMap userInfo;
userInfo["frame_index"] = Value(i);
auto animFrame = AnimationFrame::create(spriteFrame, frameRate, userInfo);
animFrames.pushBack(animFrame);
}
auto animation = Animation::create(animFrames, frameRate);
auto animationAction = Animate::create(animation);
p->runAction(RepeatForever::create(animationAction));
1 Like
Interesting idea. I’ll try this too OP code doesn’t work and it should.
I’m using version 3.13.1 - so it’s been around for a while. The weird thing is that’s it’s a common use case so someone should have stumbled on it sooner. That’s why I started thinking there is something wrong with my code.
Thanks, that’s a good idea. I can’t try it yet because I’m away from the computer. But it think if it does work, then it the problem has to do with the animation code.
For a workaround, for now, you should be able to put the sprite into a parent node and flip the parent.
Node doesn’t have a setFlippedX() method, so instead I tried creating a parent sprite, and flipping the parent.
That doesn’t seem to work either.
Did you guys try it on your code ? I’m asking because I’m wondering if this is a bug, or something wrong with what I’m doing. Since the current version is 3.18, and my version is 3.13.1, I would have thought this would be a known problem by now…
I tried this again to where I used a visible parent sprite.
It seems that the parent is flipped, but the animated child is not.
This seems like a bug - I’ve tried it with 3.15.1, and it doesn’t seem to work here either.
This is a bug - it appears that a fix was checked in in 3.16 https://github.com/cocos2d/cocos2d-x/pull/18085/files
1 Like