Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally mirror a SKSpriteNode texture

I'm developing an iOS7 game with the new API called Sprite Kit. I'd like to horizontally rotated a SKSpriteNode image/texture. I've tried it by first mirroring the image, then creating a SKTexture and finally applying it to the SKSpriteNode but it doesn't work. Is there some way to do this? Or I should have to different images?

like image 712
Marti Serra Vivancos Avatar asked Oct 03 '13 15:10

Marti Serra Vivancos


2 Answers

If you're just trying to flip the sprite along an axis, you can do something like this:

sprite.xScale = -1.0; 
like image 121
Greg Avatar answered Sep 22 '22 04:09

Greg


You can use this code to flip among x-axis:

spriteNode.xScale = spriteNode.xScale * -1; 

but be careful you can lose some of physicsbody's property, I highly suggest u to use xScale in this way:

spriteNodeBody = [SKNode node]; spriteNodeBody.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.size]; spriteNodeBody.physicsBody.categoryBitMask = CNPhysicsCategoryPlayer; spriteNodeBody.physicsBody.collisionBitMask = CNPhysicsCategoryBall;  [spriteNodeBody addChild:spriteNode]; [self addChild:spriteNodeBody]; 

And now you can safely use:

    spriteNode.xScale = spriteNode.xScale * -1; 
like image 30
Ahmet Hayrullahoglu Avatar answered Sep 20 '22 04:09

Ahmet Hayrullahoglu