为什么为我的 SKSpriteNode 启用 physicBody 会导致异常? [英] Why does enabling a physicBody to my SKSpriteNode causes abnormalities?
问题描述
我对这个问题进行了多次更改,因此我将其移至一个新线程:
在没有看到更完整的代码示例的情况下,很难判断为什么在第二个屏幕截图中打开动态时红色矩形精灵会向下移动.
至于 SpriteKit 动态调试可视化看起来不对,这是因为您正在更改 sprites anchorPoint
属性.该属性定义了节点的原点点,即节点的中心点(默认为CGPoint(x: 0.5, y: 0.5)
).
如果您查看 SKPhysicsBody(rectangleOf:)
方法的文档,您会看到默认情况下物理形状是在原点创建的:
创建一个以拥有节点的原点为中心的矩形物理体.
如果您想将精灵的 anchorPoint
保持为零,您需要手动指定物理形状的中心点.对于本示例,我们假设您要查找的边框矩形大小为 600x10.您需要使用 SKPhysicsBody(rectangleOf:center:)
方法来偏移物理体中心:
let borderSize = CGSize(width: 600, height: 10)mBorder.physicsBody = SKPhysicsBody(rectangleOf: borderSize, center: CGPoint(x: borderSize.width/2, y: borderSize.height/2))
I have changed this question more than a bit, so I moved it to a new thread: How can I set a physicsbody to be properly centered?
I am just starting with SKPhysicsBody, and I do have view.showsPhysics = true
Below is the code where I create a simple red border, which comes out just fine, and exactly where I would like it to be.
However, when I uncomment the two lines that apply physicsBody
to the border, it moves unpredictably and even the physics debug lines from view.showsPhysics = true
are out of sync with the border line.
Below is the code, and the screenshot provided on the left is what I want. The screenshot on the right shows what happens when I apply physicsBody to the SKSpriteNode.
Obviously I am applying something incorrectly, but I can't figure it out.
func createBorder () -> SKSpriteNode
{
let mBorder = MyBorder(color: .red, size: CGSize(
width: myGlobalVars.widthPoints,
height: myGlobalVars.heightPoints * 0.01 ))
mBorder.isHidden = true
mBorder.anchorPoint = CGPoint(x: 0, y: 0)
mBorder.position = CGPoint(x: 0, y: myGlobalVars.heightPoints * 0.2)
// mBorder.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: myGlobalVars.widthPoints, height: myGlobalVars.heightPoints * 0.01))
// mBorder.physicsBody!.affectedByGravity = false
mBorder.zPosition = theZ.theBoarder
mBorder.isHidden = false
myGlobalVars.gameScene!.addChild(mBorder)
return mBorder
}
p.s. Please ignore the first screen showing a gray bar. I had just changed the color.
Without seeing a more complete code example, it's hard to gauge why the red rectangular sprite has shifted downwards with dynamics turned on in the second screenshot.
As for the SpriteKit dynamics debug visualization looking wrong, that's happening because you're changing the sprites anchorPoint
property. This attribute defines the node's origin point, as in the point that the node should be centered on (the default is CGPoint(x: 0.5, y: 0.5)
).
If you look at the documentation for the SKPhysicsBody(rectangleOf:)
method, you'll see that by default the physics shape is created at the origin:
Creates a rectangular physics body centered on the owning node’s origin.
If you want to keep the sprite's anchorPoint
at zero, you'll need to specify the center point of the physics shape manually. For the sake of this example, let's assume the border rectangle size you're looking for is 600x10. You'll want to use the SKPhysicsBody(rectangleOf:center:)
method to offset the physics body center:
let borderSize = CGSize(width: 600, height: 10)
mBorder.physicsBody = SKPhysicsBody(rectangleOf: borderSize, center: CGPoint(x: borderSize.width / 2, y: borderSize.height / 2))
这篇关于为什么为我的 SKSpriteNode 启用 physicBody 会导致异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!