区分不同的位掩码

时间:2017-08-16 19:06:52

标签: swift3 sprite-kit physics

我无法弄清楚如何区分不同的位掩码。

我希望这种情况发生:

 /*func didBegin(_ contact: SKPhysicsContact) {
if (spaceship1 collides with spaceship2) {
    print("contact between 1 and 2")
    }
if (spaceship1 collides with spaceship3) {
print("contact between 1 and 3")
    }
}
*/

这是我尝试过的代码:

    func didBegin(_ contact: SKPhysicsContact) {
    if (contact.bodyA.categoryBitMask = enemyCategory && contact.bodyB.categoryBitMask = enemy2Category) {
        print("contact between 1 and 2")
    }
}

我能得到一些帮助吗?

编辑:这是其他代码:

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let enemy2Category: UInt32 = 1

let enemyCategory: UInt32 = 2

let enemy3Category: UInt32 = 3

var spaceship1: SKSpriteNode!
var spaceship2: SKSpriteNode!
var spaceship3: 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 = true;
    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

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

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


    addChild(spaceship1)

}


        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    addChild(spaceship3)
}

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

}

我只想弄清楚如何检测哪些位掩码相互接触。就像宇宙飞船1与太空船2接触一样,它会打印出来,但是如果宇宙飞船1与宇宙飞船3接触,它会打印出来#34;哇哇哇哇哇哇

3 个答案:

答案 0 :(得分:0)

你在if语句中缺少一个=。

func didBegin(_ contact: SKPhysicsContact) {
    if (contact.bodyA.categoryBitMask == enemyCategory && contact.bodyB.categoryBitMask == enemy2Category) {
        print("contact between 1 and 2")
    }
}

答案 1 :(得分:0)

有一篇文档文章试图解释Sprite-Kit冲突和联系人中使用的位掩码,但我无法再链接到它。试着找到&#39;文档&#39;页面,标签&#39; Sprite-Kit&#39;然后操纵contactTest和collison位掩码来启用/禁用特定的接触和碰撞。&#39; &#39; SKNode Collision&#39;。

下的文章

这一行有一个明显的问题:

let enemy3Category: UInt32 = 3

所有位掩码都有被认为是二进制的,而二进制3中则是11.这是&#39; 1&#39;位和&#39; 2&#39;将两者都设置为1. 类别定义应该只有1位设置为1,您可以通过对类别使用2的精确幂来确保 - 1,2,4,8,16,32等

场景中需要碰撞/联系的对象最初应该只属于一个类别。对于更高级的联系人和冲突,精灵可以属于多个类别。

答案 2 :(得分:0)

这将firstBody设置为较小的位掩码并将secondBody设置为较大的位掩码

        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
        }

确认bitmask1小于2和3。

        if firstBody.categoryBitMask == bitMask1 && secondBody.categoryBitMask == bitMask.bitmask3 {
            print("a")

        //If ball hits a pad, activate the pad
        }else if firstBody.categoryBitMask == bitMask1 && secondBody.categoryBitMask == bitMask.bitmask2 {
            print("e")
}