Collsion Detection Sprite-Kit Bitmasking

时间:2015-09-18 16:32:45

标签: swift sprite-kit collision-detection

我花了几周时间尝试使用BitMasking完全掌握Sprite-Kit中的碰撞检测。香港专业教育学院搜索谷歌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")
    }
}
}

1 个答案:

答案 0 :(得分:0)

我已经修改了一些代码,现在它运行正常。这是你的代码:

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的精彩教程,您可以在其中了解更多信息。