

現在Swift4のSpriteKitにて、シューティングゲームを製作しております。  
その中で必要な衝突判定（didBegin）が動かなく、困っております。

# 該当のソースコード

[code]

    //Swift4、Xcode9、iOS 11.2（最新版）を利用し、ビルドシステムはデフォルトのものを使用しております。
    import SpriteKit
    import GameplayKit
    import Foundation
    
    class GameScene: SKScene, SKPhysicsContactDelegate {
    
    let playerjudge = SKShapeNode(circleOfRadius: 2.5)
    let enemy1 = SKEmitterNode(fileNamed: "ghost.sks")
    
    let category_player:UInt32 = 1 << 1 // 000001
    let category_graze:UInt32 = 1 << 2  // 000010
    let category_playerbullet:UInt32 = 1 << 4 // 000100
    let category_enemy1:UInt32 = 1 << 10       // 001000
    let category_enemybullet1:UInt32 = 1 << 20 // 010000
    let category_other:UInt32 = 1 << 40        // 100000
    
    private var bullet : SKShapeNode?
    
    
    override func didMove(to view: SKView) {
        self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
        // 物理衝突の情報を自分で受け取る
        self.physicsWorld.contactDelegate = self
        // 物理空間の外枠の種類は、その他
        self.physicsBody?.categoryBitMask = category_other
        
        playerjudge.strokeColor = .red
        playerjudge.physicsBody = SKPhysicsBody(circleOfRadius: 2)
        playerjudge.physicsBody?.isDynamic = false
        addChild(playerjudge)
        playerjudge.physicsBody?.categoryBitMask = category_player
        self.bullet = SKShapeNode(circleOfRadius: 15)
        
        enemy1!.xScale = enemy1!.xScale / 2.5
        enemy1!.yScale = enemy1!.yScale / 2.5
        enemy1!.position.x = frame.midX
        enemy1!.position.y = frame.midY + 150
        enemy1!.physicsBody = SKPhysicsBody(circleOfRadius: 10)
        addChild(enemy1!)
        enemy1!.physicsBody?.isDynamic = false
        enemy1!.physicsBody?.categoryBitMask = category_enemy1
        if let bullet = self.bullet {
            bullet.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Double.pi), duration: 1)))
            bullet.run(SKAction.sequence([SKAction.wait(forDuration: 0.5),
                                          SKAction.fadeOut(withDuration: 0.5),
                                          SKAction.removeFromParent()]))
        }
    }
    
    func playershot() {
        if let n = self.bullet?.copy() as! SKShapeNode? {
            n.position = playerjudge.position
            n.physicsBody = SKPhysicsBody(circleOfRadius: 12.5)
            n.physicsBody?.isDynamic = false
            self.addChild(n)
            n.physicsBody?.categoryBitMask = category_playerbullet
            n.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Double.pi), duration: 0.1)))
            n.run(SKAction.sequence([SKAction.moveBy(x: n.position.x, y: n.position.y + 350, duration:0.75),
                                     SKAction.fadeOut(withDuration: 0.5),
                                     SKAction.removeFromParent()]))
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let location = touches.first!.location(in: self)
        let action = SKAction.move(to: CGPoint(x:location.x, y:location.y + 50), duration:0.1)
        playerjudge.run(action)
        playershot()
    }
    
    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
    
    //ここの衝突判定のプログラムが機能していないようで、ログに衝突と表示されない。
    func didBegin(_ contact: SKPhysicsContact) {
        print("衝突")
        if contact.bodyA.node?.physicsBody?.categoryBitMask == category_playerbullet && contact.bodyB.node?.physicsBody?.categoryBitMask == category_enemy1{
            contact.bodyA.node!.removeFromParent()
            contact.bodyB.node!.removeFromParent()
        }else if contact.bodyB.node?.physicsBody?.categoryBitMask == category_playerbullet && contact.bodyA.node?.physicsBody?.categoryBitMask == category_enemy1{
            contact.bodyA.node!.removeFromParent()
            contact.bodyB.node!.removeFromParent()
        }
    }
    }
    
[/code]

回答よろしくお願い致します。


CC BY-SA 3.0
2018-01-01T07:57:30.857
40655
2018-01-01T07:57:30.857
2020-06-17T08:14:45.997
-1
26850
question
swift ios spritekit swift4
SpriteKit、Swift4にて、衝突判定（didBegin）が動作しない