碰撞玩家 - 敌人(射击游戏 - 迅捷4 -SpriteKit)

时间:2018-03-09 20:00:50

标签: swift sprite-kit sprite game-physics

我正在使用SKSpriteNodes编写一个使用Swift 4编码的射击游戏。

我试图让我的游戏在敌人时停止,并展示GameOverScene,但它不起作用,我不明白为什么。

我已经使用相同类型的功能来实现射弹和敌人的碰撞,并且效果很好。

这是我的代码,来自GameScene:

struct PhysicsCategory {
    static let None      : UInt32 = 0
    static let All       : UInt32 = UInt32.max
    static let Monster   : UInt32 = 0b1       // 1
    static let Projectile: UInt32 = 0b10      // 2
    static let Player    : UInt32 = 0b01
}

override func didMove(to view: SKView) {
    player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
    player.zPosition = 2

    player.physicsBody = SKPhysicsBody(rectangleOf: player.size) // 1
    player.physicsBody?.isDynamic = true // 2
    player.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
    player.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile // 4
    player.physicsBody?.collisionBitMask = PhysicsCategory.None // 5

    addChild(player) 
}

    func addMonster() {

    // Create sprite
    let monster = SKSpriteNode(imageNamed: "monster")
    monster.zPosition = 2

    monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size) // 1
    monster.physicsBody?.isDynamic = true // 2
    monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
    monster.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile // 4
    monster.physicsBody?.collisionBitMask = PhysicsCategory.None // 5

    // Determine where to spawn the monster along the Y axis
    let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)

    // Position the monster slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
    monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)

    // Add the monster to the scene
    addChild(monster)

    // vitesse augmentée
    let actualDuration = random(min: CGFloat(1), max: CGFloat(4))

     // Create the actions
    let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY), duration: TimeInterval(actualDuration))
    let actionMoveDone = SKAction.removeFromParent()

    monster.run(SKAction.sequence([ actionMove, actionMoveDone]))
}

    func ennemyCollidesWithPlayer(monster: SKSpriteNode, player: SKSpriteNode){
    print("Game Over")

    let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
    let gameOverScene = GameOverScene(size: self.size, won: false)
    self.view?.presentScene(gameOverScene, transition: reveal)
}

func didBegin(_ contact: SKPhysicsContact) {
    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    //collision player et enemy
    if ((firstBody.categoryBitMask & PhysicsCategory.Monster != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.Player != 0)) {
        if let player = firstBody.node as? SKSpriteNode, let
            monster = secondBody.node as? SKSpriteNode {
            ennemyCollidesWithPlayer(monster: monster, player: player)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

categoryBitMask是当前物体上物理体的分类,它也是检查物理体类型时使用的标识符

contactBitMask正在设置在联系时将在didBegin中报告的对象

你的玩家和怪物都有相同的categoryBitMask set

将玩家更改为PhysicsCategory.Player

您还需要将播放器contactBitMask设置为

contactTestBitMask = PhysicsCategory.Monster