1

我的暂停菜单的代码我如何将一个承认插页式广告集成到 swift 3 中。它是一个虚拟按钮。这是我的暂停屏幕的样子:import SpriteKit

让 BUTTON_DISTANCE: CGFloat = 15 让 Background_COLOR = UIColor(red: 255, green: 255, blue: 255, alpha: 0.2)

类 PauseMenu { 让 resumeButton: 按钮让 resetButton: 按钮让 menuButton: 按钮让根: SKNode = SKNode() 让 gameDelegate: GameDelegate 让背景: SKSpriteNode

init(scene: SKScene, delegate: GameDelegate) {
    self.gameDelegate = delegate
    root.position = CGPoint(x: scene.frame.midX, y: scene.frame.midY)
    root.zPosition = 5

    resumeButton = Button(scene: scene, parent: root, text: "Resume",
                          x: 0, y: DEFAULT_BUTTON_SIZE.height + BUTTON_DISTANCE,
                          action: gameDelegate.gameResumed)
    resetButton = Button(scene: scene, parent: root, text: "Reset",
                          x: 0, y: 0, action: gameDelegate.gameReseted)
    menuButton = Button(scene: scene, parent: root, text: "Menu",
                          x: 0, y: -(DEFAULT_BUTTON_SIZE.height + BUTTON_DISTANCE),
                          action: gameDelegate.returnMenu)
    background = SKSpriteNode()
    background.size = scene.size
    background.position = CGPoint(x: 0, y: 0)
    background.color = BACKGROUND_COLOR

    root.addChild(background)

    hide()
    scene.addChild(root)
}

func touch(_ touch: UITouch) {
    resumeButton.press(touch)

    resetButton.press(touch)
    menuButton.press(touch)
}

func release(_ touch: UITouch) {
    resumeButton.release(touch)
    resetButton.release(touch)
    menuButton.release(touch)
}

func hide() {
    root.isHidden = true
}

func show() {
    root.isHidden = false
}

}

我希望在您单击休息按钮时显示广告。-Thx Zain

4

1 回答 1

0

因此,您要做的就是在GameViewController您按下其中一个场景中的按钮时从您的功能(显示广告)中运行。为此,您需要执行以下操作:

在您的 GameViewController 中,设置一个通知观察者viewWillLayoutSubviews并设置显示广告的函数:

override func viewWillLayoutSubviews() {

    NotificationCenter.default.addObserver(self, selector: #selector(self.showAd), name: NSNotification.Name(rawValue: "showAd"), object: nil)

}

func showAd() {

    //Code to show the interstitial ad

}

然后在你的场景中,当你想要GameViewController运行函数时调用它:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showAd"), object: nil)

您可以将此代码放在场景中以在按下按钮时运行。

于 2017-01-24T17:57:53.487 回答