联系位掩码

时间:2018-12-12 14:50:51

标签: swift sprite-kit

Heyo!我试图使我的游戏在球碰到墙壁时重新启动(又称“危险”)。我设置了接触位掩码和PhysicsCategories,但它无法正常工作。有人可以帮忙吗?当球碰到危险时,它不会打印“接触”,而我似乎也找不到原因。

此外,如何为“目标”添加另一个物理类别?如何添加另一个UInt32号?我可以将0x1更改为0x2吗?还是有一定的理由我需要这样做。我对这整个物理学类别都是陌生的,因此确实令人困惑。我找不到我想要的专门教程。

import SpriteKit
import GameplayKit



class GameScene: SKScene {

    var ball = SKSpriteNode()
    var danger1 = SKSpriteNode()
    var danger2 = SKSpriteNode()
    var goal = SKSpriteNode()


    override func didMove(to view: SKView) {

        ball = self.childNode(withName: "ball") as! SKSpriteNode
        danger1 = self.childNode(withName: "danger1") as! SKSpriteNode
        danger2 = self.childNode(withName: "danger2") as! SKSpriteNode
        goal = self.childNode(withName: "goal") as! SKSpriteNode

        let border = SKPhysicsBody(edgeLoopFrom: self.frame)
        border.friction = 0
        border.restitution = 0

        print("x: \(ball.position.x), y: \(ball.position.y)")

        danger1.physicsBody = SKPhysicsBody(circleOfRadius: danger1.size.width/2)
        danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
        danger2.physicsBody = SKPhysicsBody(circleOfRadius: danger2.size.width/2)
        danger2.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory

        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
        ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
        ball.physicsBody?.contactTestBitMask = PhysicsCategories.dangerCategory
        ball.physicsBody?.collisionBitMask = PhysicsCategories.none

        danger1.physicsBody?.isDynamic = true
        ball.physicsBody?.isDynamic = true
        goal.physicsBody?.isDynamic = true
        danger2.physicsBody?.isDynamic = true
        danger2.physicsBody!.affectedByGravity = false
        danger1.physicsBody!.affectedByGravity = false
        goal.physicsBody!.affectedByGravity = false
        ball.physicsBody!.affectedByGravity = false
        setupPhysics()

    }
    func setupPhysics() {
        physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
        physicsWorld.contactDelegate = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let location = touch.location(in: self)
            ball.position.x = location.x
            ball.position.y = location.y
        }
    }

    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

extension GameScene: SKPhysicsContactDelegate {



    func didBegin(_ contact: SKPhysicsContact) {
        let contactMask = contact.bodyA.contactTestBitMask | contact.bodyB.categoryBitMask

        if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.dangerCategory {
            print("Contact")
        }
    }

}

Settings.swift:

import SpriteKit

enum PhysicsCategories {
    static let none: UInt32 = 0
    static let ballCategory: UInt32 = 0x1           // 01
    static let dangerCategory: UInt32 = 0x1 << 1    // 10
}

1 个答案:

答案 0 :(得分:0)

您已正确设置所有位掩码,因此请保持原样。有问题的线是

let contactMask = contact.bodyA.contactTestBitMask | contact.bodyB.categoryBitMask

您正在使用按位算术,这意味着您要将这些位(UInt32)加在一起。签出(Bitwise Operators

您想要做的是比较两个categoryBitmasks,而不是contactBitMask和CategoryBitmask,因此只需将上面的行更改为此

let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

也可以创建更多类别,只需将位移过来

let Category4:UInt32 = 0x1 << 2
let Category5:UInt32 = 0x1 << 3

等等。