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.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

2 Answers2

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

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
0

Its because you are calling this twice on your code,

p->setFlippedX(true);
Romil Patel
  • 12,879
  • 7
  • 47
  • 76
Truth
  • 76
  • 1
  • 6
  • both times I am calling it as "true", so it shouldn't make any difference. – Rahul Iyer May 25 '18 at 05:39
  • 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. – Truth May 25 '18 at 07:33
  • 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 ? – Rahul Iyer May 25 '18 at 07:44
  • 1
    Also, does setFlippedY works? also what is the value after calling setFlippedX and setFlippedY? – Truth May 25 '18 at 07:45
  • sorry, I dont have v3.13 on my machine. – Truth May 25 '18 at 08:03
  • 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 ? – Rahul Iyer May 25 '18 at 08:10