类型 'GameScene' 没有成员 'handleTap(tapGesture:)'

Type 'GameScene' has no member 'handleTap(tapGesture:)'

我一直在使用 XCode(版本 9.3.1)开发应用程序,并使用编码 iPhone 儿童应用程序 (https://nostarch.com/iphoneappsforkids/) 作为学习指南如何创建游戏,例如从第 14 章开始的滑板游戏。

我已经按照代码进行操作,但是 运行 使用 handleTap(tapGesture:) 时出错。

      override func didMove(to view: SKView) {
    physicsWorld.gravity = CGVector(dx: 0.0, dy: -6.0)
    physicsWorld.contactDelegate = self

    anchorPoint = CGPoint.zero

    let background = SKSpriteNode(imageNamed: "background")
    let xMid = frame.midX
    let yMid = frame.midY
    background.position = CGPoint(x: xMid, y: yMid)
    addChild(background)

    setupLabels()

    // Set up the player and add her to the scene
    player.setupPhysicsBody()
    addChild(player)

    // Add a tap gesture recognizer to know when the user tapped the screen
    let tapMethod = #selector(GameScene.handleTap(tapGesture:))
    let tapGesture = UITapGestureRecognizer(target: self, action: tapMethod)
    view.addGestureRecognizer(tapGesture)

    // Add a menu overlay with "Tap to play" text
    let menuBackgroundColor = UIColor.black.withAlphaComponent(0.4)
    let menuLayer = MenuLayer(color: menuBackgroundColor, size: frame.size)
    menuLayer.anchorPoint = CGPoint(x: 0.0, y: 0.0)
    menuLayer.position = CGPoint(x: 0.0, y: 0.0)
    menuLayer.zPosition = 30
    menuLayer.name = "menuLayer"
    menuLayer.display(message: "Tap to play", score: nil)
    addChild(menuLayer)
}

它给出了

中的错误
    let tapMethod = #selector(GameScene.handleTap(tapGesture:))

但是,再往下,我有这段代码。

     func handleTap(tapGesture: UITapGestureRecognizer) {

        if gameState == .running {

            // Make the player jump if player taps while she is on the ground
            if player.isOnGround {

                player.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy: 260.0))

                run(SKAction.playSoundFileNamed("jump.wav", waitForCompletion: false))
            }
        }
        else {

            // If the game is not running, tapping starts a new game
            if let menuLayer: SKSpriteNode = childNode(withName: "menuLayer") as? SKSpriteNode {

                menuLayer.removeFromParent()
            }

            startGame()
        }
    }

虽然我在update 方法下有handleTap,错误不会自行消除。

如果您对标题中的错误代码有解决方案,请告诉我。

将选择器和签名更改为此

let tapMethod = #selector(GameScene.handleTap(_:))

//

@objc func handleTap(_ tapGesture: UITapGestureRecognizer) {}

根据docs,

In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and can be constructed using the #selector expression. To create a selector for a method that can be called from Objective-C, pass the name of the method

所以我们必须通过将成员标记为 @objc

来将其暴露给 obj-c
@objc func handleTap(_ tapGesture: UITapGestureRecognizer) {}

更多信息请参考: