SpriteKit框架不正确

时间:2015-04-16 20:59:20

标签: swift sprite-kit skspritenode skphysicsbody

我正在制作一款简单的游戏。我正在尝试将帧设置为physicsBody,因此平面永远不会落在屏幕下方。下面的函数阻止它完全掉落,但SKSpriteNode在停止之前就会掉出视线。

func setBoundry(view: SKView) {
    // Create ground
    var boundry = SKNode()
    boundry.physicsBody = SKPhysicsBody(edgeLoopFromRect: view.frame)
    boundry.physicsBody?.dynamic = false
    boundry.physicsBody?.contactTestBitMask = PhysicsCategory.Plane
    boundry.physicsBody?.categoryBitMask = PhysicsCategory.Boundry | PhysicsCategory.Collidable

    self.addChild(boundry)
}

正在创建这样的平面......

func createPlane(sceneView: SKView) {
    let planeTexture = SKTexture(imageNamed: "planeRed1")
    let planeTexture1 = SKTexture(imageNamed: "planeRed2")
    let planeTexture2 = SKTexture(imageNamed: "planeRed3")

    // Animate plans propeller
    let animation = SKAction.animateWithTextures([planeTexture, planeTexture1, planeTexture2], timePerFrame: 0.05)
    let makePropellerSpin = SKAction.repeatActionForever(animation)

    // Set planes position
    plane = SKSpriteNode(texture: planeTexture)
    plane.position = CGPointMake(size.width/4, size.height/2)

    plane.runAction(makePropellerSpin)

    plane.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(plane.size.width, plane.size.height))
    plane.physicsBody?.dynamic = true
    plane.physicsBody?.allowsRotation = false
    plane.physicsBody?.categoryBitMask = PhysicsCategory.Plane
    plane.physicsBody?.collisionBitMask = PhysicsCategory.Collidable | PhysicsCategory.Boundry
    plane.physicsBody?.contactTestBitMask = PhysicsCategory.Collidable | PhysicsCategory.Boundry

    // Set elevation
    plane.zPosition = 5

    self.addChild(plane)
}

1 个答案:

答案 0 :(得分:0)

您需要为边界节点设置contactTestBitMask并为平面节点设置物理:

boundry.physicsBody?.contactTestBitMask = PhysicsCategory.YourPlayerEnum

另请查看物理学家的collisionBitMask属性。