简体   繁体   English

Xcode,无法运行 didBegin(_contact SKPysics) 函数

[英]Xcode, Unable to run the didBegin(_contact SKPysics) function

class GameScene: SKScene {
    
    let player = SKSpriteNode(imageNamed: "spaceship")
    
    struct PhysicsCategories {
        static let None: UInt32 = 1
        static let Player: UInt32 = 0b1
        static let Obsticles: UInt32 = 0b10
        static let Diamond: UInt32 = 0b100
    }
    
    func didBegin(_ contact: SKPhysicsContact) {
        
        var body1 = SKPhysicsBody()
        var body2 = SKPhysicsBody()
        
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            body1 = contact.bodyA
            body2 = contact.bodyB
        }
        else {
            body1 = contact.bodyB
            body2 = contact.bodyA
        }
        
        if body1.categoryBitMask == PhysicsCategories.Player && body2.categoryBitMask == PhysicsCategories.Obsticles {
            body1.node?.removeFromParent()
            body2.node?.removeFromParent()
        }
        
    }

I want to remove the diamond once it come in contact with the player and also the player when it comes in contact with the obstacle.我想在钻石与玩家接触时以及在与障碍物接触时移除钻石。 Currently im unable to register the contact between any of them Im currently using Xcode version 9.4.1.目前我无法注册他们任何人之间的联系我目前使用 Xcode 9.4.1 版。 Will the problem stay if I update the app to如果我将应用程序更新为

You've got a lot of things missing here (unless you've a lot of code you're not showing) :您在这里缺少很多东西(除非您有很多代码没有显示):

  1. Contacts only occur between physics bodies and you don't have any接触只发生在物理体之间,你没有任何
  2. Your class isn't an SKPhysicsContactDelegate and you haven't made yourself the physics contact delegate.您的课程不是 SKPhysicsContactDelegate,并且您还没有让自己成为物理联系代表。
  3. You haven't defined which contacts you want your code to be notified for.您尚未定义您希望向哪些联系人通知您的代码。 By default, all physics bodies 'bounce off' each other (collisions) but you don't get notified when ANY bodies touch (contacts).默认情况下,所有物理物体都会相互“反弹”(碰撞),但是当任何物体接触(接触)时,您不会收到通知。

I've written more details here : How to Detect collision in Swift, Sprite kit我在这里写了更多细节: 如何在 Swift、Sprite 套件中检测碰撞

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM