打开和关闭物理

时间:2017-04-21 14:00:31

标签: swift3 sprite-kit collision-detection

我试图解决一个问题,即一个精灵节点可以通过一个平台跳起但不能跳回来。我尝试使用此代码:

override func didMove(to view: SKView) {
    if (thePlayer.position.y > stonePlatform1.position.y) ==  true {
        stonePlatform1.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: stonePlatform.size.width * 0.9, height: stonePlatform.size.height * 0.75))
        stonePlatform1.physicsBody!.isDynamic = false
        stonePlatform1.physicsBody!.affectedByGravity = false
        stonePlatform1.physicsBody!.categoryBitMask = BodyType.object.rawValue
        stonePlatform1.physicsBody!.contactTestBitMask = BodyType.object.rawValue
        stonePlatform1.physicsBody!.restitution = 0.4
    }
}

当玩家在平台上方时,想法是打开平台的物理主体。但是,当我使用此代码时,物理学根本不起作用。实际上我尝试使用这段代码:

override func didMove(to view: SKView) {
    if (thePlayer.position.y < stonePlatform1.position.y) ==  true {
        stonePlatform1.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: stonePlatform.size.width * 0.9, height: stonePlatform.size.height * 0.75))
        stonePlatform1.physicsBody!.isDynamic = false
        stonePlatform1.physicsBody!.affectedByGravity = false
        stonePlatform1.physicsBody!.categoryBitMask = BodyType.object.rawValue
        stonePlatform1.physicsBody!.contactTestBitMask = BodyType.object.rawValue
        stonePlatform1.physicsBody!.restitution = 0.4
    }
}

并且物理学也没有开启。如果IF语句不存在,那么物理学一直都在工作。

2 个答案:

答案 0 :(得分:3)

您可以在此平台上使用节点速度,如下所示:

SpriteKit - Swift 3代码:

private var up1 : SKSpriteNode!
private var down1 : SKSpriteNode!
private var down2 : SKSpriteNode!
private var player : SKSpriteNode!

override func didMove(to view: SKView) {
    up1 = self.childNode(withName: "up1") as! SKSpriteNode
    down1 = self.childNode(withName: "down1") as! SKSpriteNode
    down2 = self.childNode(withName: "down2") as! SKSpriteNode
    player = self.childNode(withName: "player") as! SKSpriteNode
    up1.physicsBody?.categoryBitMask = 0b0001 // Mask for UoPlatforms
    down1.physicsBody?.categoryBitMask = 0b0010 // Mask for downPlatforms
    down2.physicsBody?.categoryBitMask = 0b0010 // Same mask
}

override func update(_ currentTime: TimeInterval) {
    player.physicsBody?.collisionBitMask = 0b0000 // Reset the mask

    // For UP only Platform
    if (player.physicsBody?.velocity.dy)! < CGFloat(0.0) {
        player.physicsBody?.collisionBitMask |= 0b0001 // The pipe | operator adds the mask by binary operations
    }

    // For Down only platforms
    if (player.physicsBody?.velocity.dy)! > CGFloat(0.0) {
        player.physicsBody?.collisionBitMask |= 0b0010  // The pipe | operator adds the mask by binary operations
    }

}

示例源代码:https://github.com/Maetschl/SpriteKitExamples/tree/master/PlatformTest

示例显示: 绿色平台 - &gt;只有下来 红色平台 - &gt;仅限

enter image description here

答案 1 :(得分:0)

您可以尝试从物理主体开始nil,然后在玩家高于它之后将物理值设置为它。此外,这种代码应该在更新功能中。在didMove中使用它只能让它被调用一次。

override func update(_ currentTime: TimeInterval){
 if (thePlayer.position.y < stonePlatform1.position.y) && stonePlatform1.physicsBody != nil {
       stonePlatform1.physicsBody = nil
 }else if (thePlayer.position.y > stonePlatform1.position.y) && stonePlatform1.physicsBody == nil{
    setPhysicsOnPlatform(stonePlatform1)
 }
}

func setPhysicsOnPlatform(_ platform: SKSpriteNode){
   platform.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: stonePlatform.size.width * 0.9, height: stonePlatform.size.height * 0.75))
   ...
   //the rest of your physics settings
}

您还应该对播放器和anchorPoint的高度进行一些处理。否则,如果您的anchorPoint为(0,0)并且玩家位于平台的中途,则将应用物理特性并且将发生不良结果。