你如何通过 Spritekit 关节锚



我有一棵由一系列关节构成的树。锚是树的底部/树桩。我的英雄目前无法穿过锚点。设置 collisionBitMask = 0 不适用于定位点,但该方法适用于各个关节段。

所以,基本上我只是想避免这种碰撞。这是代码:

锚点代码

let chunkHolder = SKSpriteNode(imageNamed: ImageName.ChunkHolder)
chunkHolder.position = anchorPoint
chunkHolder.physicsBody = SKPhysicsBody(circleOfRadius: chunkHolder.size.width / 2)
chunkHolder.physicsBody?.isDynamic = false
chunkHolder.physicsBody?.categoryBitMask = PhysicsCategory.chunkAnchor.rawValue
chunkHolder.physicsBody?.collisionBitMask = 0
addChild(chunkHolder)

英雄正确通过的单个树分隔:

for i in 0..<length {
    let treeSegment = SKSpriteNode(imageNamed: ImageName.ChunkTexture)
    let offset = treeSegment.size.height * CGFloat(i + 1)
    treeSegment.position = CGPoint(x: anchorPoint.x, y: anchorPoint.y - offset)
    treeSegment.name = "tree" + String(i)
    treeSegments.append(treeSegment)
    addChild(treeSegment)
    treeSegment.physicsBody = SKPhysicsBody(rectangleOf: treeSegment.size)
    treeSegment.physicsBody?.collisionBitMask = 0  
}

//关节

for i in 1..<length {
        let nodeA = treeSegments[i - 1]
    let nodeB = treeSegments[i]
    let joint = SKPhysicsJointPin.joint(withBodyA: nodeA.physicsBody!, bodyB: nodeB.physicsBody!, anchor: CGPoint(x: nodeA.frame.midX, y: nodeA.frame.minY))
    scene.physicsWorld.add(joint)
}

这里供参考的是英雄的物理体:

self.physicsBody?.categoryBitMask = PhysicsCategory.hero.rawValue
self.physicsBody?.contactTestBitMask = PhysicsCategory.ground.rawValue

此外,将英雄的碰撞位掩码设置为 0 目前也无济于事,因为这样英雄也会从地面/地板上掉下来。

此问题已通过将 categoryBitMask 设置为 0 来解决。

chunkHolder.physicsBody?.categoryBitMask = 0
chunkHolder.physicsBody?.collisionBitMask = 0

相关内容

  • 没有找到相关文章

最新更新