SWIFT Как различить, с какой частью узла связываются - PullRequest
0 голосов
/ 25 февраля 2020

Прямо сейчас, когда я загружаю свою игру в симулятор, мой кирпич падает как обычно. Однако, когда мой кирпич попадает в квадрат, квадрат исчезает вместо кирпича, независимо от того, попадает ли он в правильную сторону. Иногда приложение просто падает, если оно не попадает в правильную сторону. Могу ли я получить некоторую помощь по разграничению контактных точек на одном узле, чтобы игра распознавала попадание моего кирпича в правильную сторону квадрата.

Изображение игры: изображение игры

Не беспокойтесь о неправильном размере узлов. Я исправлю это в конце концов.

class GameScene: SKScene {
    let brickTop = SKSpriteNode(imageNamed: "Top Side");
    let brickBottom = SKSpriteNode(imageNamed: "Bottom Side");
    let brickLeft = SKSpriteNode(imageNamed: "Left Side");
    let brickRight = SKSpriteNode(imageNamed: "Right Side copy 2");
    let basicBrick = SKSpriteNode(imageNamed: "Basic Brick")

    override func didMove(to view: SKView) {
        isUserInteractionEnabled = true
        layoutScene()

    }

        func spawnBrick()  {
            let randomFunc = [self.spawnbrickTop, self.spawnbrickBottom, self.spawnbrickLeft, self.spawnbrickRight]
            let randomResult = Int(arc4random_uniform(UInt32(randomFunc.count)))
            randomFunc[randomResult]()
        }



        func spawnbrickTop() {

            brickTop.size = CGSize(width: 210, height: 105)
            brickTop.name = "BrickTop"
            brickTop.position = CGPoint(x: frame.midX, y: frame.maxY)
            brickTop.zPosition = 0
            //physics stuff begins here
            brickTop.physicsBody = SKPhysicsBody(rectangleOf: brickTop.size)
            brickTop.physicsBody?.categoryBitMask = PhysicsCategories.brickTopCategory
            brickTop.physicsBody?.contactTestBitMask = PhysicsCategories.basicTopCategory
            brickTop.physicsBody?.contactTestBitMask = PhysicsCategories.basicBottomCategory
            brickTop.physicsBody?.contactTestBitMask = PhysicsCategories.basicLeftCategory
            brickTop.physicsBody?.contactTestBitMask = PhysicsCategories.basicRightCategory
            brickTop.physicsBody?.collisionBitMask = PhysicsCategories.none
             //bye bye physics
            self.addChild(brickTop)

        }

        func spawnbrickBottom() {

            brickBottom.size = CGSize(width: 230, height: 101)
            brickBottom.name = "BrickBottom"
            brickBottom.position = CGPoint(x: frame.midX, y: frame.maxY)
            brickBottom.zPosition = 0
            //physics stuff begins here
            brickBottom.physicsBody = SKPhysicsBody(rectangleOf: brickBottom.size)
            brickBottom.physicsBody?.categoryBitMask = PhysicsCategories.brickBottomCategory
            brickBottom.physicsBody?.contactTestBitMask = PhysicsCategories.basicTopCategory
            brickBottom.physicsBody?.contactTestBitMask = PhysicsCategories.basicBottomCategory
            brickBottom.physicsBody?.contactTestBitMask = PhysicsCategories.basicLeftCategory
            brickBottom.physicsBody?.contactTestBitMask = PhysicsCategories.basicRightCategory
            brickBottom.physicsBody?.collisionBitMask = PhysicsCategories.none
             //bye bye physics
            self.addChild(brickBottom)
        }

        func spawnbrickLeft() {

            brickLeft.size = CGSize(width: 210, height: 70)
            brickLeft.name = "BrickLeft"
            brickLeft.position = CGPoint(x: frame.midX, y: frame.maxY)
            brickLeft.zPosition = 0
            //physics stuff begins here
            brickLeft.physicsBody = SKPhysicsBody(rectangleOf: brickLeft.size)
            brickLeft.physicsBody?.categoryBitMask = PhysicsCategories.brickLeftCategory
            brickLeft.physicsBody?.contactTestBitMask = PhysicsCategories.basicTopCategory
            brickLeft.physicsBody?.contactTestBitMask = PhysicsCategories.basicBottomCategory
            brickLeft.physicsBody?.contactTestBitMask = PhysicsCategories.basicLeftCategory
            brickLeft.physicsBody?.contactTestBitMask = PhysicsCategories.basicRightCategory
            brickLeft.physicsBody?.collisionBitMask = PhysicsCategories.none
             //bye bye physics
            self.addChild(brickLeft)

        }


        func spawnbrickRight() {

            brickRight.size = CGSize(width: 140, height: 95)
            brickRight.name = "BrickRight"
            brickRight.position = CGPoint(x: frame.midX, y: frame.maxY)
            brickRight.zPosition = 0
            //physics stuff begins here
            brickRight.physicsBody = SKPhysicsBody(rectangleOf: brickRight.size)
            brickRight.physicsBody?.categoryBitMask = PhysicsCategories.brickRightCategory
            brickRight.physicsBody?.contactTestBitMask = PhysicsCategories.basicTopCategory
            brickRight.physicsBody?.contactTestBitMask = PhysicsCategories.basicBottomCategory
            brickRight.physicsBody?.contactTestBitMask = PhysicsCategories.basicLeftCategory
            brickRight.physicsBody?.contactTestBitMask = PhysicsCategories.basicRightCategory
            brickRight.physicsBody?.collisionBitMask = PhysicsCategories.none
             //bye bye physics
            self.addChild(brickRight)

        }
    func spawnBasicBrick() {
            basicBrick.size = CGSize(width: 200, height: 177.6)
            basicBrick.position = CGPoint(x: frame.midX, y: frame.minY + basicBrick.size.width)
            basicBrick.zPosition = 1

            basicBrick.physicsBody = SKPhysicsBody(rectangleOf: basicBrick.size)
            basicBrick.physicsBody?.categoryBitMask = PhysicsCategories.basicBrickCategory
            basicBrick.physicsBody?.isDynamic = false
            basicBrick.physicsBody?.allowsRotation = true

            addChild(basicBrick)
        }

    extension GameScene: SKPhysicsContactDelegate {

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

            if contactMask == PhysicsCategories.brickTopCategory |
                (PhysicsCategories.basicTopCategory) {
                if let brickTop = contact.bodyA.node?.name == "BrickTop" ?
                    contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as?
                    SKSpriteNode {

                        print("Correct!")
                        brickTop.run(SKAction.fadeOut(withDuration: 0.15), completion: {
                            brickTop.removeFromParent()
                            self.spawnBrick()
                        })
                }
                    else if contactMask == PhysicsCategories.brickBottomCategory |
                    (PhysicsCategories.basicBottomCategory) {
                    if let brickBottom = contact.bodyA.node?.name == "BrickBottom" ?
                        contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as?
                        SKSpriteNode {

                            print("Correct!")
                            brickBottom.run(SKAction.fadeOut(withDuration: 0.15), completion: {
                                brickBottom.removeFromParent()
                                self.spawnBrick()
                            })
                        }
                    else if contactMask == PhysicsCategories.brickLeftCategory |
                                   (PhysicsCategories.basicBottomCategory) {
                                   if let brickLeft = contact.bodyA.node?.name == "BrickLeft" ?
                                       contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as?
                                       SKSpriteNode {

                                           print("Correct!")
                                           brickLeft.run(SKAction.fadeOut(withDuration: 0.15), completion: {
                                               brickLeft.removeFromParent()
                                            self.spawnBrick()
                                           })
                                       }
                   else if contactMask == PhysicsCategories.brickRightCategory |
                                  (PhysicsCategories.basicRightCategory) {
                                  if let brickRight = contact.bodyA.node?.name == "BrickRight" ?
                                      contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as?
                                      SKSpriteNode {

                                          print("Correct!")
                                          brickRight.run(SKAction.fadeOut(withDuration: 0.15), completion: {
                                              brickRight.removeFromParent()
                                            self.spawnBrick()
                                          })
                                      }
                    else {
                        gameOver()
                    }


                                        }
                                    }
                            }
                        }
                      }
     }
...