ios - 作为一个单元旋转时重叠 SpriteNode

标签 ios swift swift3 sprite-kit skspritenode

我在使用 Swift 3 和 Spritekit 开发我的应用程序时遇到了一个问题。为了检测碰撞,我制作了四个独立的三角形组成一个正方形。当我将这些三角形作为一个单元旋转时,它们会重叠并且在旋转过程中不会保持正方形的形状(它们彼此重叠),但一旦旋转完成就会恢复正常。

这是我用于旋转的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let location = touch.location(in: self)

        if location.x < 0 {
            blueTriLeft.run(SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 0.2))
            redTriLeft.run(SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 0.2))
            yellowTriLeft.run(SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 0.2))
            greenTriLeft.run(SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 0.2))

        } else if location.x > 0 {
            blueTriRight.run(SKAction.rotate(byAngle: CGFloat(-M_PI_2), duration: 0.2))
            redTriRight.run(SKAction.rotate(byAngle: CGFloat(-M_PI_2), duration: 0.2))
            yellowTriRight.run(SKAction.rotate(byAngle: CGFloat(-M_PI_2), duration: 0.2))
            greenTriRight.run(SKAction.rotate(byAngle: CGFloat(-M_PI_2), duration: 0.2))
        }
    }
}

下面是我的正方形的图片,由四个独立的三角形组成

square

请随时向我询问您需要查看的任何其他代码,任何输入都会有所帮助。另外,我无法解决重叠问题,所以提前抱歉,但我会尽力解决。

最佳答案

问题是你在旋转单个三角形,它们根据自己的坐标系旋转。想象一下,每个三角形都固定在中心并围绕它旋转。显然三角形会重叠。您应该围绕一个独特的点旋转它们。在您的情况下,最简单的方法是将它们添加到同一个父节点,然后旋转父节点:

// This is the configuration to do in sceneDidLoad
let node = SKNode()
node.addChild(blueTriLeft) 
node.addChild(redTriLeft)
node.addChild(yellowTriLeft)
node.addChild(greenTriLeft)
scene.addChild(node)

// Inside touchesBegan(_:, with:)
node.run(SKAction.rotate(byAngle: CGFloat(-M_PI_2), duration: 0.2))

关于ios - 作为一个单元旋转时重叠 SpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45122470/

相关文章:

ios - 如何快速过滤字符串并获得中间结果

ios - 没有这样的文件或目录 : 'SwiftyJSON'

javascript - 有没有办法共享特定的应用程序 ios?

ios - 翻译时仅运行一次 swift 条件

ios - 内部名称 swift 2.0+?

ios - Array.index(: can return nil But there's compile error when checking for optional value

ios - Ionic 4 禁用 iOS 仅在一页中滑动返回

swift - Tableview 附件无法正确加载

ios - 不能在属性初始值设定项中使用实例成员

ios - 如何在 Xcode 8 中使用 Swift 3 创建 managedObjectContext?