How to detect the location of a mouseDown event using Swift?

How to detect the location of a mouseDown event using Swift?

With the following code snippet, I get the error "Cannot find type 'NSEvent' in scope"?

import SpriteKit

func mouseDown(with event: NSEvent) {
    
    if let ourScene = GameScene(fileNamed: "GameScene") {
        
        let location = event.location(in: view)
        let node:SKNode = ourScene.atPoint(location)
        
        if (node.name == "creditsInfo") {
            showCredits()
        }
        
    }   // if let ourScene

}   // mouseDown

Anybody have a floodlight to shine on this very basic error?

  • What's 'view' in event.location(in: view)? If you are already in SpriteKit, not a view controller, that's the cause of confusion.

Add a Comment

Accepted Reply

You should import the needed framework:

import AppKit

or

import Cocoa

Don't forget to close your threads by marking the correct answer once you get it.

Replies

You should import the needed framework:

import AppKit

or

import Cocoa

Don't forget to close your threads by marking the correct answer once you get it.

I think I need to re-group and start from scratch ... I am so, so sorry:

FWIW, your corrections are "spot on" if I were dealing with a macOS App, but I have a iOS App:

How to detect the location of a mouseDown event using Swift?

With the following code snippet, I get the error "Value of type 'UIEvent' has no member 'location' in scope"?

import SpriteKit

extension GameViewController {
    
        func mouseDown(with event: UIEvent) {
            
            if let ourScene = GameScene(fileNamed: "GameScene") {
                
                // error here:
                let location = event.location(in: view)
                let node:SKNode = ourScene.atPoint(location)
                
                if (node.name == "creditsInfo") {
                    showCredits()
                }
                
            }   // if let ourScene
    
        }   // mouseDown
    
    }

Anybody have a floodlight to shine on this very basic error?

OK, that's different from your original post.

Did you try:

import UIKit

(1) an iOS App

(2) Someone else nailed it when he effectively stated that I called an iOS App with a macOS func mouseDown(with event: NSEvent). Actually, func mouseDown(with event: UIEvent) doesn't exist. iOS Apps deal with touches, not mouse clicks, obviously

(3) I've got touches covered with override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {...}

(4) BUT, I am hung up with how do I see if my code works with touches within Xcode?

(5) Folk have their mouse linked to their iPad via Bluetooth ?

  • Silly ole me .. just activate touch-simulation via the settings Panel of the Simulator. My very bad!

Add a Comment