0

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!

4

1 回答 1

2

您可以尝试对图层对象应用变换,而不是翻转图像。就像是:

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

这也可能对性能更好;如果精灵需要朝相反的方向行走,您只需将变换设置回 CATransform3DIdentity,而不是分配新图像并旋转它。

于 2013-05-24T19:47:50.293 回答