Why isn't an animation flipped horizontally when I call setFlipped(true)?

22.5k views Asked by At

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.

2

There are 2 answers

0
Rahul Iyer On BEST ANSWER

This seems to be a bug, and there doesn't seem to be a way to work around, short of using two sprite sets - one for all the sprites without flipping, and the other set for when the sprites are flipped.

What makes it worse - this means you can't use the animation code if you want flipping, and instead need to implement your own logic, to use the appropriate set of sprites, animations etc.

EDIT: It seems to be fixed in 3.16

6
Truth On

Its because you are calling this twice on your code,

p->setFlippedX(true);