我是 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;
}
}
提前致谢!