Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 195 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Horizontally mirror a SKSpriteNode texture

#1
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?
Reply

#2
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]

Reply

#3
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.
Reply

#4
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;
Reply

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

sprite.xScale = -1.0;
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through