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

2

17

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.

Kaylenekayley answered 22/5, 2018 at 9:32 Comment(0)
K
4

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

Kaylenekayley answered 28/5, 2018 at 4:20 Comment(0)
H
0

Its because you are calling this twice on your code,

p->setFlippedX(true);
Haply answered 24/5, 2018 at 23:12 Comment(6)
both times I am calling it as "true", so it shouldn't make any difference.Kaylenekayley
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.Haply
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 ?Kaylenekayley
Also, does setFlippedY works? also what is the value after calling setFlippedX and setFlippedY?Haply
sorry, I dont have v3.13 on my machine.Haply
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 ?Kaylenekayley

© 2022 - 2024 — McMap. All rights reserved.