Swift 3 SpriteKit 检测接触

Swift 3 SpriteKit Detecting Contacts

我是 swift 的新手,我一直在尝试弄清楚如何使用位掩码和 didBegin(_ contact: SKPhysicsContact) 来检测两艘宇宙飞船何时相互接触。我似乎无法弄清楚如何。

这是我目前的情况:

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let pufferCategory: UInt32 = 1 << 0;

let enemyCategory: UInt32 = 1 << 1;

var spaceship1: SKSpriteNode!
var spaceship2: SKSpriteNode!

override func didMove(to view: SKView) {
    spaceship1 = SKSpriteNode(imageNamed: "Spaceship");
    spaceship1.setScale(CGFloat(0.1))
    spaceship1.position = CGPoint(x: self.frame.width / 2, y: (self.frame.height / 2));
    spaceship1.name = "spaceship1";


    spaceship1.physicsBody = SKPhysicsBody(circleOfRadius: spaceship1.size.width / 2);
    spaceship1.physicsBody?.isDynamic = true // apply gravity, friction, and collision
    spaceship1.physicsBody?.affectedByGravity = false;
    spaceship1.physicsBody?.allowsRotation = false
    spaceship1.physicsBody = SKPhysicsBody(circleOfRadius: spaceship1.size.width / 2);
    spaceship1.physicsBody?.categoryBitMask = pufferCategory
    spaceship1.physicsBody?.contactTestBitMask = enemyCategory

    spaceship2 = SKSpriteNode(imageNamed: "Spaceship");
    spaceship2.setScale(CGFloat(0.1))
    spaceship2.position = CGPoint(x: self.frame.width / 2, y: (spaceship1.position.y + 300));
    spaceship2.name = "puffer2";

    spaceship2.physicsBody = SKPhysicsBody(circleOfRadius: spaceship2.size.width / 2);
    spaceship2.physicsBody?.isDynamic = true // apply gravity, friction, and collision
    spaceship2.physicsBody?.affectedByGravity = false;
    spaceship2.physicsBody?.allowsRotation = false
    spaceship2.physicsBody = SKPhysicsBody(circleOfRadius: spaceship2.size.width / 2);
    spaceship2.physicsBody?.categoryBitMask = enemyCategory
    spaceship2.physicsBody?.contactTestBitMask = pufferCategory

    addChild(spaceship1)
    addChild(spaceship2)
}

func didBegin(_ contact: SKPhysicsContact) {
    print("contact")
}

    override func update(_ currentTime: TimeInterval) {
        spaceship1.physicsBody?.affectedByGravity = false;
  }
}

提前致谢!

我想通了!

这是我的代码:)

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let enemy2Category: UInt32 = 1

let enemyCategory: UInt32 = 2

var spaceship1: SKSpriteNode!
var spaceship2: SKSpriteNode!

override func didMove(to view: SKView) {
    self.physicsWorld.contactDelegate = self
    spaceship1 = SKSpriteNode(imageNamed: "Spaceship");
    spaceship1.setScale(CGFloat(0.1))
    spaceship1.position = CGPoint(x: self.frame.width / 2, y: (self.frame.height / 2));
    spaceship1.name = "spaceship1";


    spaceship1.physicsBody = SKPhysicsBody(circleOfRadius: spaceship1.size.width / 2);
    spaceship1.physicsBody?.isDynamic = true // apply gravity, friction, and collision
    spaceship1.physicsBody?.affectedByGravity = false;
    spaceship1.physicsBody?.allowsRotation = false
    spaceship1.physicsBody = SKPhysicsBody(circleOfRadius: spaceship1.size.width / 2);
    spaceship1.physicsBody?.categoryBitMask = enemy2Category
    spaceship1.physicsBody?.collisionBitMask = enemyCategory
    spaceship1.physicsBody?.contactTestBitMask = enemyCategory

    spaceship2 = SKSpriteNode(imageNamed: "Spaceship");
    spaceship2.setScale(CGFloat(0.1))
    spaceship2.position = CGPoint(x: self.frame.width / 2, y: (spaceship1.position.y + 300));
    spaceship2.name = "spaceship2";

    spaceship2.physicsBody = SKPhysicsBody(circleOfRadius: spaceship2.size.width / 2);
    spaceship2.physicsBody?.isDynamic = true // apply gravity, friction, and collision
    spaceship2.physicsBody?.affectedByGravity = false;
    spaceship2.physicsBody?.allowsRotation = false
    spaceship2.physicsBody = SKPhysicsBody(circleOfRadius: spaceship2.size.width / 2);
    spaceship2.physicsBody?.categoryBitMask = enemyCategory
    spaceship2.physicsBody?.collisionBitMask = enemy2Category
    spaceship2.physicsBody?.contactTestBitMask = enemy2Category

    addChild(spaceship1)
    addChild(spaceship2)
}

 func didBegin(_ contact: SKPhysicsContact) {
    print("contact")
}

    override func update(_ currentTime: TimeInterval) {
        spaceship1.physicsBody?.affectedByGravity = false;
   }
}

我用这个视频来帮助:https://www.youtube.com/watch?v=43hzb4NmQfw