Swift 3 Physicsbody Collisions 不起作用

Swift 3 Physicsbody Collisions doesn't work

这是我的class玩家

我的播放器不会与 Gamefield 发生碰撞,它们只是相互穿过。 我不希望他们彼此重叠。 我尝试 google 解决方案,但对我来说,我似乎大部分都做得很好。

请帮忙:

class Player: SKShapeNode {
    static public let length: CGFloat = 50
    static public let rect = CGRect(x: -length/2, y: -length/2, width: length, height: length)

    var life = 100

    override init() {
        super.init()
        self.fillColor = SKColor.blue
        self.strokeColor = SKColor.black
        self.position = CGPoint(x: 50, y: 50)

        self.physicsBody = SKPhysicsBody(edgeLoopFrom: Player.rect)
        self.physicsBody?.isDynamic = true
        self.physicsBody?.allowsRotation = false
        self.physicsBody?.categoryBitMask = PhysicsCategory.Robot
        self.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
        self.physicsBody?.collisionBitMask = PhysicsCategory.Gamefield
        self.physicsBody?.usesPreciseCollisionDetection = true
    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    func moveBy(vect: CGVector) {
        self.position.x += vect.dx
        self.position.y += vect.dy
        //print(vect.dx, vect.dy)
    }
}

这是我的游戏场景Class

struct PhysicsCategory {
    static let None         : UInt32 = UInt32.min
    static let All          : UInt32 = UInt32.max
    static let Robot        : UInt32 = 0b0001      // 1
    static let Gamefield    : UInt32 = 0b0010      // 2
    static let Monster      : UInt32 = 0b0011      // 3
    static let Projectile   : UInt32 = 0b0100      // 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {
    var player = Player(rect: Player.rect)
    var controlL: Control
    var controlR: Control

    var cam = SKCameraNode()

    override init(size: CGSize) {
        controlL = Control(posX: 0, size: size, direction: 1)
        controlR = Control(posX: size.width, size: size, direction: -1)

        super.init(size: size)

    }; required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    override func didMove(to view: SKView) {
        self.backgroundColor = SKColor.white

        physicsWorld.gravity = CGVector.zero
        physicsWorld.contactDelegate = self

        cam.xScale = 1
        cam.yScale = 1
        self.camera = cam
        player.addChild(cam)

        let gameFieldRect = CGRect(x: 0, y: 0, width: 1000, height: 600)
        let gameField = SKShapeNode(rect: gameFieldRect)
            gameField.fillColor = SKColor.clear
            gameField.strokeColor = SKColor.black
            gameField.position = CGPoint(x: 0, y: 0)

            gameField.physicsBody = SKPhysicsBody(edgeLoopFrom: gameFieldRect)
            gameField.physicsBody?.isDynamic = true
            gameField.physicsBody?.allowsRotation = false
            gameField.physicsBody?.categoryBitMask = PhysicsCategory.Gamefield
            gameField.physicsBody?.contactTestBitMask = PhysicsCategory.None
            gameField.physicsBody?.collisionBitMask = PhysicsCategory.Robot
        self.addChild(gameField)

        self.addChild(player)

        cam.addChild(controlL.add())
        cam.addChild(controlR.add())

希望有人看到错误。我花了很长时间才准备好。

您需要将您的机器人更改为不同的物理体类型 (rectangleOf)?:

SpriteKit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volume-less boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume.

此外,您是使用脉冲还是力来移动机器人?不是 .move(to:) 或 .position =?移动和位置会破坏你的物理世界(在某些情况下会通过它)

此外,您似乎需要将类别掩码更改为适当的系列(对于 didBegin(contact:))

它应该是 1、2、4、8、16,依此类推...这就是您获得 唯一 联系人点击的方式..现在您可以获得从 0+4 或 1+3 命中“4”...因此,不是唯一的碰撞。

不过你的 collisionBitMask 看起来不错。它们应该会相互碰撞。