-1

I have been working on some code that uses a button to call a scene. However it won't work on my IPad and I think I may have done a few things wrong, would anyone be willing to tell me how to get it to work. Here is my code:

import SpriteKit
import UIKit
import PlaygoundSupport

class ButtonNode: SKSpriteNode {
   var action: ((ButtonNode) -> Void)?

   var isSelected: Bool = false{
      didSet{
          alpha = isSelected ? 0.8 : 1
      }
    }
    required init(coder: NSCoder){
        fatalError("NSCoding not supported")
    }
    init(texture: SKTexture, size: CGSize){
       super.init(texture: texture, color: SKColor.white, size:size)
       isUserInteractionEnabled = true
    }
    override func touchesBegan(with event: NSEvent){
        action?(self)
    }
    override func mouseDown(with event: NSEvent){
        action?(self)
    }
}
class GameScene: SKScene{
   override func didMove(to view: SKView){
   }
}
let scene = GameScene(size: CGSize(width: 400, height: 640)
scene.scaleMode = .aspectFill
scene.backgroundColor = .gray

let view = SKView(frame: CGRect(x:0, y:0, width: scene.size.width, height: scene.size.height))
view.presentScene(scene)
PllygroundPage.current,liveView = view
start button = ButtonNode(texture: SKTexture(imageNamed: "start-button"), size: CGSize(width:184, height: 72))
startButton.position = CGPoint(x: 0.0, y: 0.0)
startButton.action = { (button) in
if let scene = IntroCutScene(fileNamed: "IntroCutScene"){
    self.scene!.scaleMode = .aspectFill
    GameVariables.sceneView.presentScene(scene, transition: SKTransition.moveIn(with: SKTransitionDirection.right, duration: 2.5))
    }
}
self.addChild(startButton)

the error message that shows up is: use of undeclared type 'NSEvent'

Oscar Biggins
  • 13
  • 1
  • 8
  • Put some breakpoints in your code and step through it. Also share your error your are getting maybe? – chirag90 Jan 28 '20 at 11:09
  • 2
    The final few lines of code won't even compile, it's very difficult to help you if this isn't your actual code and you can't be more specific than "doesn't work in general" – jrturton Jan 28 '20 at 11:13
  • 1
    What do you mean exactly by, "won't work?" Any error messages, compiler notes? – Magnas Jan 28 '20 at 12:20
  • Please show real code, not fake stuff that you type directly into SO. Copy your actual code and paste it right into the question. Do not lie to SO about what your code is, or we cannot help you with it. – matt Jan 28 '20 at 15:47

1 Answers1

1

You are importing UIKit (rightly, since you want to run on an iPad). The event type on UIKit is UIEvent, not NSEvent. You've implemented the wrong method from the wrong platform. Looks like you've implemented this method:

https://developer.apple.com/documentation/appkit/nsresponder/1531151-touchesbegan

This is the one you want:

https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan

(and so on).

matt
  • 515,959
  • 87
  • 875
  • 1,141