2
デリゲートSCNPhysicsContactDelegateを使用して衝突検出を使用してアプリケーションを構築します。 SCNPhysicsContactDelegateデリゲートとの衝突を検出しようとしましたが、動作しません。衝突検出SceneKit Swift
何が悪いですか?
この
が、これは敵func setupEnemy(){
space = space - 300
let football2 = SCNScene(named: "art.scnassets/k.scn")
let football21 = football2!.rootNode.childNodeWithName("football2", recursively: false)
football21!.name = "Enemy"
football21!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
football21!.physicsBody!.categoryBitMask = CollisionCategoryEnemy
football21!.physicsBody!.collisionBitMask = CollisionCategorySpaceMan
football21!.position = SCNVector3(x: 0, y: 0, z: Float(space))
sceneView.scene!.rootNode.addChildNode(football21!)
}
ある
let CollisionCategorySpaceMan = 1
let CollisionCategoryEnemy = 2
func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {
if contact.nodeB.physicsBody!.categoryBitMask == CollisionCategoryEnemy {
print("Enemy HIT!--------")
}
}
override func viewDidLoad() {
super.viewDidLoad()
let mainScene = createMainScene()
mainScene.physicsWorld.contactDelegate = self
mainScene.physicsWorld.gravity = SCNVector3Make(0, 0, 0)
sceneView = self.view as! GameView
sceneView.scene = mainScene
sceneView.delegate = self
}
私のコードであり、これはiOSの9以降でヒーロー
func createMainScene() -> SCNScene {
let scene = SCNScene(named: "art.scnassets/football.scn")
spaceManNode = scene!.rootNode.childNodeWithName("football", recursively: false)
spaceManNode!.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
spaceManNode!.physicsBody!.categoryBitMask = CollisionCategorySpaceMan
spaceManNode!.physicsBody!.contactTestBitMask = CollisionCategoryEnemy
spaceManNode.name = "SpaceMan"
setupLighting(scene!)
setupCameras(scene!)
return scene!
}
「didBeginContact」が少なくとも呼び出されたかどうかを確認しましたか?誰が 'nodeB'が' nodeA'ではなく 'CollisionCategoryEnemy'であることを保証しますか? – luk2302
私はどちらもうまくいきません: '( –
あなたの 'spaceManNode'に' collisionBitMask'を設定していないようです。 –