Swift SKPhysicsBody-物理实体节点设置为nil

时间:2018-08-21 18:11:43

标签: ios swift skphysicsbody

我正在制作《太空侵略者》游戏,遇到碰撞时遇到麻烦。在我的游戏中,当子弹击中敌舰时,它们应该消失并爆炸,但有时反而爆炸,却什么也没有发生。我研究了代码并尝试进行一些调试,结果发现子弹的物理实体有时为空。我找不到其他任何答案。

以下是与碰撞和SKPhysicsBody相关的代码的某些部分

let background = SKSpriteNode(imageNamed: "background")
let player = SKSpriteNode(imageNamed: "playerShip")

struct PhysicsCategory {
    static let None: UInt32 = 0 // 0
    static let Player: UInt32 = 0b1 // 1
    static let Bullet: UInt32 = 0b10 // 2
    static let Enemy: UInt32 = 0b100 // 4
}

// Did move to view
override func didMove(to view: SKView) {
    self.physicsWorld.contactDelegate = self

    // Player
    player.setScale(1)
    player.position = CGPoint(x: self.size.width/2, y: self.size.height/5)
    player.zPosition = 2
    player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
    player.physicsBody!.affectedByGravity = false
    player.physicsBody!.categoryBitMask = PhysicsCategory.Player // assigning the physics category
    player.physicsBody!.collisionBitMask = PhysicsCategory.None // no collision
    player.physicsBody!.contactTestBitMask = PhysicsCategory.Enemy // what the physics body can touch
    self.addChild(player)
}

// Contact
func didBegin(_ contact: SKPhysicsContact) {
    var body1 = SKPhysicsBody()
    var body2 = SKPhysicsBody()

    // Organizing physics bodies based on category number
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        body1 = contact.bodyA
        body2 = contact.bodyB
    } else {
        body1 = contact.bodyB
        body1 = contact.bodyA
    }

    if body1.categoryBitMask == PhysicsCategory.Player && body2.categoryBitMask == PhysicsCategory.Enemy {
        // Player hits enemy
        if body1.node != nil { // If there is a node (avoids crashing)
            spawnExplosion(body1.node!.position)
        }
        if body2.node != nil { // If there is a node (avoids crashing)
            spawnExplosion(body2.node!.position)
        }
        body1.node?.removeFromParent()
        body2.node?.removeFromParent()
    }
    if (body1.categoryBitMask == PhysicsCategory.Bullet && body2.categoryBitMask == PhysicsCategory.Enemy) {
        // Bullet hits enemy
        updateScore()

        if body2.node != nil {
            if body2.node!.position.y > self.size.height {
                return // will stop function
            } else {
                spawnExplosion(body2.node!.position)
            }
        }

        body1.node?.removeFromParent()
        body2.node?.removeFromParent()
    }
}

// Bullet
func fireBullet() {
    let bullet = SKSpriteNode(imageNamed: "bullet")
    bullet.setScale(1)
    bullet.position = player.position
    bullet.zPosition = 1
    bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
    bullet.physicsBody!.affectedByGravity = false
    bullet.physicsBody!.categoryBitMask = PhysicsCategory.Bullet // assigning the physics category
    bullet.physicsBody!.collisionBitMask = PhysicsCategory.None // no collisions
    bullet.physicsBody!.contactTestBitMask = PhysicsCategory.Enemy // what the physics body can touch
    self.addChild(bullet)

    let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
    let deleteBullet = SKAction.removeFromParent()
    let bulletSequence = SKAction.sequence([bulletSound,moveBullet, deleteBullet])
    bullet.run(bulletSequence)
}

// Enemy
func spawnEnemy() {
    let randomStartX = random(min: gameArea.minX, max: gameArea.maxX)
    let randomEndX = random(min: gameArea.minX, max: gameArea.maxX)
    let startPoint = CGPoint(x: randomStartX, y: self.size.height * 1.2)
    let endPoint = CGPoint(x: randomEndX, y: -self.size.height * 0.2)

    let enemy = SKSpriteNode(imageNamed: "enemyShip")
    enemy.setScale(1)
    enemy.position = startPoint
    enemy.zPosition = 2
    enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.size)
    enemy.physicsBody!.affectedByGravity = false
    enemy.physicsBody!.categoryBitMask = PhysicsCategory.Enemy // assigning the physics category
    enemy.physicsBody!.collisionBitMask = PhysicsCategory.None // no collisions
    enemy.physicsBody!.contactTestBitMask = PhysicsCategory.Player | PhysicsCategory.Bullet // what the physics body can touch
    self.addChild(enemy)

调试(在didBeginContact中)

print(body1) // DEBUGGING
print(body2) // DEBUGGING
print() // DEBUGGING

控制台

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> 
name:'(null)' texture:[<SKTexture> 'bullet' (25 x 100)] position: 
{795.82611083984375, 1766.0010986328125} scale:{1.00, 1.00} size:{25, 
100} anchor:{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode>             
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{808.9945068359375, 1900.0888671875} scale:{1.00, 1.00} size:{204, 88} 
anchor:{0.5, 0.5} rotation:-1.39]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> . 
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{892.953125, 1039.9288330078125} scale:{1.00, 1.00} size:{204, 88} 
anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> 
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{891.6375732421875, 896.56890869140625} scale:{1.00, 1.00} size:{204, 
88} anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> 
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{889.73736572265625, 689.49334716796875} scale:{1.00, 1.00} size:{204, 88} 
.anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> . 
name:'(null)' texture:[<SKTexture> 'playerShip' (88 x 204)] position: 
{850.55072021484375, 254.14501953125} scale:{1.00, 1.00} size:{88, 204} 
anchor:{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> . 
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{886.52154541015625, 339.05770874023438} scale:{1.00, 1.00} size:{204, 
88} anchor:{0.5, 0.5} rotation:-1.58]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> 
name:'(null)' texture:[<SKTexture> 'bullet' (25 x 100)] position: 
{1150.1441650390625, 777} scale:{1.00, 1.00} size:{25, 100} anchor: 
{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> . 
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position: 
{1205.85693359375, 800.99554443359375} scale:{1.00, 1.00} size:{204, 88} anchor:{0.5, 0.5} rotation:-1.47]

1 个答案:

答案 0 :(得分:0)

请尝试更改此部分

 body1 = contact.bodyB
 body1 = contact.bodyA

至:

 body1 = contact.bodyB
 body2 = contact.bodyA