【问题标题】:Flipping a sprite with xScale in SpriteKit in iOS 8 doesn't flip the SKPhysicsBody在 iOS 8 的 SpriteKit 中使用 xScale 翻转精灵不会翻转 SKPhysicsBody
【发布时间】:2014-10-05 16:22:06
【问题描述】:

我有一个设置,其中有一个空的SKSpriteNode(“玩家”),其中有 N 个SKSpriteNodes 作为孩子(身体部位,例如“手臂”)。每个孩子都有一个SKPhysicsBody,这样创建:

sprite.physicsBody = SKPhysicsBody(texture: sprite.texture, size: sprite.texture!.size())

当我尝试用

翻转整个精灵(“播放器”)
self.xScale = -1

所有纹理都正确翻转,但SKPhysicsBodys 不受影响。如何正确翻转所有孩子及其SKPhysicsBodys 的整个精灵?

【问题讨论】:

  • 很高兴我找到了这个,我一直在苦苦挣扎......

标签: ios swift ios8 sprite-kit


【解决方案1】:

从水平翻转的纹理创建physicsBody 的一种方法是使用图像编辑软件(例如Photoshop)创建输入图像的翻转版本。另一种方法是使用核心图形翻转原始图像。以下是如何做到这一点的示例:

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)
}

【讨论】:

  • 好像还有一个Core Image filter可以翻转图片。但这对我的目的来说已经足够了,谢谢!
  • 在没有流畅动画的情况下翻转那里..它突然翻转..我们可以在这里做到吗..
【解决方案2】:

比例是一种视觉属性,它不会影响物理。要修改节点的形状,您只能创建并分配具有新形状的新主体。

【讨论】:

  • 谢谢,我就是这么想的。所以问题是,我猜,如何创建镜像SKPhysicsBody。这似乎是用物理体翻转精灵的唯一方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多