Swift 3 SpriteKit检测联系人

时间:2017-08-14 15:33:04

标签: swift3 sprite-kit skphysicscontact

我是swift的新手,我一直试图找出如何使用bitmasks和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;
  }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我明白了!

这是我的代码:)

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