【问题标题】:How do can I make things collide and do things when they do?我怎样才能让事情发生碰撞并在他们发生时做事情?
【发布时间】:2015-02-21 10:22:19
【问题描述】:

多年来我一直在尝试这样做,我想做的是,我的场景中有火箭和导弹落下,我想要一个非常简单的解释来说明如何做到这一点,以便在导弹相撞时做到这一点使用火箭,它将在您的代码中运行一个函数,只需调用函数“func”和 我对 SKPhysicsBody 和枚举不是很熟悉,所以请也解释一下(这是在 swift spriteKit 中)。 提前致谢。

【问题讨论】:

  • 请发布一些与您已经取得的成就相关的代码
  • 我还没有做任何事情,但我有一些教程知道有什么好的吗?

标签: swift enums sprite-kit skphysicsbody


【解决方案1】:

我会发表评论,但我没有足够的声誉 XD。我在 youtube 上观看了一个教程,该教程向您解释了如何通过碰撞移除。 https://www.youtube.com/watch?v=ylIIy5EbsWQ

 func didBeginContact(contact: SKPhysicsContact!){

    // Body1 and 2 depend on the categoryBitMask << 0 und << 1
    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
    }

    torpedoDidCollideWithAlien(contact.bodyA.node as SKSpriteNode, alien: contact.bodyB.node as SKSpriteNode)


}

func torpedoDidCollideWithAlien(torpedo:SKSpriteNode, alien:SKSpriteNode){
    println("HIT")
    torpedo.removeFromParent()
    alien.removeFromParent()
    aliensDestroyed++

    if (aliensDestroyed > 30){
        var transition:SKTransition = SKTransition.flipHorizontalWithDuration(0.5)
        var gameOverScene:SKScene = GameOverScene(size: self.size, won: true)
        self.view.presentScene(gameOverScene, transition: transition)
    }
}

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多