Ios 如何在游戏场景中从ViewController调用方法

Ios 如何在游戏场景中从ViewController调用方法,ios,swift,uiviewcontroller,sprite-kit,skscene,Ios,Swift,Uiviewcontroller,Sprite Kit,Skscene,我的viewController中有一个自定义segue的方法,如下所示: func gameOver() { performSegueWithIdentifier("GameOver", sender: nil) } @objc protocol GameOverDelegate { func gameOverDelegateFunc() } gamescene_delegate?.gameOverDelegateFunc() 我在GameSecene.swift中这样调用该

我的viewController中有一个自定义segue的方法,如下所示:

func gameOver() {
    performSegueWithIdentifier("GameOver", sender: nil)
}
@objc protocol GameOverDelegate {
  func gameOverDelegateFunc()
}
gamescene_delegate?.gameOverDelegateFunc()
我在GameSecene.swift中这样调用该方法:

 GameViewController().gameOver()
我仔细检查了赛格的名字,它是正确的。每当我在GameSecene.swift文件中调用它时,我都会收到SIGABRT消息,我不知道为什么。我尝试只使用println()消息调用该函数,结果成功了

如果您能提供任何关于为什么会发生这种情况以及如何在GameScene.swift文件中成功调用该方法的建议,我们将不胜感激

谢谢

注意:这是事故日志:

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
2015-01-28 21:59:46.181 RunawaySquare[95616:3907041]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“接收器()没有标识符为“GameEnd”的序列”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x000000010d461f35例外预处理+165
1 libobjc.A.dylib 0x000000010f39ebb7 objc_异常_抛出+45
2 UIKit 0x000000010e20dd3b-[UIViewController应使用标识符执行SEGUE:发送方:][0
3逃逸方0x000000010d2683b2 _TFC13逃逸方18游戏视图控制器8游戏控制0_FT_T_+914
4逃逸方0x000000010d261af0\u TFC13逃逸方游戏场景12使用事件CSO7UIEvent+1808设置逃逸方
5逃逸方0x000000010d261c3f\u TTOFC13逃逸方游戏场景12带有事件CSO7UIEvent\u T\u79
6 SpriteKit 0x000000010df4d7e1-[SKView触控开始:withEvent:][946
7 UIKit 0x000000010e12d16e-[UIWindow\u 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\uuuu CFRUNLOOP\u正在调用\u OUT\u以执行\u函数\uuu+17
13 CoreFoundation 0x000000010d38d41d\uu CFRunLoopDoSources0+269
14 CoreFoundation 0x000000010d38ca54__CFRunLoopRun+868
15 CoreFoundation 0x000000010d38c486 CFRunLoopRunSpecific+470
16图形服务0x000000011480e9f0 GSEventRunModal+161
17 UIKit 0x000000010e0e6420 UIApplicationMain+1282
18逃逸广场0x000000010d26cbee顶层代码+78
19失控区0x000000010d26cc2a干管+42
20 libdyld.dylib 0x000000010fb8a145启动+1
)

libc++abi.dylib:以NSException类型的未捕获异常终止

它说没有segue id。带有“GameEnd”,但有一个,如果在viewcontroller上使用它就可以了。这不起作用的原因是您正在创建一个新的
GameViewController
实例,然后您正在调用
gameOver
。您真正想做的是引用现有的
GameViewController

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

将viewController属性添加到游戏场景类中

class GameScene {

    // we need to make sure to set this when we create our GameScene
    var viewController: GameViewController!
// somewhere in GameScene
self.viewController.gameOver()
在GameViewController文件中

// after GameScene is instantiated
gameScene.viewController = self
现在我们有了对viewController的引用,让我们在游戏场景类中使用它

class GameScene {

    // we need to make sure to set this when we create our GameScene
    var viewController: GameViewController!
// somewhere in GameScene
self.viewController.gameOver()

我没有足够的代表来回复您的上一条评论,但如果您仍然收到该错误,那是因为您尚未在Interface Builder中为该段指定名称(或标识符)

在Interface Builder中单击并拖动以在视图控制器之间绘制分段时,可以通过分段线中心显示的圆圈图标选择每个过渡,并为其指定一个“标识符”。然后,当您调用
performsguewithidentifier
时,它应该可以工作

这是圆形图标的外观,取决于segue的类型:


如果你有点不确定分段,请查看或

我不知道这是否仍然相关,但我想为您介绍一个解决此问题的方法

正如您在这里看到的,我不久前遇到了完全相同的问题,我使用协议解决了这个问题

问题是,您只能在GameViewController类中调用“performsgueWithIdentifier(“GameOver”,sender:nil)”,但您希望从游戏场景中执行它

因此,您可以在游戏场景中创建如下协议:

func gameOver() {
    performSegueWithIdentifier("GameOver", sender: nil)
}
@objc protocol GameOverDelegate {
  func gameOverDelegateFunc()
}
gamescene_delegate?.gameOverDelegateFunc()
以及游戏场景中代理的变量:

var gamescene_delegate : GameOverDelegate?
在GameViewController类中,必须在类定义中添加委托

class GameViewController: UIViewController, GameOverDelegate {
...
}
并将GameViewController的viewDidLoad函数中的场景代理设置为self:

scene.gamescene_delegate = self
最后一步是在GameViewController中实现gameOverDelegateFunc()函数:

func gameOverDelegateFunc() {
   self.performSegueWithIdentifier("GameOver", sender: nil)
}
这就是你要做的一切

每当你想在游戏场景中执行此步骤时,你只需通过代理调用函数,如下所示:

func gameOver() {
    performSegueWithIdentifier("GameOver", sender: nil)
}
@objc protocol GameOverDelegate {
  func gameOverDelegateFunc()
}
gamescene_delegate?.gameOverDelegateFunc()
我希望一切都很清楚我能帮上忙

问候,,
菲尔

我已经通过创建协议完成了, 我有3个游戏场景(GameSecene、GamePlaySecene、GameEndScene)和一个游戏控制器(GameViewController)

首先创建游戏协议

protocol GameDelegate {
 func gameOver()
}
在GameViewController中实现协议

class GameViewController: UIViewController, GameDelegate {
override func viewDidLoad() {
 super.viewDidLoad()
 let scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill
        scene.delegate = self
 }

 // MARK: Game Delegate
 func gameOver() {
    self.performSegueWithIdentifier("yoursegue", sender: self)
 }
}
在游戏场景类中放置委托属性

class GameScene: SKScene {
 var delegate: GameDelegate?

 // call GamePlayScene and put delegate property
 func redirectToPlay() {
        let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration: 1.0)
        let menuScene = GamePlayScene(size: size)
         menuScene.delegate = self.delegate
        self.view?.presentScene(menuScene, transition: transition)
   }
}
在游戏场景中加入协议

class GamePlayScene: SKScene {
     var delegate: GameDelegate?

     // call GameEndScene and put delegate property
     func gameScore() {
            let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration: 1.0)
            let menuScene = GameEndScene(size: size)
             menuScene.delegate = self.delegate
            self.view?.presentScene(menuScene, transition: transition)
       }
    }
最后,放置委托属性并调用gameOver函数

class GameEndScene: SKScene {
     var delegate: GameDelegate?

     init(size: CGSize) {
      // call gameOver function
      self.delegate?.gameOver()
     }
   }
希望这项工作能对你有所帮助

真诚地,
Donny

你收到关于调试程序的任何崩溃报告吗?“终止于