Swift 3 Physicsbody Collisions不起作用

时间:2017-05-05 15:06:25

标签: swift swift3 sprite-kit skphysicsbody

这是我的播放器类

我的播放器不会与他们刚刚经过的Gamefield碰撞。 我不希望他们在彼此之上。 我试图谷歌解决方案,但对我来说,似乎我做的大部分都没事。

请帮助:

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

这是我的Gamescene 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())

我希望有人看到这个错误。我已经花了很长时间。

1 个答案:

答案 0 :(得分:1)

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

  

SpriteKit支持两种物理实体,即基于体积的实体和基于边缘的实体。创建物理实体时,其类型,大小和形状由您调用的构造方法确定。基于边缘的主体没有质量或体积,并且不受系统中的力或冲动的影响。基于边的实体用于表示物理模拟中的无体积边界或空心空间。相比之下,基于体积的物体用于表示具有质量和体积的物体。

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

此外,您似乎需要将类别掩码更改为正确的系列(对于didBegin(联系人:))

它应该是,1,2,4,8,1,等等...这就是你如何获得独特的联系人点击...现在你可以得到一个&#34 ; 4"从0 + 4或1 + 3 ...因此,不是唯一的碰撞。

你的collisionBitMask看起来不错......它们应该相互撞击。