spritekit didBeginContact 三个对象不起作用 [英] spritekit didBeginContact three object not worked

查看:18
本文介绍了spritekit didBeginContact 三个对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过三个节点 [didBeginContact].我在代码下写的.但不能正常工作.当白色矩形没有击中黑色或白色矩形时,println("black") 和 println("blue") 工作...当白色矩形碰到黑色矩形时, println("blue") 工作...

I am tring to [didBeginContact] by three nodes. I wrote under code. But not work correctly. when white rectangle fole without hitting black or white rectangle, println("black"), and println("blue") work... whien white rectangle hit black rectangle, println("blue") work...

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let blackCategory:    UInt32 = 0x1 << 0
let whiteCategory:  UInt32 = 0x1 << 1
let blueCategory:   UInt32 = 0x1 << 2

override func didMoveToView(view: SKView) {

    self.physicsWorld.contactDelegate = self

    self.size = view.bounds.size
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsWorld.gravity = CGVectorMake(0.0, -3.0)

    let blackSquare = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50))
    blackSquare.position = CGPoint(
        x: CGRectGetMidX(self.frame),
        y: CGRectGetMidY(self.frame)
    )
    blackSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
    blackSquare.physicsBody?.affectedByGravity = false
    blackSquare.physicsBody?.dynamic = false

    blackSquare.physicsBody?.categoryBitMask = blackCategory
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory

    let blueSquare = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50))
    blueSquare.position = CGPoint(
        x: CGRectGetMidX(self.frame) - 100,
        y: CGRectGetMidY(self.frame) - 100
    )
    blueSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))
    blueSquare.physicsBody?.affectedByGravity = false
    blueSquare.physicsBody?.dynamic = false

    blackSquare.physicsBody?.categoryBitMask = blueCategory
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory

    self.addChild(blackSquare)
    self.addChild(blueSquare)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {


    for touch in touches {

        let location = touch.locationInNode(self)
        let whiteRectangle = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(50, 50))
        whiteRectangle.position = location
        whiteRectangle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50))


        whiteRectangle.physicsBody?.categoryBitMask = whiteCategory
        whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
        whiteRectangle.physicsBody?.contactTestBitMask = blueCategory

        self.addChild(whiteRectangle)
    }

}

override func update(currentTime: CFTimeInterval) {
}


func didBeginContact(contact: SKPhysicsContact!) {

    var firstBody, secondBody, thirdBody: SKPhysicsBody


    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
        thirdBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
        thirdBody = contact.bodyA
    }

    if firstBody.categoryBitMask & whiteCategory != 0 &&
        secondBody.categoryBitMask & blackCategory != 0 {
            //secondBody.node?.removeFromParent()
            println("black")
    }

    if firstBody.categoryBitMask & whiteCategory != 0 &&
        thirdBody.categoryBitMask & blueCategory != 0 {
            //secondBody.node?.removeFromParent()
            println("blue")
    }
}
}

推荐答案

1) 根据您的联系人处理程序,您应该将类​​别设置为

1) Based on your contact handler, you should set the categories to be

    let whiteCategory:  UInt32 = 0x1 << 0
    let blackCategory:  UInt32 = 0x1 << 1
    let blueCategory:   UInt32 = 0x1 << 2

2) 您两次错误地设置了 blackSquare 的 PhysicsBody 位掩码

2) You are incorrectly setting the blackSquare's physicsBody's bit masks twice

    blackSquare.physicsBody?.categoryBitMask = blueCategory
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory

以上应该是

    blueSquare.physicsBody?.categoryBitMask = blueCategory
    blueSquare.physicsBody?.contactTestBitMask = whiteCategory

3) 此处不需要第三个联系人正文.

3) The third contact body is not needed here.

func didBeginContact(contact: SKPhysicsContact!) {

    var firstBody, secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if ((firstBody.categoryBitMask & whiteCategory) != 0 &&
        (secondBody.categoryBitMask & blackCategory != 0)) {
            //secondBody.node?.removeFromParent()
            println("black")
    }


    if ((firstBody.categoryBitMask & whiteCategory != 0) &&
        (secondBody.categoryBitMask & blueCategory != 0)) {
            //secondBody.node?.removeFromParent()
            println("blue")
    }
}

4) 不需要以下内容(在touchesBegan 中).为蓝色和黑色节点设置接触位掩码就足够了.

4) The following is not needed (in touchesBegan). Setting the contact bit masks for the blue and black nodes is sufficient.

    whiteRectangle.physicsBody?.contactTestBitMask = blackCategory
    whiteRectangle.physicsBody?.contactTestBitMask = blueCategory

这篇关于spritekit didBeginContact 三个对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆