Swift Spritekit我检测到碰撞,但读取了多次碰撞

编程入门 行业动态 更新时间:2024-10-28 03:22:44
本文介绍了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我检测到碰撞,但读取了多次碰撞

本文发布于:2023-06-04 17:30:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/502170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:检测到   Swift   Spritekit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!