Swift 接触位掩模

Swift 接触位掩模,swift,xcode,sprite-kit,Swift,Xcode,Sprite Kit,所以我在我的应用程序中使用了physics Body和contactbitmasks/physicscategories,但它们工作不正常。我希望球在遇到危险时复位,或者一旦碰到球门,球就进入下一关。目前,出于测试原因,我将其设置为打印语句 我已经在代码中设置好了它们,但仍然不起作用。有什么问题 import SpriteKit import GameplayKit class GameScene: SKScene { var ball = SKSpriteNode() var danger

所以我在我的应用程序中使用了physics 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!")
    }
}
导入SpriteKit
导入游戏工具包
类游戏场景:SKScene{
var ball=SKSpriteNode()
var danger1=SKSpriteNode()
var danger2=SKSpriteNode()
var goal=SKSpriteNode()
覆盖func didMove(到视图:SKView){
ball=self.childNode(名称为“ball”)as!SKSpriteNode
danger1=self.childNode(名称为:“danger1”)作为!SKSpriteNode
danger2=self.childNode(名称为“danger2”)作为!SKSpriteNode
goal=self.childNode(名称为“goal”)as!SKSpriteNode
let border=SKPhysicsBody(edgeLoopFrom:self.frame)
边界摩擦力=0
border.restitution=0
danger1.physicsBody=SKPhysicsBody()
danger1.physicsBody?.categoryBitMask=PhysicsCategories.dangerCategory
danger2.physicsBody=SKPhysicsBody()
danger2.physicsBody?.categoryBitMask=PhysicsCategories.dangerCategory
ball.physicsBody=SKPhysicsBody(圆形半径:ball.size.width/2)
ball.physicsBody?.categoryBitMask=PhysicsCategories.ballCategory
ball.physicsBody?.contactTestBitMask=PhysicsCategories.dangerCategory
ball.physicsBody?.collisionBitMask=PhysicsCategories.none
goal.physicsBody=SKPhysicsBody()
目标.physicsBody?.categoryBitMask=物理类别.goalCategory
危险1.physicsBody?.isDynamic=真
ball.physicsBody?.isDynamic=true
goal.physicsBody?.isDynamic=true
危险2.physicsBody?.isDynamic=真
危险2.physicsBody!.affectedByGravity=false
危险1.physicsBody!.affectedByGravity=false
goal.physicsBody!.affectedByGravity=false
ball.physicsBody!。受重力影响=false
物理(英文)
}
函数设置物理(){
physicsWorld.gravity=CGVector(dx:0.0,dy:0.0)
physicsWorld.contactDelegate=self
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
ball.position.x=位置.x
ball.position.y=位置.y
打印(“x:\(ball.position.x),y:\(ball.position.y)”)
}
}
覆盖函数更新(uCurrentTime:TimeInterval){
//在渲染每个帧之前调用
}
}
扩展游戏场景:SKPhysicContactDelegate{
func didbegen(uu联系人:skphysiccontact){
让contactMask=contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
如果contactMask==PhysicCategories.ballCategory | PhysicCategories.dangerCategory{
打印(“联系人”)
}否则,如果contactMask==PhysicCategories.ballCategory | PhysicCategories.goalCategory{
打印(“目标!”)
}
}

}

你的物理身体对于你的危险区域来说没有大小,那么什么东西会发生碰撞呢

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

甚至没有人知道什么是物理分类。球分类和物理分类。危险分类。它直截了当地说它是一个SpriteNode?别以为你还需要知道什么。除非我错了,但是…Schmobr-你的游戏场景需要是一个
skphysiccontactdelegate
,除了KoD下面的答案。好的!我会尝试它,一旦我回到我的编码。感谢您的帮助,我会让您知道它是否有效:)当我删除setupPhysics()时,不会打印任何内容/不会发生任何碰撞。我更改了SKPhysicsBody,现在允许打印项目,但如果我将打印语句更改为类似于
ball.position=CGPoint(x:0,y:550)
什么都不会发生。此外,当球碰到球门时,什么也不会发生。您是否将联系人代理移动到我在这里列出的主要功能?“不知道你的goalIt问题”说“无法将“GameSecene”类型的值分配给“SkPhysicContactDelegate”类型“不知道你做了什么,这是你的代码,我只是将行移出了设置物理。您不知何故丢失了以前分配给GameSecene的代理