SpriteKit:SKShapeNodes重叠

时间:2015-03-06 01:30:34

标签: ios swift sprite-kit

我刚刚在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!)
    }
}

enter image description here

感谢。

1 个答案:

答案 0 :(得分:1)

您可能希望为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建议):

physics

您可以看到物理主体附加到左下锚点。

以下是解决这个问题的方法:

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!)
}

nowOkay

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