Search code examples
sprite-kitgame-physicsmaskskphysicsbody

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


So, i am creating a game and I'm trying to code physics boundaries for the hero so he doesn't just fall through the floor on launch lol, So i implemented my scene and I'm trying to create an alpha mask texture physics body on my "desertForeground" and i don't get any errors, however it keeps crashing and i get this error and the debugging doesn't tell me much, it just basically says invalid instruction but doesn't tell me why is wrong. Error screenshot Can anybody help me out? I'm pretty new to coding physics bodies in 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
 }
 }

Solution

  • The problem is how you populate desertForegroundTexture.

    Replace this

    let desertForegroundTexture = SKTexture()
    

    with this

    let desertForegroundTexture = SKTexture(imageNamed: "YOUR_IMAGE_NAME")