【问题标题】:Nodes are colliding but not responding to didBeginContact function节点发生冲突但没有响应 didBeginContact 函数
【发布时间】:2016-11-29 05:34:11
【问题描述】:

当我运行它时,我的节点正在碰撞。玩家节点的硬币反弹。但是,当我想调用 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!)
            }
        }
    }
}
}

}

【问题讨论】:

    标签: swift sprite collision


    【解决方案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
    

    如果那个物理体由于某种原因是/变成 nil 并且你正在使用 !你会崩溃的。

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

      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 指南,你的方法应该以小写字母和类开头,结构应该以大写字母开头。

    希望对你有帮助

    【讨论】:

    • @cashoverride777 - 物体正在碰撞,但当它们碰撞时我仍然可以打印“Hello”……我不明白为什么它们的碰撞无法识别?
    • 您尝试过我的建议了吗?还要确保你的 1 个身体是动态的。
    • 是的,我做到了。我将硬币更改为动态,所以现在它从玩家身上弹起并继续下落(尚未实现从父函数中删除)但它甚至没有打印你好......所以我不能为碰撞写任何规则,因为它不是认识它..
    • 你用的是swift 3吗?
    • 联系方法现在称为 func didBegin(_contact: SKPhysicsContact) {...}。我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2019-01-27
    • 2015-10-15
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    相关资源
    最近更新 更多