PhysicsBody:无法创建物理体 [英] PhysicsBody: Could not create physics body

查看:20
本文介绍了PhysicsBody:无法创建物理体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 App Store 上有一个完整的可运行游戏,但从 iOS 13 开始,它根本无法运行.我已经通过 Xcode 将游戏安装到我的设备上,但出现了很多错误:

PhysicsBody:无法创建物理体.

我一直在像这样创建我的 SKSpriteNode:

 let bird = SKSpriteNode(texture: SKTextureAtlas(named:"player").textureNamed("bird0001"))bird.physicsBody = SKPhysicsBody(纹理:bird.texture!,尺寸:鸟.尺寸)

根据一些研究,这可能是 iOS 和 Xcode 的一个持续存在的错误.有人可以确认是否是这种情况,因为对于使用纹理创建 SKSpriteNodes 的应用商店上的游戏来说,这似乎是一个主要问题?

在需要纹理的地方有解决办法吗?

解决方案

好的,从 iOS 13.3(现在在 13.3.1 也尝试编辑)和 Xcode 11.3 版本开始,这里测试了避免此错误的不同方法.此链接的完整测试源代码:

I have an entire working game that has been up on the App Store, but since iOS 13 it simply does not work. I've installed the game onto my device via Xcode and I'm getting a lot of errors coming up saying:

PhysicsBody: Could not create physics body.

I've been creating my SKSpriteNodes like this:

 let bird = SKSpriteNode(texture: SKTextureAtlas(named:"player").textureNamed("bird0001"))
 bird.physicsBody = SKPhysicsBody(texture: bird.texture!,
                                         size: bird.size)

Based on some research, this is possibly an ongoing bug with iOS and Xcode. Could someone please confirm if this is the case, as this seems to be like a major problem for Games on the app store that create their SKSpriteNodes using textures?

Is there a fix to this where textures are needed?

解决方案

OK, here's a test of different approaches for avoiding this bug as of iOS 13.3 (edit also now tried on 13.3.1) and Xcode version 11.3. Full source of the test at this link:

https://github.com/bg2b/bugtest

Relevant code:

  func addShip(_ texture: SKTexture, how: String) {
    let sprite = SKSpriteNode(texture: texture)
    sprite.position = CGPoint(x: x, y: 0)
    sprite.zRotation = .pi / 4
    x += 100
    sprite.physicsBody = SKPhysicsBody(texture: texture, size: sprite.size)
    if sprite.physicsBody == nil {
      print("(how) failed")
    } else {
      print("(how) worked")
    }
    addChild(sprite)
  }

  override func didMove(to view: SKView) {
    // The atlas version of a texture
    addShip(SKTexture(imageNamed: "ship_blue"), how: "simple atlas reference")
    // From an atlas, but call size() to force loading
    let texture = SKTexture(imageNamed: "ship_blue")
    _ = texture.size()
    addShip(texture, how: "atlas force load")
    // Reconstruct via CGImage (size would be wrong because of 2x)
    let cgTexture = SKTexture(cgImage: texture.cgImage())
    addShip(cgTexture, how: "reconstruct via cgImage")
    // Re-render using view
    let renderedTexture = view.texture(from: SKSpriteNode(texture: texture))!
    addShip(renderedTexture, how: "re-render using view")
    // Non-atlas texture
    addShip(SKTexture(imageNamed: "nonatlas_ship_blue"), how: "not in atlas")
  }

Summary:

  1. Simply referencing the texture from an atlas and making the physics body may fail
  2. Force-loading the texture by calling size() before making the body fails
  3. Trying to make a new texture by going through cgImage() fails (the image itself is broken, probably related to the same bug)
  4. Rendering to a texture using a view and then making the physics body from that new texture works
  5. Making the physics body from a non-atlas copy of the texture works

Console output from the test program showing what works and what does not:

2020-02-01 06:23:51.872975-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
simple atlas reference failed
2020-02-01 06:23:51.886387-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
atlas force load failed
2020-02-01 06:23:51.913927-0500 bugtest[14399:9898087] PhysicsBody: Could not create physics body.
reconstruct via cgImage failed
re-render using view worked
not in atlas worked

Here's a screen shot showing the effect of the different approaches. You have to look a bit closely, but only the last two have valid physics bodies.

这篇关于PhysicsBody:无法创建物理体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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