我的方法didBeginContact有问题。我有一个球和一面墙。 两个对象的碰撞都很好。如果两个对象发生碰撞,它们会改变它们的位置,但不会调用方法didBeginContact。
// constants.swift
...
let ballCategory: UInt32 = 0x1 << 0
let wallCategory: UInt32 = 0x1 << 1
...
// Physics balls (ball.swift)
...
init() {
let size = CGSize(width: 32, height: 44)
loadPhysicsBodyWithSize(size: size)
}
func loadPhysicsBodyWithSize(size: CGSize){
physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.categoryBitMask = ballCategory
physicsBody?.contactTestBitMask = wallCategory
physicsBody?.affectedByGravity = false
}
...
// Physics wall (wall.swift)
...
init() {
let size = CGSize(width: 32, height: 44)
loadPhysicsBodyWithSize(size: size)
}
func loadPhysicsBodyWithSize(size: CGSize){
physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.categoryBitMask = wallCategory
physicsBody?.affectedByGravity = false
}
...
// GameScene (gameScene.swift)
class GameScene: SKScene, SKPhysicsContactDelegate {
...
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
}
func didBeginContact(contact: SKPhysicsContact){
print("didBeginContact called")
}
}
有人有什么想法吗?谢谢你的帮助!
答案 0 :(得分:3)
我通过改变
解决了这个问题func didBeginContact(联系方式:SKPhysicsContact)
到
func didBegin(_ contact:SKPhysicsContact)