小编典典

如何从GameScene中的ViewController调用方法

swift

我有一个方法,它在我的viewController中具有自定义序列,如下所示:

func gameOver() {
    performSegueWithIdentifier("GameOver", sender: nil)
}

我在GameScene.swift中这样调用方法:

 GameViewController().gameOver()

我仔细检查了segue名称,它是正确的。每当我在GameScene.swift文件中调用此命令时,我都会收到SIGABRT消息,但我不知道为什么。我尝试仅使用println()消息调用该函数,并且该函数有效。

对于为什么发生这种情况以及如何成功调用GameScene.swift文件中的方法的任何建议,将不胜感激。

谢谢!

PS这里是崩溃日志:

2015-01-28 21:59:46.181 RunawaySquare[95616:3907041] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<RunawaySquare.GameViewController: 0x7fe4305c7890>) has no segue with identifier 'GameEnd''
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d461f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010f39ebb7 objc_exception_throw + 45
    2   UIKit                               0x000000010e20dd3b -[UIViewController shouldPerformSegueWithIdentifier:sender:] + 0
    3   RunawaySquare                       0x000000010d2683b2 _TFC13RunawaySquare18GameViewController8gameOverfS0_FT_T_ + 914
    4   RunawaySquare                       0x000000010d261af0 _TFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 1808
    5   RunawaySquare                       0x000000010d261c3f _TToFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 79
    6   SpriteKit                           0x000000010df4d7e1 -[SKView touchesBegan:withEvent:] + 946
    7   UIKit                               0x000000010e12d16e -[UIWindow _sendTouchesForEvent:] + 325
    8   UIKit                               0x000000010e12dc33 -[UIWindow sendEvent:] + 683
    9   UIKit                               0x000000010e0fa9b1 -[UIApplication sendEvent:] + 246
    10  UIKit                               0x000000010e107a7d _UIApplicationHandleEventFromQueueEvent + 17370
    11  UIKit                               0x000000010e0e3103 _UIApplicationHandleEventQueue + 1961
    12  CoreFoundation                      0x000000010d397551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    13  CoreFoundation                      0x000000010d38d41d __CFRunLoopDoSources0 + 269
    14  CoreFoundation                      0x000000010d38ca54 __CFRunLoopRun + 868
    15  CoreFoundation                      0x000000010d38c486 CFRunLoopRunSpecific + 470
    16  GraphicsServices                    0x000000011480e9f0 GSEventRunModal + 161
    17  UIKit                               0x000000010e0e6420 UIApplicationMain + 1282
    18  RunawaySquare                       0x000000010d26cbee top_level_code + 78
    19  RunawaySquare                       0x000000010d26cc2a main + 42
    20  libdyld.dylib                       0x000000010fb8a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

它说没有segue ID。与“ GameEnd”一起使用,但是只有一个,并且如果在ViewController上使用它就可以工作


阅读 312

收藏
2020-07-07

共1个答案

小编典典

之所以不起作用,是因为您正在创建的新实例,GameViewController然后再调用gameOver它。您真正想做的是参考现有的GameViewController

有几种方法可以做到这一点,我举一个例子。

将viewController属性添加到您的GameScene类

class GameScene {

    // we need to make sure to set this when we create our GameScene
    var viewController: GameViewController!

在您的GameViewController文件中

// after GameScene is instantiated
gameScene.viewController = self

现在我们有一个对viewController的引用,让我们在GameScene类中使用它

// somewhere in GameScene
self.viewController.gameOver()
2020-07-07