【问题标题】:Ship collision with coin not working properly与硬币发生碰撞无法正常工作
【发布时间】:2015-05-12 15:39:45
【问题描述】:

我创建了一个玩家必须收集硬币的游戏。现在由于某种原因,当屏幕上有很多硬币要抓取时,当船与硬币碰撞时,它会直接穿过当前与之碰撞的硬币并移除添加到屏幕上的最后一个硬币。我怎样才能使发生碰撞的实际硬币从屏幕上移除?

func didBeginContact(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
    }


    if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(obstacleCategory)) != 0 {
        ship.removeFromParent()
        let reveal = SKTransition.flipHorizontalWithDuration(0.5)
        let scene = GameOverScene(size: self.size)
        self.view?.presentScene(scene, transition: reveal)
    }

    if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
        coin.removeFromParent()
        playerScore = playerScore + 1
        playerScoreUpdate()
    }


override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if currentTime - self.lastMissileAdded > 1 {
        self.lastMissileAdded = currentTime + 1
        self.addMissile()
    }

    // Current time + 6 takes longer to respawn coins
    if currentTime - self.lastCoinAdded > 1 {
        self.lastCoinAdded = currentTime + 0
        self.addCoin()
    }

    // Current time + 6 takes longer to respawn coins
    //if currentTime - self.lastDiamondAdded > 1 {
      //  self.lastDiamondAdded = currentTime + 1
        //self.addDiamond()
    //}

    self.moveBackground()
    self.moveObstacle()
    self.moveCoin()
    //self.moveDiamond()

}

【问题讨论】:

    标签: xcode swift


    【解决方案1】:

    要删除与ship 发生冲突的coin,请更改didBeginContact 函数中第二个条件内的代码。您必须删除属于secondBody 的节点。在您的代码中,您只是删除了coin global variable。这就是为什么当场景中有其他硬币时它不起作用的原因。

    if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
        secondBody.node?.removeFromParent() // Changed line.
        playerScore = playerScore + 1
        playerScoreUpdate()
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      相关资源
      最近更新 更多