输入' GameScene'没有会员' handleTap(tapGesture :)'

时间:2018-05-26 01:48:11

标签: ios swift uitapgesturerecognizer

我一直在使用XCode的应用程序(版本9.3.1)并使用Coding iPhone Apps for Kids(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()
        }
    }

虽然我在更新方法下有handleTap,但错误不会自行删除。

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

2 个答案:

答案 0 :(得分:0)

更改选择器&签名到这个

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

//

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

答案 1 :(得分:0)

根据docs

  

在Objective-C中,选择器是一种引用名称的类型   Objective-C方法。在Swift中,Objective-C选择器由   Selector结构,可以使用#selector构造   表达。为可以从中调用的方法创建选择器   Objective-C,传递方法的名称

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

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

有关更多信息,请参阅:How can I deal with @objc inference deprecation with #selector() in Swift 4?