首页 > 解决方案 > 接触位掩码

问题描述

所以我在我的应用程序中使用物理 Body 和 contactbitmasks/physicscategories 但它们不能正常工作。我希望球在遇到危险时重置,或者在触球后移动到下一个级别。目前,我将其设置为仅出于测试原因打印语句。

我已经在代码中设置了它们,但它仍然无法正常工作。问题是什么?

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

    danger1.physicsBody = SKPhysicsBody()
    danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
    danger2.physicsBody = SKPhysicsBody()
    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

    goal.physicsBody = SKPhysicsBody()
    goal.physicsBody?.categoryBitMask = PhysicsCategories.goalCategory

    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
        print("x: \(ball.position.x), y: \(ball.position.y)")
    }
}

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

extension GameScene: SKPhysicsContactDelegate {



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

    if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.dangerCategory {
        print("Contact")
    } else if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.goalCategory {
        print("Goal!")
    }
}

}

标签: swiftxcodesprite-kit

解决方案


你的危险区域的物理体没有大小,那么怎么会发生碰撞呢?

danger1.physicsBody = SKPhysicsBody() should be danger1.physicsBody = SKPhysicsBody(rectangleOf:danger1.size)

现在对于一些高级注释,除非您的精灵实际上在移动,否则请关闭 isDynamic。在现实世界中,除非那堵墙倒塌在球的顶部,否则你的墙永远不会与球发生碰撞。

清理应如下所示:

physicsWorld.contactDelegate = self
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

danger1.physicsBody = SKPhysicsBody(rectangleOf:danger1.size)
danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger1.physicsBody?.isDynamic = false

danger2.physicsBody = SKPhysicsBody(rectangleOf:danger2.size)
danger2.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger2.physicsBody?.isDynamic = false

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
ball.physicsBody?.contactTestBitMask = PhysicsCategories.dangerCategory | PhysicsCategories.goalCategory
ball.physicsBody?.collisionBitMask = PhysicsCategories.none
ball.physicsBody?.isDynamic = true
ball.physicsBody!.affectedByGravity = false

goal.physicsBody = SKPhysicsBody(rectangleOf:goal.size)
goal.physicsBody?.categoryBitMask = PhysicsCategories.goalCategory
goal.physicsBody?.isDynamic = false
//setupPhysics()  Remove this

推荐阅读