Swift Spritekit我检测到碰撞,但读取了多次碰撞 [英] Swift Spritekit I detect a collison but it reads the collision mulitple times

查看:47
本文介绍了Swift Spritekit我检测到碰撞,但读取了多次碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppImage 我有一个由四个不同颜色的矩形组成的墙,穿过墙壁,球的颜色必须与墙壁上的矩形的颜色匹配.球将穿过墙壁,并出现新的墙壁.但是,当我检测到此碰撞时,会得到多个碰撞读数.我已经通过打印死活文件进行了测试,并且可以打印两次或多次.

AppImage I have a wall of 4 rectangles that are different colors, to pass through the wall the color of the ball has to match that of the rectangle on the wall. The ball will pass through the wall, and a new wall will appear. However, when I detect this collision I get multiple collision readings. I have tested this by printing dead or alive, and it prints both or more many times.

func didBegin(_ contact: SKPhysicsContact) {

    if let nodeA = contact.bodyA.node as? SKShapeNode, let nodeB = contact.bodyB.node as? SKShapeNode {
        if nodeA.fillColor != nodeB.fillColor {
            print("DEAD")
        }
        else {
            print("Alive")
        }
    }
}      

请帮助!

推荐答案

是的-发生这种情况.处理它的方法(在某些情况下,您不能让sprite-kit多次不调用 didBegin )是确保您的联系代码可以容纳该代码,并且多次处理合同不会导致问题(例如多次添加分数,删除多个生命,尝试访问已删除的节点或PhysicalBody等).

Yep - this happens. The way to handle it (you can't get sprite-kit to NOT call didBegin multiple times in some circumstances) is to make sure that your contact code accommodates this and that handling the contract multiple times does not cause a problem (such as adding to the score multiple times, removing multiple lives, trying to access a node or physicsBody that has been removed etc).

这里有一个讨论: Sprite-Kit为单个联系人注册多个碰撞

您可以做的一些事情包括:

Some things you can do include:

  • 如果删除已联系的节点,请先检查其是否为 nil 您将其删除(用于重复的联系人)
  • 将节点添加到集合中,然后从以下位置删除集合中的所有节点 didFinishUpdate
  • 向节点的 userData
  • 添加无效"标志
  • 将节点设为SKSpriteNode的子类,并添加 inactive 属性
  • 等等.
  • If you remove a node that is contacted, check for it being nil before you remove it (for the duplicate contacts)
  • Add the node to a set and then remove all the nodes in the set in didFinishUpdate
  • Add an 'inactive' flag' to the node's userData
  • Make the node a subclass of SKSpriteNode and add an inactive property
  • Etc etc.

这篇关于Swift Spritekit我检测到碰撞,但读取了多次碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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