如何检测两个对象何时在SpriteKit中触摸

时间:2023-01-16 23:08: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.

我在互联网上没有发现如何做到这一点。我只是试图在物理身体触摸时运行一行代码。在这种情况下,我有一个带有物理主体的SKSpriteNode和另一个用于地面的物理主体。当他们触摸它时应运行代码行,这是我迄今为止所发现的。

    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

那么我应该用catGroup和groundGroup替换第一个身体和第二个身体吗?我不知道该怎么做

1 个解决方案

#1


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.

不要成为一个坚持者,但你不应该开始提出一个问题“我在互联网上找不到任何东西”,当那些公然不真实时。作为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

希望这可以帮助

#1


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.

不要成为一个坚持者,但你不应该开始提出一个问题“我在互联网上找不到任何东西”,当那些公然不真实时。作为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

希望这可以帮助