即使设置了接触面罩,Spritekit 节点也不会发生碰撞

Spritekit nodes are not colliding even though contact mask are set up

在下面的代码中,我有几个节点,但现在我只是想检测玩家和敌人子弹之间的碰撞(我将其称为 "BBullet"),但在检测碰撞的功能下, 什么都不弹出。

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var player = SKSpriteNode()

    var firebutton = SKShapeNode()
    var bullet = SKSpriteNode()

    enum CategoryMask : UInt32 {
        case player = 1
        case GBullet = 2
        case BBullet = 3
        case enemyships = 4
    }

    override func didMove(to view: SKView) {
        player = SKSpriteNode(color: .red, size: CGSize(width: 150, height: 150))
        player.physicsBody?.affectedByGravity = false
        player.position = CGPoint(x: 100, y: 375)
        player.physicsBody?.isDynamic = true
        player.physicsBody?.restitution = 1
        player.physicsBody?.categoryBitMask = CategoryMask.player.rawValue
        player.physicsBody?.collisionBitMask = CategoryMask.BBullet.rawValue
        player.physicsBody?.contactTestBitMask = CategoryMask.BBullet.rawValue
        player.name = "Player"

        addChild(player)
        firebutton = SKShapeNode(circleOfRadius: 75)
        firebutton.position = CGPoint(x: 1000, y: 150)
        firebutton.alpha = 0.7
        firebutton.physicsBody?.isDynamic = false
        firebutton.physicsBody?.affectedByGravity = false
        firebutton.physicsBody?.categoryBitMask = 0
        firebutton.physicsBody?.collisionBitMask = 100
        firebutton.physicsBody?.contactTestBitMask = 100
        firebutton.physicsBody?.contactTestBitMask = 0
        firebutton.physicsBody?.collisionBitMask = 0
        addChild(firebutton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            spawnenemybullets()
            let position = touch.location(in: self)
            if firebutton.contains(position){
                bullet = SKSpriteNode(color: .orange, size: CGSize(width: 20, height: 10))
                bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
                bullet.physicsBody?.affectedByGravity = false
                bullet.physicsBody?.isDynamic = true
                bullet.physicsBody?.allowsRotation = false
                bullet.position.x = player.position.x + 90
                bullet.position.y = player.position.y
                bullet.physicsBody?.mass = 0.01
                bullet.physicsBody?.categoryBitMask = CategoryMask.GBullet.rawValue
                bullet.physicsBody?.collisionBitMask = CategoryMask.enemyships.rawValue
                bullet.physicsBody?.contactTestBitMask = CategoryMask.enemyships.rawValue
                bullet.name = "FBullet"
                addChild(bullet)
                bullet.physicsBody?.applyForce(CGVector(dx: 400, dy: 0))

            }
        }
    }

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

             if player.contains(position) {

                if position.y > 85 && position.y < 665 && position.x > 85 && position.x < 640{
                    player.position = position
                }
            }
        }
    }

    func spawnenemybullets () {
        bullet = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 10))
        bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
        bullet.physicsBody?.affectedByGravity = false
        bullet.physicsBody?.isDynamic = true
        bullet.physicsBody?.allowsRotation = false
        bullet.position.x = 1300
        bullet.position.y = 200
        bullet.physicsBody?.mass = 0.01
        bullet.physicsBody?.categoryBitMask = CategoryMask.BBullet.rawValue
        bullet.physicsBody?.collisionBitMask = CategoryMask.player.rawValue
        bullet.physicsBody?.contactTestBitMask = CategoryMask.player.rawValue
        bullet.name = "EnemyBullet"
        addChild(bullet)
        bullet.physicsBody?.applyForce(CGVector(dx: -400, dy: 0))
    }

    func didBegin(_ contact: SKPhysicsContact) {
        print("Contact Happened")
    }

}

这两个 Sprite 都设置了接触面罩,但由于某种原因它们没有发生碰撞。当我在我的 phone 上测试该项目时,它们直接通过彼此。如何让他们相互联系?

您还没有为 player(或 fireButton)创建 physicsBody

您还需要让自己成为物理联系代表:

class GameScene: SKScene, SKPhysicsContactDelegate { physicsWorld.contactDelegate = self

编辑:我的碰撞和接触分步指南:

碰撞和接触测试位掩码指南:

操纵位掩码以关闭和打开单个碰撞和接触。