发射时Alpha掩码/纹理物理身体致命错误 - 快速

时间:2016-07-01 22:33:46

标签: sprite-kit game-physics mask skphysicsbody

所以,我正在创建一个游戏,我正在尝试为英雄编码物理边界,所以他不会只是在发射lol时落在地板上,所以我实现了我的场景,我正在尝试创建一个alpha掩码纹理物理体在我的“desertForeground”上,我没有得到任何错误,但它不断崩溃,我得到这个错误,调试并没有告诉我太多,它只是基本上说无效指令但不告诉我为什么是错的。 Error screenshot 任何人都可以帮助我吗?我很擅长快速编写物理机构

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
 }
 }

2 个答案:

答案 0 :(得分:1)

问题在于如何填充desertForegroundTexture

替换此

let desertForegroundTexture = SKTexture()

用这个

let desertForegroundTexture = SKTexture(imageNamed: "YOUR_IMAGE_NAME")

答案 1 :(得分:0)

当您尝试从空纹理创建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))