SpriteKit:SKShapeNodes 重叠

SpriteKit: SKShapeNodes overlapping

我刚刚在 SpriteKit 中创建了一个非常基本的场景,第一个问题就出现了。

当两个SKShapeNode接触时,玩家节点与地面节点重叠,我看不出为什么。

这是我的代码和截图:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    var contentCreated = false
    var player: SKShapeNode?

    override func didMoveToView(view: SKView) {
        if !contentCreated {
            createSceneContents()
            contentCreated = true
        }
    }

    func createSceneContents() {
        self.backgroundColor = SKColor(white: 1.0, alpha: 1.0)
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsWorld.gravity = CGVectorMake(0, -10)
        self.physicsWorld.contactDelegate = self

        let groundCategory: UInt32 = 0x1 << 0
        let playerCategory: UInt32 = 0x1 << 0

        let ground = SKShapeNode(rect: CGRectMake(0, 0, self.frame.width, 50))
        ground.fillColor = UIColor.blueColor()
        ground.lineWidth = 0
        ground.position = CGPoint(x: 0, y: 0)
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.frame.size)
        ground.physicsBody?.dynamic = false
        ground.physicsBody?.categoryBitMask = groundCategory
        ground.physicsBody?.collisionBitMask = groundCategory
        ground.physicsBody?.contactTestBitMask = groundCategory
        self.addChild(ground)

        player = SKShapeNode(rect: CGRectMake(0, 0, 30, 30))
        player?.fillColor = UIColor.blackColor()
        player?.lineWidth = 0
        player?.position = CGPoint(x: 20, y: 400)
        player?.physicsBody = SKPhysicsBody(rectangleOfSize: player!.frame.size)
        player?.physicsBody?.mass = 1.0
        player?.physicsBody?.categoryBitMask = playerCategory
        player?.physicsBody?.collisionBitMask = playerCategory
        player?.physicsBody?.contactTestBitMask = playerCategory
        self.addChild(player!)
    }
}

谢谢。

您可能希望为您的 groundplayer 使用不同的位掩码:

let groundCategory: UInt32 = 0x1
let playerCategory: UInt32 = 0x1 << 1

那么您可能还想以不同方式设置碰撞/接触位掩码:

ground.physicsBody?.categoryBitMask = groundCategory
ground.physicsBody?.collisionBitMask = playerCategory
ground.physicsBody?.contactTestBitMask = playerCategory

player?.physicsBody?.categoryBitMask = playerCategory
player?.physicsBody?.collisionBitMask = groundCategory
player?.physicsBody?.contactTestBitMask = groundCategory

即便如此,您还是会拥有与您已经拥有的一样的东西。但是,如果您打开 showPhysics(如@Skoua 所建议):

可以看到物理体附着在左下方的锚点上。

解决方法如下:

func createSceneContents() {
    self.backgroundColor = SKColor(white: 1.0, alpha: 1.0)
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsWorld.gravity = CGVectorMake(0, -10)
    self.physicsWorld.contactDelegate = self

    let groundCategory: UInt32 = 0x1
    let playerCategory: UInt32 = 0x1 << 1

    let ground = SKShapeNode(rectOfSize: CGSize(width: self.frame.width, height: 50)) // HERE
    ground.fillColor = UIColor.blueColor()
    ground.lineWidth = 0
    ground.position = CGPoint(x: ground.frame.size.width/2, y: ground.frame.size.height/2) // HERE
    ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.frame.size)
    ground.physicsBody?.dynamic = false
    ground.physicsBody?.categoryBitMask = groundCategory
    ground.physicsBody?.collisionBitMask = playerCategory
    ground.physicsBody?.contactTestBitMask = playerCategory
    self.addChild(ground)

    player = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30)) // HERE
    player?.fillColor = UIColor.blackColor()
    player?.lineWidth = 0
    player?.position = CGPoint(x: 20, y: 400)
    player?.physicsBody = SKPhysicsBody(rectangleOfSize: player!.frame.size)
    player?.physicsBody?.mass = 1.0
    player?.physicsBody?.categoryBitMask = playerCategory
    player?.physicsBody?.collisionBitMask = groundCategory
    player?.physicsBody?.contactTestBitMask = groundCategory
    self.addChild(player!)
}

不要忘记,在放置节点时,默认锚点是场景的左下角。