-1

I get this message: "libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)" in my console whenever I press my button in my program. I'm not sure why this is happening...

Here is my code for the button:

class gameOverScene: SKScene {

  override func didMove(to view: SKView) {

    let backgroundNode = SKSpriteNode(imageNamed: "background")
    backgroundNode.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height / 2)
    self.addChild(backgroundNode)

    let button = UIButton(type: .system)
    button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
    button.backgroundColor = UIColor.red
    button.setTitle("New Game", for: .normal)
    button.addTarget(self, action: Selector(("action:")), for: UIControlEvents.touchUpInside)
    self.view?.addSubview(button)

    func action(sender:UIButton!) {
        print("button")
    }

    func buttonAction(sender: UIButton!) {

    }
  } // end didMove(to view:)
} // end class declaration
Adrian
  • 16,233
  • 18
  • 112
  • 180
AglaiaZ
  • 31
  • 6
  • Why are you using UIKit objects with SKScene? – El Tomato Jun 15 '18 at 14:52
  • Show the whole error message. It's usually giving good highlights ont what is going one. For instance, I guess it's saying "[YourProject.YourViewController.gameOverScene action:] unrecognized selector sent to instance" because the selector you used (why no `#selector` by the way) doesn't seem to match the signature (there is no "sender" in it). – Larme Jun 15 '18 at 14:52
  • In the debug console, there will be a list of stuff that happened just before the crash. Post the backtrace that's outputted along with your question. Working from the top down, it will show you where your problem is. – Adrian Jun 15 '18 at 14:56

1 Answers1

1
  1. Move your handler function out of func didMove(to:) to make it member of class

  2. Add @objc in your handler function declaration (i.e. @objc func action(sender: UIButton!))

  3. Then change creation of the selector to this: #selector(gameOverScene.action(sender:))

Anton Filimonov
  • 1,655
  • 15
  • 19