0Day Forums
Horizontally mirror a SKSpriteNode texture - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Swift (https://zeroday.vip/Forum-Swift)
+--- Thread: Horizontally mirror a SKSpriteNode texture (/Thread-Horizontally-mirror-a-SKSpriteNode-texture)



Horizontally mirror a SKSpriteNode texture - oversleep605910 - 07-19-2023

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?


RE: Horizontally mirror a SKSpriteNode texture - requiescat560723 - 07-19-2023

This is my solution written in **Swift 2.x**, usually I prefeer to use `landscape` mode for my games, so I write this extension:

extension SKTexture {
class func flipImage(name:String,flipHoriz:Bool,flipVert:Bool)->SKTexture {
if !flipHoriz && !flipVert {
return SKTexture.init(imageNamed: name)
}
let image = UIImage(named:name)

UIGraphicsBeginImageContext(image!.size)
let context = UIGraphicsGetCurrentContext()

if !flipHoriz && flipVert {
// Do nothing, X is flipped normally in a Core Graphics Context
// but in landscape is inverted so this is Y
} else
if flipHoriz && !flipVert{
// fix X axis but is inverted so fix Y axis
CGContextTranslateCTM(context, 0, image!.size.height)
CGContextScaleCTM(context, 1.0, -1.0)
// flip Y but is inverted so flip X here
CGContextTranslateCTM(context, image!.size.width, 0)
CGContextScaleCTM(context, -1.0, 1.0)
} else
if flipHoriz && flipVert {
// flip Y but is inverted so flip X here
CGContextTranslateCTM(context, image!.size.width, 0)
CGContextScaleCTM(context, -1.0, 1.0)
}

CGContextDrawImage(context, CGRectMake(0.0, 0.0, image!.size.width, image!.size.height), image!.CGImage)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return SKTexture(image: newImage)
}
}

**Usage:**

let spriteTxt = SKTexture.flipImage(imageName, flipHoriz: true, flipVert: false)

P.S.: If you want the same function but to `portrait` mode please refeer to this [answer][1]


[1]:

[To see links please register here]




RE: Horizontally mirror a SKSpriteNode texture - onfre287 - 07-19-2023

I use this for my rightHanded or leftHanded sprites:

with some help from here

[To see links please register here]


BOOL leftHanded = YES;

SKSpriteNode *sprite;

if (leftHanded) { //my textures point east, so I flip vertically
SKTexture *texture = [SKTexture textureWithImageNamed:@"figure-step0"];
CIFilter *filter = [CIFilter filterWithName:@"CIAffineTransform"];
[filter setValue:[CIImage imageWithCGImage:[texture CGImage]] forKey:kCIInputImageKey];

#ifdef IOS_BLOCK //set these up with defines
CGAffineTransform flipTransform = CGAffineTransformMakeScale(1.0f, -1.0f); // vert
[filter setValue:[NSValue valueWithBytes:&flipTransform
objCType:@encode(CGAffineTransform)]
forKey:@"inputTransform"];
#else //OSX_BLOCK
NSAffineTransform* flipTransform = [NSAffineTransform transform];
[flipTransform scaleXBy:1.0f yBy: -1.0f]; // vert
[filter setValue:flipTransform forKey:@"inputTransform"];
#endif
sprite = [SKSpriteNode spriteNodeWithTexture:
[texture textureByApplyingCIFilter:filter]];
} else {
sprite = [SKSpriteNode spriteNodeWithImageNamed:@"figure-step0"];
}

You can do the same for animation frames likewise. Works for iOS or OS X.


RE: Horizontally mirror a SKSpriteNode texture - parous578 - 07-19-2023

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;


RE: Horizontally mirror a SKSpriteNode texture - elsyfgfof - 07-19-2023

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

sprite.xScale = -1.0;