接触位屏蔽

时间:2018-12-13 16:35:12

标签: swift xcode sprite-kit

所以我在我的应用程序中使用了Physical Body和contactbitmasks / physicscategories,但是它们不能正常工作。我希望球在遇到危险时重置,或者一旦碰到球门就继续前进到下一个级别。目前,我已将其设置为仅出于测试原因而打印语句。

我在代码中都设置了它们,但仍然无法正常工作。有什么问题吗?

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var ball = SKSpriteNode()
var danger1 = SKSpriteNode()
var danger2 = SKSpriteNode()
var goal = SKSpriteNode()


override func didMove(to view: SKView) {

    ball = self.childNode(withName: "ball") as! SKSpriteNode
    danger1 = self.childNode(withName: "danger1") as! SKSpriteNode
    danger2 = self.childNode(withName: "danger2") as! SKSpriteNode
    goal = self.childNode(withName: "goal") as! SKSpriteNode

    let border = SKPhysicsBody(edgeLoopFrom: self.frame)
    border.friction = 0
    border.restitution = 0

    danger1.physicsBody = SKPhysicsBody()
    danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
    danger2.physicsBody = SKPhysicsBody()
    danger2.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory

    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
    ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
    ball.physicsBody?.contactTestBitMask = PhysicsCategories.dangerCategory
    ball.physicsBody?.collisionBitMask = PhysicsCategories.none

    goal.physicsBody = SKPhysicsBody()
    goal.physicsBody?.categoryBitMask = PhysicsCategories.goalCategory

    danger1.physicsBody?.isDynamic = true
    ball.physicsBody?.isDynamic = true
    goal.physicsBody?.isDynamic = true
    danger2.physicsBody?.isDynamic = true
    danger2.physicsBody!.affectedByGravity = false
    danger1.physicsBody!.affectedByGravity = false
    goal.physicsBody!.affectedByGravity = false
    ball.physicsBody!.affectedByGravity = false
    setupPhysics()

}
func setupPhysics() {
    physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)
    physicsWorld.contactDelegate = self
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        ball.position.x = location.x
        ball.position.y = location.y
        print("x: \(ball.position.x), y: \(ball.position.y)")
    }
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}
}

extension GameScene: SKPhysicsContactDelegate {



func didBegin(_ contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.dangerCategory {
        print("Contact")
    } else if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.goalCategory {
        print("Goal!")
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的危险区域的物理体没有大小,那么什么会碰撞?

danger1.physicsBody = SKPhysicsBody() should be danger1.physicsBody = SKPhysicsBody(rectangleOf:danger1.size)

现在要获取一些高级注释,除非您的精灵实际上正在移动,否则请关闭isDynamic。在现实世界中,除非墙壁在球顶上塌陷,否则您的墙壁将永远不会与球碰撞。

清理应如下所示:

physicsWorld.contactDelegate = self
ball = self.childNode(withName: "ball") as! SKSpriteNode
danger1 = self.childNode(withName: "danger1") as! SKSpriteNode
danger2 = self.childNode(withName: "danger2") as! SKSpriteNode
goal = self.childNode(withName: "goal") as! SKSpriteNode

let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 0

danger1.physicsBody = SKPhysicsBody(rectangleOf:danger1.size)
danger1.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger1.physicsBody?.isDynamic = false

danger2.physicsBody = SKPhysicsBody(rectangleOf:danger2.size)
danger2.physicsBody?.categoryBitMask = PhysicsCategories.dangerCategory
danger2.physicsBody?.isDynamic = false

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
ball.physicsBody?.contactTestBitMask = PhysicsCategories.dangerCategory | PhysicsCategories.goalCategory
ball.physicsBody?.collisionBitMask = PhysicsCategories.none
ball.physicsBody?.isDynamic = true
ball.physicsBody!.affectedByGravity = false

goal.physicsBody = SKPhysicsBody(rectangleOf:goal.size)
goal.physicsBody?.categoryBitMask = PhysicsCategories.goalCategory
goal.physicsBody?.isDynamic = false
//setupPhysics()  Remove this