4
votes

I'm creating a fish for an app that swims to random locations on the screen. Before the fish begins swimming towards the next location, it rotates to the angle between its starting point and the target point.

What I'm trying to figure out is: if (target.x < start.x), I need to flip the sprite horizontally.

The problem is, after I create the sprite and addChild to the layer, I can't set the flipX property of the sprite using [sprite setFlipX].

Is setFlipX locked after the sprite is added to the layer? How can I get around this? Is my only solution to animate?

2
I did not think FlipX was locked.Almo
It doesn't flip the texture after the sprite is added to the layer, only beforeMatisse VerDuyn
Are you sure? Usually Cocos2d makes such things readonly.Almo

2 Answers

5
votes

To flip and preserve any previous scaling, use:

sprite.scaleX *= -1.f;

After you've done this you should not use the property sprite.scale any more as it includes an assertion of scaleX == scaleY.

3
votes

Try flipping it by setting the scaleX to -1:

sprite.scaleX = -1;

Also, for what it's worth, you should be able to set the flipX boolean after a node is added as a child. Something else must be going on if you can't.