节点发生冲突但没有响应didBeginContact函数

时间:2016-11-28 09:38:05

标签: swift sprite collision

我的节点在运行时发生碰撞。硬币弹出玩家节点。但是,当我想调用didBeginContact函数时,它没有响应... 每次硬币击中玩家时,我最终都希望标签显示+1分数。当与玩家发生碰撞时,硬币也会消失。但是我的联系人没有工作,所以我不能制定任何碰撞规则让标签显示得分。

import SpriteKit
import GameplayKit

// Collision categories

struct physicsCategory {
static let playerCat : UInt32 = 1
static let coinCat : UInt32 = 2
}

class GameScene: SKScene, controls, SKPhysicsContactDelegate {

let player  = SKSpriteNode(imageNamed:"trump")
let points = SKLabelNode()
let buttonDirLeft = SKSpriteNode(imageNamed: "left")
let buttonDirRight = SKSpriteNode(imageNamed: "right")
let background = SKSpriteNode(imageNamed: "background")
var pressedButtons = [SKSpriteNode]()
let popUpMenu = SKSpriteNode(imageNamed: "popupmenu")
var score = 0
var gameOver = false
var startGame = false
var rules = false

override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self

//score label
points.position = CGPoint(x: 530, y: 260)
points.text =  ("\(score)")
points.zPosition = 6
points.fontColor = UIColor.black
points.fontSize = 50
addChild(points)

//Set Background
background.zPosition = 1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height /  2)
background.size.width = 580
background.size.height = 320

addChild(background)

// Player
player.position = CGPoint(x: 250, y: 40)
player.zPosition = 2
player.size.width = 40
player.size.height = 60
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody?.affectedByGravity = false
player.physicsBody!.categoryBitMask = physicsCategory.playerCat
player.physicsBody!.contactTestBitMask = physicsCategory.coinCat
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.isDynamic = false
self.addChild(player)

//contact has started
func didBeginContact(contact: SKPhysicsContact){
    let firstBody: SKPhysicsBody = contact.bodyA
    let secondBody: SKPhysicsBody = contact.bodyB

    if ((firstBody.categoryBitMask == physicsCategory.playerCat) &&  (secondBody.categoryBitMask == physicsCategory.coinCat)){
        CollisionWithCoin(player: firstBody.node as! SKSpriteNode, coins: secondBody.node as! SKSpriteNode)
    }
   }

func CollisionWithCoin(player: SKSpriteNode, coins:SKSpriteNode){
    NSLog("Hello")
}


//repeat coing spawning
run(SKAction.repeatForever(
    SKAction.sequence([
        SKAction.run(spawnCoins),
        SKAction.wait(forDuration: 1.0)])))
}
//coin settings
func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

//spawn coins
func spawnCoins() {
    // 2
    let coins = SKSpriteNode(imageNamed: "coins")
    coins.zPosition = 2
    coins.size.width = 25
    coins.size.height = 25
    coins.physicsBody = SKPhysicsBody(rectangleOf: coins.size )
    coins.physicsBody!.categoryBitMask = physicsCategory.coinCat
    coins.physicsBody!.contactTestBitMask = physicsCategory.playerCat
    coins.physicsBody?.collisionBitMask = 1
    coins.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height + coins.size.height/2)

    let action = SKAction.moveTo(y: -350, duration: TimeInterval(random(min: 1, max: 5)))

    let remove = SKAction.run({coins.removeFromParent()})

    let sequence = SKAction.sequence([action,remove])


    coins.run(sequence)

    addChild(coins)

}

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

    if pressedButtons.index(of: buttonDirLeft) != nil {
        player.position.x -= 4.0
    }
    if pressedButtons.index(of: buttonDirRight) != nil {
        player.position.x += 4.0
    }

}
//MOVEMENT FUNCTIONS START HERE
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            // I check if they are already registered in the list
            if button.contains(location) && pressedButtons.index(of: button) == nil {
                pressedButtons.append(button)

                }
            }
        }
    }


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            // if I get off the button where my finger was before
            if button.contains(previousLocation)
                && !button.contains(location) {
                // I remove it from the list
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }

                // if I get on the button where I wasn't previously
            else if !button.contains(previousLocation)
                && button.contains(location)
                && pressedButtons.index(of: button) == nil {
                // I add it to the list
                pressedButtons.append(button)

            }}}}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            if button.contains(location) {
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }
            else if (button.contains(previousLocation)) {
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }
        }
    }
}


override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

    for button in [buttonDirLeft, buttonDirRight] {
        if button.contains(location) {
            let index = pressedButtons.index(of: button)
            if index != nil {
                pressedButtons.remove(at: index!)
            }
        }
        else if (button.contains(previousLocation)) {
            let index = pressedButtons.index(of: button)
            if index != nil {
                pressedButtons.remove(at: index!)
            }
        }
    }
}
}

}

1 个答案:

答案 0 :(得分:1)

你能尝试像这样定义你的位掩码。

  enum PhysicsCategory {
      static let playerCat: UInt32 =  0x1 << 0
      static let coinCat: UInt32 =  0x1 << 1
}

您可以在联系方式中尝试此代码吗?另请注意,如果您使用的是Swift 3,则联系方式名称的名称已更改。

  //contact has started
  func didBegin(_ contact: SKPhysicsContact) {
        let firstBody: SKPhysicsBody = contact.bodyA
        let secondBody: SKPhysicsBody = contact.bodyB


        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        if ((firstBody.categoryBitMask == physicsCategory.playerCat) &&    (secondBody.categoryBitMask == physicsCategory.coinCat)){
            CollisionWithCoin(player: firstBody.node as! SKSpriteNode, coins: secondBody.node as! SKSpriteNode)
    }
   }
}

你也在使用一些!在你的代码中,它使安全性降低。试试用吗?在处理期权时,只要可能,“if let”。因此,例如,即使你知道你刚刚创建它,也可以写下这样的物理实体。你有时会这样做,而你正在使用的其他时间!,保持一致。

  player.physicsBody?.categoryBitMask...
  etc

如果由于某种原因,物理机构是/变为零并且你正在使用!你会崩溃。

我也会写这样的联系方式,以确保如果联系方法在同一次碰撞中多次触发,你也不会崩溃。

  func collisionWithCoin(player: SKSpriteNode?, coins:SKSpriteNode?){
         guard let player = player, let coins = coins else { return }

         print("Hello")
  } 

而不是在didBeginContact方法中调用它

 collisionWithCoin(player: firstBody.node as? SKSpriteNode, coins: secondBody.node as? SKSpriteNode)

最后我还会尝试遵循swift指南,你的方法应该以小写字母和类开头,结构应该以大写字母开头。

希望这有帮助