【问题标题】:Does anyone know why my app crashes when I switch scenes?有谁知道为什么我的应用程序在切换场景时崩溃?
【发布时间】:2017-11-12 11:30:08
【问题描述】:

我是编码新手,我使用 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

}

控制台:

【问题讨论】:

  • 请正确格式化您的代码并使用缩进,没有缩进很难阅读您的代码,这不是您第一个缺少正确格式的问题。
  • 但我正在使用 Sprite-Kit @AndersSørensen
  • 尝试使用:let homeButton = SKSpriteNode(imageNamed: "HomeButton5.0")
  • 那行不通。 @AndersSørensen

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


【解决方案1】:

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

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

【讨论】:

  • 您的应用在哪里崩溃?在 if 语句中还是在它里面?使用打印语句显示位置
  • 'print("1") if atPoint(locationUser) == homeButton { //
  • 这将显示错误发生在 if 语句之前或之后的位置
  • 我明白了:1;主页按钮; 2; libc++abi.dylib:以 NSException (lldb) 类型的未捕获异常终止 @Niall Kehoe
  • 给我看控制台截图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2016-04-01
  • 2017-08-13
相关资源
最近更新 更多