1
votes

code of my pause menuHow can i integrate an admit interstitial Ad into swift 3. It is a virtual button. This is a what my pause screen looks like:import SpriteKit

let BUTTON_DISTANCE: CGFloat = 15 let BACKGROUND_COLOR = UIColor(red: 255, green: 255, blue: 255, alpha: 0.2)

class PauseMenu { let resumeButton: Button let resetButton: Button let menuButton: Button let root: SKNode = SKNode() let gameDelegate: GameDelegate let background: 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
}

}

I want the ad to be displayed when you click the rest button. -Thx Zain

1
Can you please clarify what you're asking forNik
I want the interstitial ad to be played when someone clicks "reset button"user6794475
Take a look at this: stackoverflow.com/a/40164206/6728196 I can make a specific answer for you if you wantNik
@Nik I'm new to swift could you by any chance tell me how to do it with the reset buttonuser6794475

1 Answers

0
votes

So what you're trying to do is run a function (that shows the ad) from your GameViewController whenever a button in one of your scenes is pressed. To do this, you'll need to do the following:

In your GameViewController, setup a notification observer in viewWillLayoutSubviews and setup the function that would show the ad:

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

}

Then in your scene, call this when you want the function in GameViewController to be run:

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

You would put this code in the scene to run when the button is pressed.