Unwrapping optional values: PhysicsBody - Swift SpriteKit

大城市里の小女人 提交于 2019-12-06 06:38:32

I don't know your CollideType class or declaration, so my code below explain to you how to build a correct physicsWorld and physicsBody to don't have issues during the collisions.

But first: we're going to be using the categoryBitMask, contactTestBitMask and collisionBitMask properties in their fullest for this project, because we have very precise rules that make the project work:

  • categoryBitMask is a number defining the type of object this is for considering collisions.

  • collisionBitMask is a number defining what categories of object this node should collide with.

  • contactTestBitMask is a number defining which collisions we want to be notified about.

SpriteKit expects these three bitmasks to be described using a UInt32.Your bitmasks should start at 1 then double each time.

The code:

class GameScene: SKScene, SKPhysicsContactDelegate {
    var ball: Ball!
    let ballSpeedX = CGFloat(500)
    enum CollisionTypes: UInt32 {
        case Field = 1
        case Ball = 2
    }
    override func didMoveToView(view: SKView) {
        // ball
        ball = Ball(imageNamed:"colorBall.png")
        ball.position = CGPointMake(self.ball.frame.size.width,self.ball.frame.size.height)

        // Prepare the world - physics engine.
        self.physicsWorld.gravity = CGVectorMake(0, -6)
        self.physicsWorld.contactDelegate = self
        let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
        self.physicsBody = fieldBody
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.usesPreciseCollisionDetection = true
        self.physicsBody!.dynamic = true
        self.physicsBody!.mass = 0
        self.physicsBody!.friction = 0
        self.physicsBody!.linearDamping = 0
        self.physicsBody!.angularDamping = 0
        self.physicsBody!.restitution = 1
        self.physicsBody!.categoryBitMask = CollisionTypes.Field.rawValue
        self.physicsBody!.contactTestBitMask = CollisionTypes.Ball.rawValue

        // Prepare the ball - physics engine.
        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2)
        ball.physicsBody!.affectedByGravity = true
        ball.physicsBody!.restitution = 0.8
        ball.physicsBody!.linearDamping = 0
        ball.physicsBody!.friction = 0.3
        ball.physicsBody!.dynamic = true
        ball.physicsBody!.mass = 0.5
        ball.physicsBody!.allowsRotation = true
        ball.physicsBody!.categoryBitMask = CollisionTypes.Ball.rawValue
        ball.physicsBody!.contactTestBitMask = CollisionTypes.Field.rawValue
        ball.physicsBody!.collisionBitMask = CollisionTypes.Field.rawValue

        //ball.physicsBody!.categoryBitMask = CollideType.Ball.toMask()
        //ball.physicsBody!.collisionBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
        //ball.physicsBody!.contactTestBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
        ball.hidden = false
        self.addChild(ball)
    }
    func didBeginContact(contact: SKPhysicsContact) {
        if (contact.bodyA.categoryBitMask == CollisionTypes.Field.rawValue &&
            contact.bodyB.categoryBitMask == CollisionTypes.Ball.rawValue) {
            print("contact between field and ball")
        }
    }
}

As you can see, the first important thing is to prepare the physicsWorld (track the boundaries within wich your physics must work), assign it a category and all the other parameters need to interact with the other objects. Then you build the physicsBody of your object , in this case the ball.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!