Alpha mask/texture 物理体在启动时出现致命错误 - swift

Alpha mask/texture physics body fatal error on launch - swift

所以,我正在创建一个游戏,我正在尝试为英雄编写物理边界,这样他就不会在启动时从地板上掉下来哈哈,所以我实现了我的场景,我正在尝试创建我的 "desertForeground" 上有一个 alpha 蒙版纹理物理体,我没有收到任何错误,但是它一直崩溃,我收到这个错误,调试并没有告诉我太多信息,它基本上只是说指令无效但没有不要告诉我为什么错了。 谁能帮帮我?我对 swift

中的物理体编码还很陌生
import SpriteKit

enum BodyType:UInt32 {

case hero =    01

case bullet1 = 010

case enemy1 =  0100
case enemy2 =  01000
case enemy3 =  010000

case desertForegroundCase = 02
}
class GameScene: SKScene, SKPhysicsContactDelegate {

    let desertBackground = SKSpriteNode (imageNamed: "RZ- 
DesertBackground.png")
let desertForeground = SKSpriteNode (imageNamed: "RZ-
DesertForeground.png")
let desertgully = SKSpriteNode (imageNamed: "RZ-DesertGully.png")

     override func didMoveToView(view: SKView) {
       CreateScene()
}
    func CreateScene (){

    desertForeground.position = CGPoint(x: 570 , y: 102)
    desertForeground.size = CGSize (width: 1136, height: 200)
    desertForeground.zPosition = 1

    let desertForegroundTexture = SKTexture()
    let desertForegroundBody = SKPhysicsBody(texture: 
    desertForegroundTexture, size: CGSize(width: 1136, height: 200))

    desertForeground.texture = desertForegroundTexture

    desertForegroundBody.dynamic = true
    desertForegroundBody.affectedByGravity = false
    desertForegroundBody.allowsRotation = false
    desertForegroundBody.categoryBitMask = 
                   BodyType.desertForegroundCase.rawValue
    desertForegroundBody.contactTestBitMask = BodyType.enemy1.rawValue 
    | BodyType.enemy2.rawValue | BodyType.enemy3.rawValue  | 
      BodyType.soldierL.rawValue | BodyType.soldierT.rawValue

    desertForeground.physicsBody = desertForegroundBody
 }
 }

问题在于您如何填充 desertForegroundTexture

替换这个

let desertForegroundTexture = SKTexture()

有了这个

let desertForegroundTexture = SKTexture(imageNamed: "YOUR_IMAGE_NAME")

当您尝试从空纹理创建 physicBody 时出现问题:

/**
     Creates a body from the alpha values in the supplied texture.
     @param texture the texture to be interpreted
     @param size of the generated physics body
     */
    @available(iOS 8.0, *)
    public /*not inherited*/ init(texture: SKTexture, size: CGSize)

事实上,例如,如果您尝试创建这样的 SKSpriteNode,这种事情就不会发生:

let desertForegroundTexture = SKTexture()
desertForeground = SKSpriteNode(texture: desertForegroundTexture, size:CGSize(width: 1136, height: 200))