I have a setup where there is an empty SKSpriteNode
("Player") that has N number of SKSpriteNode
s as childs (body parts, such as "Arm"). Every child has an SKPhysicsBody
created like this:
sprite.physicsBody = SKPhysicsBody(texture: sprite.texture, size: sprite.texture!.size())
When I try to flip the whole sprite ("Player) with
self.xScale = -1
all the textures are flipped properly, but the SKPhysicsBody
s are unaffected. How do I flip the whole sprite with all the children and their SKPhysicsBody
s properly?
One way to create a physicsBody from a texture that is flipped horizontally is to create a flipped version of input image using an image editing software, such as Photoshop. An alternate way is to flip the original image using core graphics. Here's an example of how to do that:
func flippedTextureWithImageNamed(name:NSString) -> SKTexture
{
let image = UIImage(named:name)
UIGraphicsBeginImageContext(image.size);
let context = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(context, image.size.width, image.size.height)
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)
}
Scale is a visual property, it does not affects physics. To modify a node's shape you can only create and assign a new body with a new shape.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.