我花了数周的时间尝试使用BitMasking完全掌握Sprite-Kit中的碰撞检测。 Ive搜索google youtube,找不到任何东西。病发我现在的代码。当我的两个SKSpriteNodes相互联系时,我现在拥有的代码目前无法正常工作。有人可以告诉我如何修复我的代码,并告诉我这些步骤及其含义。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1
let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
    let box = SKSpriteNode(imageNamed: "box")
    box.position = CGPoint(x: 400, y: 400)
    box.physicsBody?.categoryBitMask = boxCategory
    box.physicsBody?.contactTestBitMask = boxCategory
    box.physicsBody?.collisionBitMask = boxCategory | ballCategory
    addChild(box)

    redBall.position = CGPoint(x: 200, y: 200)
    redBall.physicsBody?.categoryBitMask = ballCategory
    redBall.physicsBody?.contactTestBitMask = ballCategory
    redBall.physicsBody?.collisionBitMask = ballCategory | boxCategory
    addChild(redBall)

    //Backround Properties
    backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

}




override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    redBall.position = touchLocation

}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    redBall.position = touchLocation
}
func didBeginContact(contact: SKPhysicsContact) {
    let contactBodyA = SKSpriteNode()
    let contactBodyB = SKSpriteNode()

    if(contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == boxCategory){

        println("contact was made")
    }
}
}

最佳答案

我已经修改了一些代码,现在可以正常工作了。这是您的代码:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

    let ballCategory:UInt32 = 0x1 << 0
    let boxCategory:UInt32 = 0x1 << 1

    let ball = SKSpriteNode(imageNamed: "redball")
    let redBall = SKSpriteNode(imageNamed: "redball")


    override func didMoveToView(view: SKView) {
        self.physicsWorld.contactDelegate = self
        //make gravity 0
        self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)

        let box = SKSpriteNode(imageNamed: "redball")
        box.position = CGPoint(x: 400, y: 400)
        //add physicsbody for collisin detection
        box.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: box.size)
        //assign a correct cetogary
        box.physicsBody?.categoryBitMask = boxCategory
        box.physicsBody?.collisionBitMask =  ballCategory
        box.physicsBody?.contactTestBitMask =  ballCategory
        addChild(box)

        redBall.position = CGPoint(x: 200, y: 200)
        //add physics body
        redBall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: redBall.size)
        //assign correct cetogary
        redBall.physicsBody?.categoryBitMask = ballCategory
        redBall.physicsBody?.contactTestBitMask = boxCategory
        redBall.physicsBody?.collisionBitMask = boxCategory
        addChild(redBall)

        //Backround Properties
        backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

    }




    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first!
        let touchLocation = touch.locationInNode(self)
        redBall.position = touchLocation
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first!
        let touchLocation = touch.locationInNode(self)
        redBall.position = touchLocation
    }

    func didBeginContact(contact: SKPhysicsContact) {
        // 1. Create local variables for two physics bodies
        var firstBody: SKPhysicsBody
        var secondBody: SKPhysicsBody

        // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        // 3. react to the contact between ball and bottom
        if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == boxCategory {
            //TODO: Replace the log statement with display of Game Over Scene
            //add your code here.
            print("Called")
        }
    }
}


已通过xCode 7测试。

HERE是有关spriteKit的出色教程,您可以了解更多信息。

关于swift - 碰撞检测Spritekit位屏蔽,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32656926/

10-09 01:40