某些physicsBody碰撞没有注册

时间:2014-12-14 09:17:33

标签: swift sprite-kit collision skphysicsbody bounce

到目前为止,我正在练习我在swift中学到的东西,并且我试图制作一个简单的游戏,其中一个球在你可以移动的平台上弹跳。我在平台下面有一层,最终会让游戏结束。此刻,球可以从地板上反弹,并且我已经在场景周围放置了障碍物,但只是穿过可以在屏幕上移动的平台。除了类别名称,地板和平台的代码基本相同,我无法说明为什么这不起作用。这是我的代码:

class GameScene: SKScene, SKPhysicsContactDelegate {


    let ballGroup: UInt32 = 0b001
    let platformGroup: UInt32 = 0b010
    let floorGroup: UInt32 = 0b100
    let sceneEdgeGroup : UInt32 = 0b101

    var platform = SKShapeNode()
    var floor = SKShapeNode()
    var ball = SKShapeNode()

    var i = 0

    override func didMoveToView(view: SKView) {

        self.size = view.bounds.size
        self.physicsWorld.contactDelegate = self
        self.physicsWorld.gravity = CGVectorMake(0, -5)

        let physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = physicsBody
        self.physicsBody?.categoryBitMask = sceneEdgeGroup
        self.physicsBody?.friction = 0
        self.physicsBody?.restitution = 1


        platform = SKShapeNode(rectOfSize: CGSizeMake(100, 15))
        platform.fillColor = SKColor.redColor()

        platform.position = CGPointMake(CGRectGetMidX(self.frame)  -  (CGRectGetMidX(self.frame)), CGRectGetMidY(self.frame) * 0.4)
        platform.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(100, 15))
        platform.physicsBody?.dynamic = false
        platform.physicsBody?.allowsRotation = false

        platform.physicsBody?.categoryBitMask = platformGroup
        platform.physicsBody?.friction = 0


        self.addChild(platform)


        floor = SKShapeNode(rectOfSize: CGSizeMake(self.frame.size.width, 200))
        floor.fillColor = SKColor.blueColor()
        floor.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - CGRectGetMidY(self.frame))
        floor.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 200))
        floor.physicsBody?.dynamic = false

        floor.physicsBody?.categoryBitMask = floorGroup
        floor.physicsBody?.friction = 0

        self.addChild(floor)


        ball = SKShapeNode(circleOfRadius: 5)
        ball.fillColor = SKColor.blackColor()
        ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) * 1.5 )
        ball.physicsBody = SKPhysicsBody(circleOfRadius: 5)
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.categoryBitMask = ballGroup
        ball.physicsBody?.collisionBitMask = platformGroup
        ball.physicsBody?.collisionBitMask = floorGroup
        ball.physicsBody?.collisionBitMask = sceneEdgeGroup

        ball.physicsBody?.contactTestBitMask = floorGroup
        ball.physicsBody?.contactTestBitMask = platformGroup
        ball.physicsBody?.contactTestBitMask = sceneEdgeGroup

        ball.physicsBody?.friction = 0
        ball.physicsBody?.restitution = 0
        ball.physicsBody?.mass = 0.5
        ball.physicsBody?.linearDamping = 0
        ball.physicsBody?.allowsRotation = false

        self.addChild(ball)

        //start ball movement
        ball.physicsBody?.applyImpulse(CGVectorMake(-5, 0))
    }

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

        let moveAction1 = SKAction.moveByX((self.frame.width) , y: 0, duration: 1)
        let moveAction2 = SKAction.moveByX((-self.frame.width) , y: 0, duration: 1)


        if(i == 0){
            platform.runAction(moveAction1)
            i++
        }else if (i == 1){
            platform.runAction(moveAction2)
            i = 0
        }
    }


    func didBeginContact(contact: SKPhysicsContact) {
        if(contact.bodyB.categoryBitMask == ballGroup && contact.bodyA.categoryBitMask == floorGroup || (contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == floorGroup)){

            //this contact works
           // ball.physicsBody?.restitution = 0.5
            //ball.physicsBody?.applyImpulse(CGVectorMake(0, 350))

        }

        if(contact.bodyB.categoryBitMask == ballGroup && contact.bodyA.categoryBitMask == platformGroup || (contact.bodyA.categoryBitMask == ballGroup && contact.bodyB.categoryBitMask == platformGroup)){

            //this contact doesn't work
            //should make ball bounce
            ball.physicsBody?.applyImpulse(CGVectorMake(0, 300))
        }

    }

}

1 个答案:

答案 0 :(得分:0)

目前您覆盖了Bitmasks:

    ball.physicsBody?.contactTestBitMask = floorGroup
    ball.physicsBody?.contactTestBitMask = platformGroup
    ball.physicsBody?.contactTestBitMask = sceneEdgeGroup

你必须把它们结合起来:

    ball.physicsBody?.contactTestBitMask = floorGroup | platformGroup | sceneEdgeGroup

同样在这里:

    ball.physicsBody?.collisionBitMask = platformGroup
    ball.physicsBody?.collisionBitMask = floorGroup
    ball.physicsBody?.collisionBitMask = sceneEdgeGroup