【问题标题】:sprite doesnt detect collision精灵没有检测到碰撞
【发布时间】:2020-01-11 23:18:01
【问题描述】:

我是 sprite kit 的新手,我正在使用 Swift。尝试在英雄与右墙碰撞时检测到碰撞。当英雄与其他物体发生碰撞时,我可以检测到碰撞,但由于某种原因它不会检测到右墙。

再次,英雄会检测到与左墙的碰撞,但与右墙没有任何反应。 我被难住了...

在我的游戏场景中,这就是我设置所有内容的方式

    class GameScene: SKScene, SKPhysicsContactDelegate {

    enum contactType: UInt32 {
            case heroCategory = 1
            case fireCategory = 2
            case rightTrapCategory = 4
            case leftTrapCategory = 8
            case leftWallCategory = 16
            case rightWallCategory = 32
        }


        let leftTrap = SKSpriteNode()
        let rightTrap = SKSpriteNode()
        let fire = SKSpriteNode()
        let hero = SKSpriteNode()
        let leftWall = SKSpriteNode()
        let rightWall = SKSpriteNode()

  override func didMove(to view: SKView) {

        physicsWorld.contactDelegate = self


        addLeftWall()
        addRightWall()
        addFire()
        addHero()
        addLeftTrap()
        addrightTrap()

}

这是我检测、排序和处理这些冲突的地方。

func collision(between Hero: SKNode, object: SKNode) {



    switch object.name {
    case "fire":
        addHighscore()
        destroy(hero: Hero)
        gameOver()
    case "rightTrap":
        jumpCounter = 0
        hero.color = .cyan
        print("right trap")
    case "leftTrap":
        jumpCounter = 0
        hero.color = .cyan
        print("left Trap")
    case "leftWall":
        hero.color = .red
        print("left wall")
    case "rightWall":
        hero.color = .red
        print("right wall")
    default:
        print("something went wrong")
    }




}

func didBegin(_ contact: SKPhysicsContact) {
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {

        collision(between: contact.bodyA.node!, object:contact.bodyB.node!)

    }else if contact.bodyB.categoryBitMask < contact.bodyA.categoryBitMask{

        collision(between: contact.bodyB.node!, object: contact.bodyA.node!)

    }


}

我如何设置精灵

func addHero(){

        hero.name = "hero"
        hero.zPosition = 1
        hero.color = .cyan
        hero.size = CGSize(width: 25, height: 50)
        hero.position = CGPoint(x: frame.midX, y: (frame.midY + 100))



        hero.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 25, height: 50))
        hero.physicsBody?.restitution = 0.1
        hero.physicsBody?.allowsRotation = false

        hero.physicsBody?.categoryBitMask = contactType.heroCategory.rawValue


        hero.physicsBody?.contactTestBitMask = contactType.fireCategory.rawValue

        hero.physicsBody?.contactTestBitMask = contactType.leftTrapCategory.rawValue
        hero.physicsBody?.contactTestBitMask = contactType.rightTrapCategory.rawValue

        hero.physicsBody?.contactTestBitMask = contactType.rightWallCategory.rawValue
        hero.physicsBody?.contactTestBitMask = contactType.leftWallCategory.rawValue


        addChild(hero)


    }


 func addRightWall(){

        rightWall.name = "rightWall"
        rightWall.color = .red
        rightWall.size = CGSize(width: 25, height: (frame.maxX / 2) + 25 )
        rightWall.position = CGPoint(x: frame.maxX, y: (frame.midX + 200))
        rightWall.anchorPoint = CGPoint(x: 1, y: 0.5)


        let centerPoint = CGPoint(x: rightWall.size.width / 2 - (rightWall.size.width * rightWall.anchorPoint.x), y: rightWall.size.height / 2 - (rightWall.size.height * rightWall.anchorPoint.y))


        rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size, center: centerPoint)

        rightWall.physicsBody?.isDynamic = false
        rightWall.physicsBody?.categoryBitMask = contactType.rightWallCategory.rawValue



        addChild(rightWall)


    }

你可以看到,左墙精灵。

再次碰撞适用于左墙,但不适用于右墙。

func addLeftWall(){

        leftWall.name = "leftWall"
        leftWall.color = .red
        leftWall.size = CGSize(width: 25, height: (frame.maxX / 2) + 25 )
        leftWall.position = CGPoint(x: frame.minX, y: (frame.midX + 200))
        leftWall.anchorPoint = CGPoint(x: 0, y: 0.5)


        let centerPoint = CGPoint(x: leftWall.size.width / 2 - (leftWall.size.width * leftWall.anchorPoint.x), y: leftWall.size.height / 2 - (leftWall.size.height * leftWall.anchorPoint.y))


        leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size, center: centerPoint)




        leftWall.physicsBody?.isDynamic = false
        leftWall.physicsBody?.categoryBitMask = contactType.leftWallCategory.rawValue



        addChild(leftWall)



    }

【问题讨论】:

  • 区分接触和碰撞。它们不一样。
  • 你是对的,我错误地将其标记为碰撞问题。
  • 你完全误解了我的建议。你最好把你的 Sprite Kit 书读一遍。
  • 您已经做出了这样的声明,即您希望您的节点相互碰撞,而实际上您只使用contactTestBitMask 进行该工作。所以你的问题根本没有意义。
  • @ElTomato 英雄节点能够四处移动并与其他节点发生碰撞。我只想知道英雄节点是否与我列出的节点有联系。我将每个相应的节点设置为它们的类别,然后设置英雄节点 contactTestBitMap 以侦听每个其他节点类别。这很有效,当英雄接触到火、leftTrap、rightTrap、leftWall 时,我会收到通知,但是由于某种原因,我无法让它响应我的 rightwall 节点

标签: swift sprite-kit collision-detection


【解决方案1】:

编辑

我没有意识到我不应该把它写出来……

hero.physicsBody?.contactTestBitMask = contactType.fireCategory.rawValue

    hero.physicsBody?.contactTestBitMask = contactType.leftTrapCategory.rawValue
    hero.physicsBody?.contactTestBitMask = contactType.rightTrapCategory.rawValue

    hero.physicsBody?.contactTestBitMask = contactType.rightWallCategory.rawValue
    hero.physicsBody?.contactTestBitMask = contactType.leftWallCategory.rawValue

但是需要用 | 分隔每个

hero.physicsBody?.contactTestBitMask = contactType.fireCategory.rawValue | contactType.leftTrapCategory.rawValue | contactType.rightTrapCategory.rawValue |  contactType.rightWallCategory.rawValue | contactType.leftWallCategory.rawValue

【讨论】:

    猜你喜欢
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多