斯威夫特:由于两次检测到碰撞,得分增加了两倍? [英] Swift: score increases twice because collision is detected twice?

查看:88
本文介绍了斯威夫特:由于两次检测到碰撞,得分增加了两倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迅速制作一个Sprite Kit游戏,当检测到2个节点之间的碰撞时,我需要将分数提高1。分数存储在名为 animalsCount 的变量中,并输出到标签节点:

I'm building a sprite kit game in swift and I need the score to increase by 1 when collision between 2 nodes is detected. The score is stored in a variable named animalsCount and is outputted to a label node:

    //Score count in stats bar
    //Animal score count

    animalsCount = 0

    animalsCountLabel.text = "\(animalsCount)"
    animalsCountLabel.fontSize = 45
    animalsCountLabel.fontColor = SKColor.blackColor()
    animalsCountLabel.position = CGPoint (x: 630, y: 40)

    addChild(animalsCountLabel)

两个碰撞的精灵节点是救星 chicken1 。现在,我正在使用以下代码保持得分并检测碰撞:

The two sprite nodes that are colliding are savior and chicken1. Right now, I am keeping score and detecting collision using the following code:

 func didBeginContact(contact: SKPhysicsContact) {

        //Chicken1

        if (contact.bodyA.categoryBitMask == ColliderType.Savior.rawValue && contact.bodyB.categoryBitMask == ColliderType.Chicken1.rawValue )  {

            println("chicken1 contact made")
            chicken1.hidden = true
            chicken1.setScale(0)

            animalsCount++
            animalsCountLabel.text = "\(animalsCount)"


        } else if (contact.bodyA.categoryBitMask == ColliderType.Chicken1.rawValue && contact.bodyB.categoryBitMask == ColliderType.Savior.rawValue)  {

            println("chicken1 contact made")
            chicken1.hidden = true
            chicken1.setScale(0)

        }

else if语句中的分数未增加因为它不可能在我的游戏中发生。

Score is not increased in the else if statement because it can't happen in my game.

问题是 animalsCount 增加2,而不是1,每次救星 chicken1 发生冲突。

The problem is that animalsCount increases by 2, not 1, every time savior and chicken1 collide.

经过一些故障排除后,我发现这不是因为两个碰撞体的分数都在增加。并非如此,因为只满足了1行代码。这是唯一满足的行:

After some troubleshooting, I found out this is NOT because the score is being increased for both of the colliding bodies. This is not the case because only 1 line of code is ever satisfied. This is the only line that is satisfied:

if (contact.bodyA.categoryBitMask == ColliderType.Savior.rawValue)

分数是2而不是1,因为救星似乎从 chicken1 反弹,因此 contact.bodyA.categoryBitMask 设置为等于 ColliderType.Savior.rawValue 每次出现一次碰撞就两次。

The score goes up by 2 instead of 1 because savior seems to "bounce" off of chicken1 so that contact.bodyA.categoryBitMask is set equal to ColliderType.Savior.rawValue TWICE every time collision appears to occur ONCE.

我不知道如何解决此问题。我如何做到只检测到一次碰撞,所以分数只增加一次?

I don't know how to fix this problem. How do I make it so that collision is only detected ONCE and so the score is only increased once?

推荐答案

我最终使用一个控制变量if的Int变量解决了这个问题,因此只有在Sprite节点循环之前,才可以检测到一次冲突通过并重置变量。

I eventually solved the problem using an Int variable that controlled if statements so collision could only be detected once until the sprite node cycled through and the variable was reset.

我声明了一个名为 chickenHasBeenGrabbed 的变量,并将其初始设置为0。第一次检测到碰撞后,我将 chickenHasBeenGrabbed 设置为1。只有将 chickenHasBeenGrabbed 设置为0后,我才能再次检测到冲突:

I declared a variable called chickenHasBeenGrabbed and set it at 0 initially. Once collision had been detected that first time, I set chickenHasBeenGrabbed to 1. Only after chickenHasBeenGrabbed was set back to 0 could collision be detected again:

func didBeginContact(contact: SKPhysicsContact) {

        //Chicken1

        if chickenHasBeenGrabbed == 0  {

        if (contact.bodyA.categoryBitMask == ColliderType.Savior.rawValue && contact.bodyB.categoryBitMask == ColliderType.Chicken1.rawValue )  {

            println("chicken1 contact made")
            chicken1.hidden = true
            chicken1.setScale(0)

            animalsCount += 1
            animalsCountLabel.text = "\(animalsCount)"

            chickenHasBeenGrabbed = 1


        } else if (contact.bodyA.categoryBitMask == ColliderType.Chicken1.rawValue && contact.bodyB.categoryBitMask == ColliderType.Savior.rawValue)  {

            println("chicken1 contact made")
            chicken1.hidden = true
            chicken1.setScale(0)

        }
        }

        else if chickenHasBeenGrabbed == 1 {

            if (contact.bodyA.categoryBitMask == ColliderType.Savior.rawValue && contact.bodyB.categoryBitMask == ColliderType.Chicken1.rawValue )  {

                println("nothing to do; chicken was already grabbed!")


            } else if (contact.bodyA.categoryBitMask == ColliderType.Chicken1.rawValue && contact.bodyB.categoryBitMask == ColliderType.Savior.rawValue)  {

                println("nothing to do; chicken was already grabbed!")

            }}

这篇关于斯威夫特:由于两次检测到碰撞,得分增加了两倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆