SpriteKit碰撞未被检测到

时间:2015-06-23 13:59:22

标签: ios swift sprite-kit

好的,我已经和这个代码打了几天了。我无法弄清楚为什么没有检测到碰撞。

我正试图沿着一个逆时针转动的圆圈画一个字符串,我想要一个对撞机圆圈顺时针旋转弦。

这是我的代码:

class GameScene : SKScene, SKPhysicsContactDelegate {
var circleString:String = "some string to draw in a circle"

private struct ColliderType {
    static let Symbol: UInt32 = 0x1 << 0
    static let OrbitingBall: UInt32 = 0x1 << 1
}

override func didMoveToView(view: SKView) {
    self.physicsWorld.contactDelegate = self
    createSpinningText()
}

func createSpinningText() {
    //circleNode serves as a container node to simplify placing and rotating the symbolNodes 
    let circleNode = SKShapeNode(circleOfRadius: self.size.width * 0.40)
    circleNode.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
    circleNode.strokeColor = UIColor.purpleColor()

    self.addChild(circleNode)

    //count elements and calculate rotation per character
    let l = countElements(symbolString)
    let charRotation = -(2.0 * M_PI) / Double(l)

    for x in 0..<l {
        let character = circleString.substringWithRange(Range<String.Index>(start: advance(circleString.startIndex, x), end: advance(circleString.startIndex, x + 1)))

        //charSprite is a container for charNode, to rotate the starting
        //position of each character. (because I can't set a frame or anchorPoint
        //on a SKLabelNode)
        let charSprite = SKSpriteNode(color: UIColor.clearColor(), size: circleNode.frame.size)

        let charNode = SKLabelNode(fontNamed: "Arial")
        charNode.text = character
        charNode.fontSize = 25
        charNode.position = CGPoint(x: 0, y: circleNode.frame.size.width / 2.27)
        charNode.fontColor = UIColor.purpleColor()

        charNode.physicsBody = SKPhysicsBody(rectangleOfSize: charNode.frame.size)
        charNode.physicsBody?.dynamic = false
        charNode.physicsBody?.categoryBitMask = ColliderType.Symbol
        charNode.physicsBody?.contactTestBitMask = ColliderType.OrbitingBall | ColliderType.Symbol
        charNode.physicsBody?.usesPreciseCollisionDetection = true
        charNode.physicsBody?.collisionBitMask = ColliderType.OrbitingBall | ColliderType.Symbol

        let rotate = SKAction.rotateByAngle(CGFloat(Double(x) * charRotation), duration: 0)
        charSprite.runAction(rotate)

        charSprite.addChild(charNode)
        circleNode.addChild(charSprite)
    }

    let turnSlowlyCCW = SKAction.rotateByAngle(1, duration: 20)
    let repeat = SKAction.repeatActionForever(turnSlowlyCCW)

    circleNode.runAction(repeat)

    let orbitingColliderContainer = SKShapeNode(circleOfRadius: self.size.width * 0.35)
    orbitingColliderContainer.strokeColor = UIColor.redColor()

    orbitingColliderContainer.position = CGPointZero
    let xy = CGPoint(x: orbitingColliderContainer.frame.size.width * 0.5, y: orbitingColliderContainer.frame.size.height * 0.25)

    orbitingColliderContainer.physicsBody = SKPhysicsBody(circleOfRadius: 70, center:xy)
    orbitingColliderContainer.physicsBody?.dynamic = false
    orbitingColliderContainer.physicsBody?.contactTestBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
    orbitingColliderContainer.physicsBody?.collisionBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
    orbitingColliderContainer.physicsBody?.categoryBitMask = ColliderType.OrbitingBall
    orbitingColliderContainer.physicsBody?.usesPreciseCollisionDetection = true
    circleNode.addChild(orbitingColliderContainer)

    let turnSlowlyCW = SKAction.rotateByAngle(-2, duration: 20)
    let repeat2 = SKAction.repeatActionForever(turnSlowlyCW)
    orbitingColliderContainer.runAction(repeat2)
}

func didBeginContact(contact: SKPhysicsContact) {
    println("Contact between \(contact.bodyA) and \(contact.bodyB) detected")
}
}

这是应用程序在运行时的样子(使用.showPhysics):

enter image description here

一切都按照预期放置和移动,但我没有得到任何didBeginContact调用。那么,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

通过设置

让它工作
self.physicsWorld.gravity = CGVectorMake(0, 0)

和...

orbitingColliderContainer.physicsBody?.dynamic = true

然后为两个charNodes和orbitingColliderContainer设置collisionBitMask为0。