How to detect when two objects touch in SpriteKit

被刻印的时光 ゝ 提交于 2019-12-24 15:00:38

问题


I have found nothing on the internet about how to do this. Im simply trying to run a line of code when to physics body touch. In this case I have an SKSpriteNode with a physics body and another for the ground. When they touch it should run the line of code this is all I have found so far.

    let catGroup:UInt32 = 0x1 << 0
let groundGroup:UInt32 = 0x2 << 1
    cat.physicsBody?.categoryBitMask = catGroup
    cat.physicsBody?.contactTestBitMask = groundGroup
    ground.physicsBody?.categoryBitMask = groundGroup
    ground.physicsBody?.contactTestBitMask = catGroup

and here is where I'm confused

    func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

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

    if firstBody.categoryBitMask==0 && secondBody.categoryBitMask==1 {
        print("contact")
    }

}

so should i be replacing first body and second body with catGroup and groundGroup? Im not sure how to do this


回答1:


Not to be a stickler but you shouldn't start of a question with "I couldn't find anything on the internet" when thats blatantly not true. There is a million tutorials on collision detection around as its one of the basics in SpriteKit.

Now to your question. You did not give your sprites an actual physics body and your physics categories are set up weird. Change your code to this

 struct PhysicsCategory {
       static let cat:UInt32 = 0x1 << 0
       static let ground:UInt32 = 0x1 << 1
 }

 class GameScene: SKScene, SKPhysicsContactDelegate {


     override func didMoveToView(view: SKView) {
     /* Setup your scene here */

         physicsWorld.contactDelegate = self


         cat.physicsBody = SKPhysicsBody(rectangleOfSize: cat.size) // FORGOT THIS
         cat.physicsBody?.categoryBitMask = PhysicsCategory.cat
         cat.physicsBody?.contactTestBitMask = PhysicsCategory.ground

         ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size) // FORGOT THIS
         ground.physicsBody?.categoryBitMask = PhysicsCategory.ground
         ground.physicsBody?.contactTestBitMask = PhysicsCategory.cat // You dont really need this line as long as you have set it on the other body.
    }

    func didBeginContact(contact: SKPhysicsContact) {
         var firstBody: SKPhysicsBody
         var secondBody: SKPhysicsBody

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

         if firstBody.categoryBitMask == PhysicsCategory.cat && secondBody.categoryBitMask == PhysicsCategory.ground {
             print("contact")
         }
     } 
 }

If you dont want your objects to fall you have to turn gravity off, which is on by default.

 cat.physicsBody?.affectedByGravity = false
 ground.physicsBody?.affectedByGravity = false

Hope this helps



来源:https://stackoverflow.com/questions/36242209/how-to-detect-when-two-objects-touch-in-spritekit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!