【问题标题】:How do I make a rectangle attach to a circle and spin along with it in Swift, SpriteKit如何在 Swift、SpriteKit 中将矩形附加到一个圆圈并随之旋转
【发布时间】:2019-12-07 19:04:20
【问题描述】:

所以我一直在尝试重新创建类似于游戏“aa”的东西,其中一个“箭头”射向一个圆圈并附着在它上面并随之旋转。我已经设法使箭头将箭头附加到圆圈上,但它不会旋转它只是停留在附加到圆圈的位置上。我大约一周前开始编码,但我无法找到解决方案。这是我的 didBeginContact 函数


   func didBegin(_ contact: SKPhysicsContact) {
        let firstBody = contact.bodyA
        let secondBody = contact.bodyB

        if firstBody.categoryBitMask == PhysicsCategory.Circle && secondBody.categoryBitMask == PhysicsCategory.Arrow || firstBody.categoryBitMask == PhysicsCategory.Arrow && secondBody.categoryBitMask == PhysicsCategory.Circle {

            var Joint = SKPhysicsJointSpring.joint(withBodyA: Arrow.physicsBody!, bodyB: Circle.physicsBody!, anchorA: Arrow.position, anchorB: Circle.position)

            self.physicsWorld.add(Joint)

        }
    }

这也是我让圆圈旋转的方式 Circle.run(SKAction .rotate(byAngle: 9999, duration: 5000))

我想知道除了放很长的持续时间之外,是否还有其他方法可以让它一直保持旋转。

【问题讨论】:

  • 我将 Arrow.position 和 Circle.position 更改为 anchorPoint,这确实使它与圆一起旋转,但它不会保持在水平位置,它只是做任何想做的事情。

标签: ios swift sprite-kit


【解决方案1】:

我自己想通了。

var Joint = SKPhysicsJointSpring.joint(withBodyA: Arrow.physicsBody!, bodyB: Circle.physicsBody!, anchorA: Arrow.position, anchorB: Circle.position)

改为

var Joint = SKPhysicsJointFixed.joint(withBodyA: Circle.physicsBody!, bodyB: Arrow.physicsBody!, anchor: Circle.position)

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多