有谁知道我切换场景时我的应用程序崩溃的原因?

时间:2017-11-12 11:30:08

标签: swift button sprite-kit runtime-error transition

我是编码的新手,我用Sprite-Kit创建了一个带有两个场景(游戏场景和主菜单)的游戏。我有一个主页按钮,可以从游戏场景切换回主菜单场景。当我点击主页按钮时,应用程序崩溃,我还无法找到解决方案。

这是错误(运行touchesEnded()时发生):

    libc++abi.dylib: terminating with uncaught exception of type NSException

    libsystem_kernel.dylib`__pthread_kill:
    0x112417d38 <+0>:  movl   $0x2000148, %eax          ; imm = 0x2000148 
    0x112417d3d <+5>:  movq   %rcx, %r10
    0x112417d40 <+8>:  syscall 
->  0x112417d42 <+10>: jae    0x112417d4c               ; <+20>
    0x112417d44 <+12>: movq   %rax, %rdi
    0x112417d47 <+15>: jmp    0x112410caf               ; cerror_nocancel
    0x112417d4c <+20>: retq   
    0x112417d4d <+21>: nop    
    0x112417d4e <+22>: nop    
    0x112417d4f <+23>: nop   

这是我的代码:

var homeButton = SKSpriteNode(imageNamed: "HomeButton5.0")

class GameScene{

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

     for touch in touches{

       let locationUser = touch.location(in: self)

     if atPoint(locationUser) == homeButton {

      }
    }
  }

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches{

      let locationUser = touch.location(in: self)

       if atPoint(locationUser) == homeButton { // <-- error/ crash 


        let transition = SKTransition.crossFade(withDuration: 0.2)
        let mainMenu = Mainmenu(size: self.size)  

        self.view?.presentScene(mainMenu, transition: transition) //transition
       }
     }
   }
 }


class Mainmenu: SKScene{  //Scene to switch to

}

控制台: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用此代码切换场景。

let reveal = SKTransition.flipVertical(withDuration: 0.5)
let gameScene = Game(size: self.size)
self.view?.presentScene(gameScene, transition: reveal)