栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

iOS SpriteKit-碰撞和联系人无法按预期工作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

iOS SpriteKit-碰撞和联系人无法按预期工作

为了帮助诊断这些类型的问题,我编写了一个可以在任何地方调用的函数,该函数将分析当前场景并生成一个列表,列出哪些节点与其他节点发生碰撞以及将通知我的场景发生哪些碰撞。

该功能是独立的,不需要告知有关场景中节点的任何信息。

功能如下:

 //MARK: - Analyse the collision/contact set up.    func checkPhysics() {        // Create an array of all the nodes with physicsBodies        var physicsNodes = [SKNode]()        //Get all physics bodies        enumerateChildNodesWithName("//.") { node, _ in if let _ = node.physicsBody {     physicsNodes.append(node) } else {     print("(node.name) does not have a physics body so cannot collide or be involved in contacts.") }        }//For each node, check it's category against every other node's collion and contctTest bit mask        for node in physicsNodes { let category = node.physicsBody!.categoryBitMask // Identify the node by its category if the name is blank let name = node.name != nil ? node.name : "Category (category)" let collisionMask = node.physicsBody!.collisionBitMask let contactMask = node.physicsBody!.contactTestBitMask // If all bits of the collisonmask set, just say it collides with everything. if collisionMask == UInt32.max {     print("(name) collides with everything") } for otherNode in physicsNodes {     if (node != otherNode) && (node.physicsBody?.dynamic == true) {         let otherCategory = otherNode.physicsBody!.categoryBitMask         // Identify the node by its category if the name is blank         let otherName = otherNode.name != nil ? otherNode.name : "Category (otherCategory)"         // If the collisonmask and category match, they will collide         if ((collisionMask & otherCategory) != 0) && (collisionMask != UInt32.max) {  print("(name) collides with (otherName)")         }         // If the contactMAsk and category match, they will contact         if (contactMask & otherCategory) != 0 {print("(name) notifies when contacting (otherName)")}     } }        }      }

您还需要检查以下三件事:

  1. 现场 一定
    SKPhysicsContactDelegate
  2. 你必须设置
    physicsWorld.contactDelegate = self
  3. 您需要实现以下一种可选方法
    SKPhysicsContactDelegate

didBeginContact

didEndcontact

设置完所有图片后应调用该函数-通常在

didMoveToView
工作结束时:

checkPhysics()

didMoveToView
在我的练习Swift项目的结尾处调用此函数时,将得到以下输出:

Optional(“ shape_blueSquare”)与Optional(“ Category 2147483648”)碰撞Optional(“
shape_blueSquare”)与Optional(“ shape_redCircle”)碰撞Optional(“
shape_blueSquare”)与Optional(“ shape_purpleSquare”)碰撞Optional(“
shape_blueSides)与Optional(“ shape_yellowTriangle”)Optional(“
shape_redCircle”)碰撞Optional(“ Category 2147483648”)Optional(“
shape_redCircle”)与Optional(“ shape_blueSquare”)碰撞Optional(“
shape_redCircle”)在与Optional(“ shape_purpleSquare”联系时通知)Optional(“
shape_redCircle”)与Optional(“ shape_yellowTriangle”)发生冲突。Optional(“
shape_redCircle”)在与Optional(“shape_yellowTriangle“)Optional(”
shape_purpleSquare“)与Optional(” Category 2147483648“)Optional(”
shape_purpleSquare“)碰撞与Optional(” shape_yellowTriangle“)Optional(”
shape_yellowTriangle“)碰撞与所有didBere_replace发生冲突的对象()和Optional(“
shape_redCircle”)didBeginContact输入了Optional(“
shape_purpleSquare”)和Optional(“
shape_redCircle”)didBeginContact输入了Optional(“
shape_yellowTriangle”)和Optional(“
shape_redCircle”)shape_yellowTriangle“)Optional(”
shape_yellowTriangle“)与为Optional(”
shape_purpleSquare“)输入的didBeginContact和为Optional(”
shape_purpleSquare“)输入的Optional(” shape_redCircle“)和为Optional(”
shape_purpleSquare“)输入的Optional(”
shape_redCircle“)didBeginContact的所有碰撞shape_yellowTriangle“)和Optional(”
shape_redCircle“)shape_yellowTriangle“)Optional(”
shape_yellowTriangle“)与为Optional(”
shape_purpleSquare“)输入的didBeginContact和为Optional(”
shape_purpleSquare“)输入的Optional(” shape_redCircle“)和为Optional(”
shape_purpleSquare“)输入的Optional(”
shape_redCircle“)didBeginContact的所有碰撞shape_yellowTriangle“)和Optional(”
shape_redCircle“)))


类别2147483648是我的边界,并且没有名称。我给了这个类别以匹配它的crashBitMask

请随时尝试使用此功能,让我知道它是否有用或在任何情况下均无法解决。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616399.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号