我正在建立一个迷宫,我已经添加了一些skspritekit节点的墙壁和一个点的球员。但是当点和墙碰撞时,没有检测到碰撞。我的代码如下:

import UIKit
import SpriteKit
import GameplayKit
import Foundation
import GameplayKit


class level1: SKScene, SKPhysicsContactDelegate {
    var entities = [GKEntity]()
    var graphs = [String : GKGraph]()
    var dot = SKSpriteNode()

override func sceneDidLoad () {
    buildMaze()
    addDot()

    func addDot() {
    let startNum = x * (y - 1)
    startCoord = coordArray[startNum]
    dot = SKSpriteNode(imageNamed: "redDot")
    dot.physicsBody?.isDynamic = true
    dot.size = CGSize(width: 20, height: 20)
    dot.position = startCoord
    dot.physicsBody = SKPhysicsBody(circleOfRadius: 10)
    dot.physicsBody?.mass = 0
    dot.physicsBody?.usesPreciseCollisionDetection = true
    self.addChild(dot)
}

 func buildMaze() {
    let difference =  coordArray[1].x - coordArray[0].x
    let wallDistance = difference/2
    let thickness = CGFloat(3)
    let length = difference  - CGFloat(thickness)/2



    var count = 0
    for point in coordArray {

        let northWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: length, height: thickness))
        northWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: length, height: thickness))
        northWall.physicsBody?.mass = 200000

        let southWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: length, height: thickness))
        southWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: length, height: thickness))
        southWall.physicsBody?.mass = 200000

        let eastWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: thickness, height: length ))
        eastWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: thickness, height: length))
        eastWall.physicsBody?.mass = 200000

        let westWall = SKSpriteNode(color: SKColor.black, size : CGSize (width: thickness, height: length ))
        westWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: thickness, height: length))
        westWall.physicsBody?.mass = 200000


        if !instructions[count].contains("N")  {
            //print("added north wall")
            northWall.position = CGPoint (x: point.x , y: point.y + wallDistance)
            if nodes(at: northWall.position) == [] {

                addChild(northWall)}
            else {print("north wall already there")}

        }
        if !instructions[count].contains("S")  {
            //print("added south wall")
            southWall.position = CGPoint (x: point.x , y: point.y - wallDistance)
            if nodes(at: southWall.position) == [] {

                addChild(southWall)}
            else {//print("southwall already there")

            }

        }
        if !instructions[count].contains("E")  {
            //print("added east wall")
            eastWall.position = CGPoint (x: point.x + wallDistance , y: point.y)
            if nodes(at: eastWall.position) == [] {

                addChild(eastWall)}
            else {//print("east already there")

            }

        }
        if !instructions[count].contains("W")  {
            //print("added west wall")
            westWall.position = CGPoint (x: point.x - wallDistance , y: point.y)
            if nodes(at: westWall.position) == [] {

                addChild(westWall)}
            else {//print("west wall already there")

            }

        }
        count = count + 1

    }
}


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

func didBegin(_ contact: SKPhysicsContact) {
    print("contact!")


}

墙壁看起来和我想要的一样,圆点也在正确的位置。
我为每一个添加了20000的masso,这样当我移动点时,墙就保持在原来的位置。然而,当我用手指移动这个点时,它只是穿过迷宫的墙壁,而不是被它们挡住。
我向didbegen函数添加了一个print语句,以查看它是否至少检测到了精灵之间的任何接触,但它没有。
这是为什么?
干杯!

最佳答案

首先,在DidMoveTo或SceneDidload中,需要设置PhysicContactDelegate:

override func sceneDidLoad () {
    physicsWorld.contactDelegate = self
    buildMaze()
    addDot()
}

要设置接触/碰撞遮罩,您必须这样做,因为它们基于按位操作:
假设你想要点和墙的碰撞
struct PhysicsCategory {
    static let wall: UInt32 = 0x1 << 1
    static let dot: UInt32 = 0x1 << 2
}

如果需要,可以将结构置于类之上
然后,分配物理体时,必须设置位掩码:
对于圆点:
    dot.physicsBody?.categoryBitMask = PhysicsCategory.dot
    dot.physicsBody?.contactTestBitMask = PhysicsCategory.wall
    dot.physicsBody?.collisionBitMask = PhysicsCategory.wall

    //Collision is different from contact, so if you want to avoid collision
    //dot.physicsBody?.collisionBitMask = PhysicsCategory.dot

碰撞与接触不同,检查apple documentation about it
对于墙:
    northWall.physicsBody?.categoryBitMask = PhysicsCategory.wall
    northWall.physicsBody?.contactTestBitMask = PhysicsCategory.dot
    northWall.physicsBody?.collisionBitMask = PhysicsCategory.dot

    //Do the same for all walls

如果希望墙与多个对象接触或碰撞:
    northWall.physicsBody?.contactTestBitMask = PhysicsCategory.dot | PhysicsCategory.other
    northWall.physicsBody?.collisionBitMask = PhysicsCategory.dot | PhysicsCategory.other

因为墙壁是有效的,所有的考虑都以点为准

关于swift - SKSpitekit节点之间未检测到冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52556207/

10-09 12:58