如何让两个东西相互碰撞,然后从场景中删除一个?

时间:2022-07-06 12:03:10

I have four colored bars aligned horizontally. I also have some of the exact colors falling from the top of the screen which need to be matched with the bars at the bottom and then removed from the scene on contact. (If falling yellow bar hits bottom yellow bar, remove falling yellow bar from the scene) So, should I have eight different cases for each node in an enum instead of four? This is what it looks like now:

我有四个水平排列的彩色条。我还有一些从屏幕顶部落下的确切颜色需要与底部的条形图匹配,然后在接触时从场景中移除。 (如果下降的黄色条击中底部的黄色条,从场景中移除掉落的黄色条)那么,我应该为枚举中的每个节点而不是四个节点设置八个不同的情况吗?这就是现在的样子:

enum Bodies: UInt32 {
case blueBar = 1
case redBar = 2
case yellowBar = 4
case greenBar = 8

 }

Some of them are not doing what they're supposed to which is why I'm asking. Thanks in advance.

他们中的一些人没有做他们应该做的事情,这就是我要问的原因。提前致谢。

1 个解决方案

#1


1  

What you are trying to do is not considered an enum although I may be able to help. It sounds like you want two things to collide and the one of them to disappear. Try this.

虽然我可能会提供帮助,但您尝试做的事情并不算是枚举。听起来你想要碰撞两件事,其中一件就会消失。尝试这个。

let blueBar = SKSpriteNode(imageNamed: "name of image")
blueBar.name = "Bluebar"

let fallingBlueBar = SKSpriteNode(imageNamed: "name of image")
blueBar.name = "FallingBluebar"

func didBeginContact(contact: SKPhysicsContact) {
    if contact.bodyA.node != nil && contact.bodyB.node != nil {
        let firstBody = contact.bodyA.node as! SKSpriteNode
        let secondBody = contact.bodyB.node as! SKSpriteNode

    if ((firstBody.name == "BlueBar") && (secondBody.name == "FallingBlueBar")) {

         //call the function of what happens when they hit. ex-fall/disappear/explode

        }
    if ((firstBody.name == "FallingBlueBar") && (secondBody.name == "BlueBar")) {

         //call the function of what happens when they hit. ex-fall/disappear/explode


        }
    }
}

As far as removing it from the scene. In the same function that we used in each of the if statements above put

至于从场景中删除它。在我们在上面的每个if语句中使用的相同函数中

fallingBlueBar.removeFromParent()

#1


1  

What you are trying to do is not considered an enum although I may be able to help. It sounds like you want two things to collide and the one of them to disappear. Try this.

虽然我可能会提供帮助,但您尝试做的事情并不算是枚举。听起来你想要碰撞两件事,其中一件就会消失。尝试这个。

let blueBar = SKSpriteNode(imageNamed: "name of image")
blueBar.name = "Bluebar"

let fallingBlueBar = SKSpriteNode(imageNamed: "name of image")
blueBar.name = "FallingBluebar"

func didBeginContact(contact: SKPhysicsContact) {
    if contact.bodyA.node != nil && contact.bodyB.node != nil {
        let firstBody = contact.bodyA.node as! SKSpriteNode
        let secondBody = contact.bodyB.node as! SKSpriteNode

    if ((firstBody.name == "BlueBar") && (secondBody.name == "FallingBlueBar")) {

         //call the function of what happens when they hit. ex-fall/disappear/explode

        }
    if ((firstBody.name == "FallingBlueBar") && (secondBody.name == "BlueBar")) {

         //call the function of what happens when they hit. ex-fall/disappear/explode


        }
    }
}

As far as removing it from the scene. In the same function that we used in each of the if statements above put

至于从场景中删除它。在我们在上面的每个if语句中使用的相同函数中

fallingBlueBar.removeFromParent()