0

私はswiftが初めてで、ビットマスクとdidBegin(_ contact: SKPhysicsContact)を使用して2つの宇宙船が互いに接触したことを検出する方法を理解しようとしています。私は方法を理解できないようです。

これが私がこれまでに持っているものです:

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;
  }
}

前もって感謝します!

4

1 に答える 1