精灵之间的碰撞检测不起作用

时间:2015-04-25 07:00:17

标签: swift sprite collision

我有三个移动的精灵。一个是球,一个是线,一个是三角形。当球击中线时,我想得分++然后用addBall()函数产生另一个球。当球击中三角形时,我想结束比赛。

以下是我的代码。球直接穿过球和三角形而没有任何碰撞。有人能指出我正确的方向吗?

import SpriteKit




class GameScene: SKScene, SKPhysicsContactDelegate  {



var center = SKSpriteNode()
var center2 = SKSpriteNode()
var centerLine = SKSpriteNode()
var bg = SKSpriteNode()
var bigCircle = SKSpriteNode()
let counterClockwise = SKAction.rotateByAngle(CGFloat(3.14), duration:1)
let clockwise = SKAction.rotateByAngle(CGFloat(-3.14), duration:1)
var spin = SKAction()
var score = 0
var setCenter = SKAction.rotateByAngle(CGFloat(-1.75), duration:1)
var setCenter2 = SKAction.rotateByAngle(CGFloat(1.75), duration:1)


struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let ball       : UInt32 = 0x1         // 1
    static let triangle   : UInt32 = 0x1 << 1    // 2
    static let line      : UInt32 = 0x1 << 2    // 4
}









    override func didMoveToView(view: SKView) {

    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")



//Background
    var bgTexture = SKTexture(imageNamed: "images/bg.png")
    bg = SKSpriteNode(texture:bgTexture)
    bg.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    bg.zPosition = -4
    self.addChild(center)

//Center Circle
    var bigCircleTexture = SKTexture(imageNamed: "images/bigCircle.png")
    bigCircle = SKSpriteNode(texture:bigCircleTexture)
    bigCircle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    self.addChild(bigCircle)
    bigCircle.zPosition = -3
//Center Triangle
    var centerTexture = SKTexture(imageNamed: "center.png")
    center = SKSpriteNode(texture:centerTexture)
    center.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
    center.zPosition = -1
    center.physicsBody = SKPhysicsBody(rectangleOfSize: center.size)
    center.physicsBody?.categoryBitMask = PhysicsCategory.triangle
    self.addChild(center)
    center.runAction(setCenter)
    center.removeAllActions()
    spin = clockwise
    center.runAction(SKAction.repeatActionForever(spin))
//Center Triangle 2
        var centerTexture2 = SKTexture(imageNamed: "center.png")
        center2 = SKSpriteNode(texture:centerTexture)
        center2.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        center2.zPosition = -1
        center2.physicsBody = SKPhysicsBody(rectangleOfSize: center2.size)
        center2.physicsBody?.categoryBitMask = PhysicsCategory.triangle
        self.addChild(center)
        center2.runAction(setCenter2)
        center2.removeAllActions()
        spin = clockwise
        center.runAction(SKAction.repeatActionForever(spin))

//Center Line
        var centerLineTexture = SKTexture(imageNamed: "centerLine.png")
        centerLine = SKSpriteNode(texture:centerLineTexture)
        centerLine.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) + center.size.height)
        centerLine.zPosition = -2
        centerLine.physicsBody = SKPhysicsBody(rectangleOfSize: centerLine.size)
        centerLine.physicsBody?.categoryBitMask = PhysicsCategory.line
        self.addChild(centerLine)
        spin = clockwise
        centerLine.runAction(SKAction.repeatActionForever(spin))

//Create Balls

    //Ball Settings
    func randomCGFloat() -> CGFloat {
            return CGFloat(Double(arc4random())/Double(UInt32.max) )
        }

        var ball = SKSpriteNode(imageNamed: "newBall.png")

        var randomBall = arc4random_uniform(4) + 1
        var moveBall = SKAction.moveTo(CGPointMake(self.size.width/2,self.size.height/2), duration:3.0)
        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height / 2)
        ball.physicsBody?.dynamic = false
        ball.physicsBody?.allowsRotation = false
        ball.physicsBody?.categoryBitMask = PhysicsCategory.ball
        ball.physicsBody?.collisionBitMask = PhysicsCategory.triangle
        ball.physicsBody?.contactTestBitMask = PhysicsCategory.triangle
        ball.physicsBody?.collisionBitMask = PhysicsCategory.line

        //Initial Ball
        if score == 0 {
            ball.position = CGPointMake(self.size.width/2, self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }



        //Spawning and Moving Balls


        func addBall() {


        if randomBall == 1 {
            ball.position = CGPointMake(self.size.width/2, self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        else if randomBall == 2 {
            ball.position = CGPointMake(self.size.width,     self.size.height/2)
            self.addChild(ball)
            ball.runAction(moveBall)
         }

        else if randomBall == 3{
            ball.position = CGPointMake(self.size.width/2, -self.size.height)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        else if randomBall == 4 {
            ball.position = CGPointMake(self.size.width - self.size.width, self.size.height/2)
            self.addChild(ball)
            ball.runAction(moveBall)
        }

        }



        func didBeginContact(contact: SKPhysicsContact) {
            if contact.bodyA.categoryBitMask == PhysicsCategory.line     {
                score++
                println("\(score)")

            } else if contact.bodyA.categoryBitMask == PhysicsCategory.triangle {
                println("lost")
            }
        }





}







override func touchesBegan(touches: NSSet, withEvent event: UIEvent)     {
    /* Called when a touch begins */
    if spin == clockwise  {
        center.removeAllActions()
        centerLine.removeAllActions()
        spin = counterClockwise



    }

    else {
        center.removeAllActions()
        centerLine.removeAllActions()
        spin  = clockwise

    }
    center.runAction(SKAction.repeatActionForever(spin))
    centerLine.runAction(SKAction.repeatActionForever(spin))
}


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

1 个答案:

答案 0 :(得分:0)

首先,您需要为要与物理交互的每个对象(重力,碰撞......)设置SKPhysicsBody。目前您的线和三角形不是这种情况。

之后,(类别位掩码)未正确设置。的确,0 << 3是不对的。您可能希望声明类别位掩码,如下所示:

struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let Ball       : UInt32 = 0b1       // 1
    static let Triangle   : UInt32 = 0b10      // 2
    static let Line       : UInt32 = 0b100     // 4
}

或者像这样:

struct PhysicsCategory {
    static let None       : UInt32 = 0
    static let All        : UInt32 = UInt32.max
    static let Ball       : UInt32 = 0x1         // 1
    static let Triangle   : UInt32 = 0x1 << 1    // 2
    static let Line       : UInt32 = 0x1 << 2    // 4
}

这是我为another question制作的表格,对于类别位掩码也可能有所帮助: http://goo.gl/7D8EGY