Question

I'm trying to animate a sprite using only CoreAnimation. It's working so far except I can't figure out how to flip the sprite sheet.

Right now, I have a walking animation, but I want the sprite to face the direction it's walking (because it looks kind of silly walking backwards).

I guess I could add the reversed images on the sprite sheet, but I would rather avoid that because it could make it really big when I decide to add more.

Right now, my sprite is extending CALayer and I've set its contents to the CGImageRef which is the sprite sheet:

self.contents = (id) image;

To flip it I tried:

UIImage *tmp = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUpMirrored];
CGImageRef flippedImage = tmp.CGImage;
self.contents = (id) flippedImage;

..and that's not working.

I found other solutions which involve animating, but I don't want to animate the flip. I just want it to happen instantly.

Is there a simple way to do this?

If there's a way to flip the whole CALayer, I'd like to know that too. =]

Thanks!

Was it helpful?

Solution

Rather than flipping your image, you could try applying a transform to your layer object. Something like:

self.transform = CATransform3DMakeScale(-1, 1, 1);

This may be better for performance too; if the sprite needs to walk in the opposite direction, you can just set your transform back to CATransform3DIdentity rather than allocing a new image and rotating it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top