Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift SKPhysicacbodies似乎稍微偏离了位置_Swift_Sprite Kit - Fatal编程技术网

Swift SKPhysicacbodies似乎稍微偏离了位置

Swift SKPhysicacbodies似乎稍微偏离了位置,swift,sprite-kit,Swift,Sprite Kit,编辑:通过使用PhysicsEditor制作多边形physicsbody而不是使用SKPhysicsBody(…alphaThreshold:…) -- 出于某种原因,我对我假设的SKPhysicacbodies有点不对劲。使用showPhysics时,我的固定障碍物节点的物理体似乎位于正确的位置,但是我能够触发碰撞而不实际接触障碍物。如果你看下面的图片,它显示了我发现物理体偏离中心的位置,尽管showPhysics告诉我不是这样。(注意,玩家节点在这些障碍节点的中间行进)。 我还认为值得注意

编辑:通过使用PhysicsEditor制作多边形physicsbody而不是使用
SKPhysicsBody(…alphaThreshold:…)

--

出于某种原因,我对我假设的SKPhysicacbodies有点不对劲。使用
showPhysics
时,我的固定障碍物节点的物理体似乎位于正确的位置,但是我能够触发碰撞而不实际接触障碍物。如果你看下面的图片,它显示了我发现物理体偏离中心的位置,尽管
showPhysics
告诉我不是这样。(注意,玩家节点在这些障碍节点的中间行进)。

我还认为值得注意的是,当球员在移动时,他的身体似乎稍微向前移动,但我认为这可能是正常的

我还使用
SKPhysicsBody(…alphaThreshold:…)
从.png图像创建物理体

干杯

编辑:下面是我如何创建障碍物物理体的。一旦将它们添加到worldNode中,它们将被单独保留,直到需要删除为止。除此之外,我不会以任何方式改变它们

let obstacleNode = SKSpriteNode(imageNamed: ... )
obstacleNode.position = CGPoint(x: ..., y: ...)
obstacleNode.name = "obstacle"

obstacleNode.physicsBody = SKPhysicsBody(texture: obstacleNode.texture!, alphaThreshold: 0.1, size: CGSize(width: obstacleNode.texture!.size().width, height: obstacleNode.texture!.size().height))
obstacleNode.physicsBody?.affectedByGravity = false
obstacleNode.physicsBody?.isDynamic = false
obstacleNode.physicsBody!.categoryBitMask = CC.wall.rawValue
obstacleNode.physicsBody!.collisionBitMask = CC.player.rawValue
obstacleNode.physicsBody!.contactTestBitMask = CC.player.rawValue

worldNode.addChild(obstacleNode)
玩家节点的处理方式相同,下面是玩家的移动方式

playerNode.physicsBody?.velocity = CGVector(dx: dx, dy: dy)

我假设您没有显示用于创建
SKSpriteNode
SKPhysicsBody
实例的确切图像。由于您正在使用纹理定义
SKPhysicsBody
的形状,因此您可能遇到以下问题:

如果不想创建自己的形状,可以使用SpriteKit基于精灵的纹理为自己创建形状

这很简单,也很方便,但有时可能会产生意想不到的结果,具体取决于您用于精灵的纹理。也许试着做一个显式的面具或者用一个简单的形状来代表你的物理身体。该文档中有非常好的示例和指南

在设置对象的属性时,我也会遵循此模式:

// safely unwrap and handle failure if it fails
guard let texture = obstacleNode.texture else { return } 

// create the physics body
let physicsBody = SKPhysicsBody(texture: texture, 
                                alphaThreshold: 0.1, 
                                size: CGSize(width: texture.size().width, 
                                             height: texture.size().height))

// safely set its properties without the need to unwrap an Optional
physicsBody.affectedByGravity = false

// set the rest of the properties

// set the physics body property on the node
obstacleNode.physicsBody = physicsBody

通过在
SKPhysicsBody
的具体实例上设置属性,并完全展开和测试
Optionals
,您可以最大限度地减少运行时崩溃的可能性,这可能很难调试。

您应该包含您的代码,这样我们就可以看到问题的根源。我添加了一些代码这些精灵正在移动吗机会?你的身体可能会领先于你的图形,因为在玩家节点移动时,绘制阶段没有发生。我想是这样的,不过障碍物是固定的。谢谢你的帮助,我现在就来调查一下