Spritekit物理身体与错误的身体碰撞

时间:2018-05-31 20:42:15

标签: ios swift sprite-kit

我正在学习Spritekit并制作飞扬的鸟类游戏。我试图在管道之间添加一个得分节点,但是鸟与节点保持冲突。我想出了它因为管道,但不知道如何解决这个问题。

这是pipesHolder的声明:

var PipesHolder = SKNode()

这是创建管道和分数节点的功能:

func createPipes() {
    pipeHolder = SKNode()
    pipeHolder.name = "Holder"

    // SCORE NODE
    let scoreNode = SKSpriteNode()
    scoreNode.name = "Score"
    scoreNode.color = UIColor.red
    scoreNode.size = CGSize(width: 5, height: 300)
    scoreNode.position = CGPoint(x: 15, y: 0)
    scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
    scoreNode.physicsBody?.categoryBitMask = ColliderType.Score
    scoreNode.physicsBody?.collisionBitMask = 0
    scoreNode.physicsBody?.affectedByGravity = false
    scoreNode.physicsBody?.isDynamic = false

    let pipeUp = SKSpriteNode(imageNamed: "pipeUp")
    pipeUp.name = "PipeUp"
    pipeUp.yScale = 1.5
    pipeUp.position = CGPoint(x: 0, y: self.frame.height / 2.1)
    pipeUp.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: pipeUp.size.width, height: pipeUp.size.height - pipeUp.size.height / 12))
    pipeUp.physicsBody?.categoryBitMask = ColliderType.Pipes
    pipeUp.physicsBody?.affectedByGravity = false
    pipeUp.physicsBody?.isDynamic = false

    let pipeDown = SKSpriteNode(imageNamed: "pipeDown")
    pipeDown.name = "pipeDown"
    pipeDown.yScale = 1.5
    pipeDown.position = CGPoint(x: 0, y: -(self.frame.height / 2.1))
    pipeDown.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: pipeDown.size.width, height: pipeDown.size.height - pipeDown.size.height / 12))
    pipeDown.physicsBody?.categoryBitMask = ColliderType.Pipes
    pipeDown.physicsBody?.affectedByGravity = false
    pipeDown.physicsBody?.isDynamic = false

    PipeHolder.zPosition = 2
    PipeHolder.position.x = self.frame.width + 20
    PipeHolder.position.y = CGFloat.randomBetween(numberOne: -(self.frame.size.height / 5.5), numberTwo: (self.frame.height / 5.5))
    PipeHolder.addChild(scoreNode)
    PipeHolder.addChild(PipesUp)
    PipeHolder.addChild(PipesDown)

下面是Collider设置和bird的配置:

struct ColliderType {
    static let Bird: UInt32 = 1
    static let Score: UInt32 = 2
    static let Pipes: UInt32 = 3
}

class Bird: SKSpriteNode {
    func initialize() {
        self.name = "Bird"
        self.zPosition = 5
        self.setScale(1.5)
        self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 3)
        self.physicsBody?.affectedByGravity = true
        self.physicsBody?.allowsRotation = false

        self.physicsBody?.categoryBitMask = ColliderType.Bird
        self.physicsBody?.collisionBitMask = ColliderType.Pipes
        self.physicsBody?.contactTestBitMask = ColliderType.Pipes | ColliderType.Score
    }

1 个答案:

答案 0 :(得分:0)

保持直线的好方法是定义类似的类别:

let oneCategory: UInt32 = 0x1 << 1
let anotherCategory: UInt32 = 0x1 << 2
let thirdCategory: UInt32 = 0x1 << 3
let fourthrCategory: UInt32 = 0x1 << 4
let fifthCategory: UInt32 = 0x1 << 5
let sixthCategory: UInt32 = 0x1 << 6
let nothingCategory: UInt32 = 0