ios - SpriteKit 为每个节点添加一个 Spring 关节

标签 ios sprite-kit skshapenode skphysicsjoint

我正在使用 SpriteKit 为项目创建一个有趣的 Hotspot UI。应该很简单吧!只是一个背景图片,上面有很多点。当您点击一个点时,它会放大。当您再次点击时,它会恢复到正常大小和位置。但是,更重要的是,每个点都有物理特性,所以当它放大时,其他点会暂时移到一边。当点恢复到正常比例时,它们将弹回原位。

为了实现这一点,我在页面上放置了一堆 SKShapeNodes。我还创建了一个附加到场景 physicsBody 和 SKShapeNode 的 SKPhysicsJointSpring。

这是场景的代码:

override func didMoveToView(view: SKView) {

    name = "Hotspot test"
    physicsWorld.gravity = CGVectorMake(0, 0)

    let physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody = physicsBody


    for i in 1...10 {

        let j:__uint32_t = 0x1 << UInt32(i) // <- IGNORE THIS. IRRELEVANT
        let shape = DotNode(circleOfRadius: 60, name: "Ball"+String(i), id: i, bitmask: j)

        // Position objects in a circle around the center
        let degrees = (360/10)*i
        let radians = degrees.degreesToRadians
        let radius = CGFloat(300)
        let cosR = cos(radians)
        let sinR = sin(radians)
        let xPos = radius*cosR+frame.size.width/2
        let yPos = radius*sinR+frame.size.height/2

        let pos = CGPoint(x: xPos, y: yPos)
        shape.position = pos

        addChild(shape)

        let spring = SKPhysicsJointSpring.jointWithBodyA(self.physicsBody!, bodyB: shape.physicsBody!, anchorA: pos, anchorB: pos)
        spring.damping = 0.01
        physicsWorld.addJoint(spring)
    }

DotNode 是 SKShapeNode 的子类。这是它在屏幕上的样子:

nodes static

这是放大后的样子:

enter image description here

看到问题了吗?他们现在不互相交流。当我拖动其中一个时,我可以看到 Spring 关节已连接:

enter image description here

是否有任何原因导致对象不再相互交互?

如果你只是移除关节会发生这种情况:

enter image description here

物体相互插入。这必须是可以实现的。我做错了什么?

最佳答案

遗憾的是,SpriteKit 似乎无法做到这一点。本帖:SKPhysicsJoint: Contacts and collisions not working证实了这一点。

我不得不说,这看起来很疯狂。为什么您不想创建关节和 Spring ,然后让您连接的节点在物理世界中发生碰撞和 react ?

看来我将不得不失去 Spring ,只需对节点进行编码,使其不断漂移回到它们原来的位置。这样,当它们中的一个扩展并将其他的推开时,它们将始终尝试回家。

关于ios - SpriteKit 为每个节点添加一个 Spring 关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862640/

相关文章:

ios - 如何在 iOS 上使用 XMPP 框架和 OpenFire 获取用户 vCard

ios - 消息显示为空白?

ios - Sprite Kit中的节点数据

swift - 任何碰撞游戏结束 - Swift Spritekit

objective-c - 如何通过在 for-in 循环中使用 isKindOfClass 来访问我的类的属性?

swift - SpriteKit。为什么 .StrokeTexture 不适用于 SKShapeNode?

ios - SKShapeNode 和 CGPathRef EXC_BAD_ACCESS

IOS subview 自动调整轮帧值

ios - 在 iOS 8 共享扩展和主应用程序之间共享数据

ios - 如何为 SKShapeNode 设置小于 1 的线宽?