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

22,563

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

Share:
22,563
Rahul Iyer
Author by

Rahul Iyer

Hello, If you're stuck on any kind of engineering / programming problem (it doesn't matter what language or technology) feel free to ask me to take a look at it. : https://www.rahuliyer.com/contact Rahul https://www.buymeacoffee.com/rahuliyer

Updated on September 04, 2020

Comments

  • Rahul Iyer
    Rahul Iyer over 3 years

    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.

  • Rahul Iyer
    Rahul Iyer almost 6 years
    both times I am calling it as "true", so it shouldn't make any difference.
  • Truth
    Truth almost 6 years
    Can you try adding other transforms, like scale or rotations. just to see that the images can be played with. Also you can try to run on other platforms just to see its not a bug.
  • Rahul Iyer
    Rahul Iyer almost 6 years
    RotateBy and ScaleBy work as expected. Likewise, setFlipped works on the image if I do not animate it. I guess the question is: If you have cocos2d-x v 3.13.1, and you run the above code, are you able to run an animation with the sprites flipped horizontally ? And if so, can I see what the difference is in your project and mine ?
  • Truth
    Truth almost 6 years
    Also, does setFlippedY works? also what is the value after calling setFlippedX and setFlippedY?
  • Truth
    Truth almost 6 years
    sorry, I dont have v3.13 on my machine.
  • Rahul Iyer
    Rahul Iyer almost 6 years
    Which version do you have on your machine, and what happens when you run the above code on your machine ? Does it work as expected ?