【问题标题】:How to run function multiple times in xcode?如何在xcode中多次运行函数?
【发布时间】:2018-03-17 19:26:28
【问题描述】:

我想在玩家死亡后展示我的插页式广告。这意味着插页式广告将加载到我的游戏结束场景中。但我不能只在 viewdidload 部分写函数名。那么有没有办法让我在玩家进入 gameOverScene 时运行 interstitialAd 功能?

如果有更新功能,我会像这样创建一个布尔值:

var interstitialAdShow = false

然后在我的 didmovetoview 中写下这个:

interstitalAdShow = true

然后在我的更新函数中:

If interstitailAdShow == true{
     interstitial.present(fromRootViewController: self)
}

但是现在当 GameViewController 中没有更新功能,并且我无法在我的 gameOverScene.swift 中执行此操作时,我无法使用此解决方案,是否有另一种方法可以触发我的插页式广告功能gameOverScene 出现了。

顺便说一句,这是我的代码

import UIKit
import SpriteKit
import StoreKit
import GameplayKit
import GoogleMobileAds


var reklameNummer = 0


class GameViewController: UIViewController, GADBannerViewDelegate, GADRewardBasedVideoAdDelegate {


    var rewardBaseAd: GADRewardBasedVideoAd!


    var interstitial: GADInterstitial!

    @IBOutlet weak var bannerView: GADBannerView!

    override func viewDidLoad() {





        super.viewDidLoad()

        let request2 = GADRequest()
        request2.testDevices = [kGADSimulatorID]
        bannerView.delegate = self
        bannerView.adUnitID = "ca-app-pub-1110799225910030/5762940412"
        bannerView.rootViewController = self
        //bannerView.load(request2)


        interstitial = GADInterstitial(adUnitID: "ca-app-pub-1110799225910030/7460037600")
        let request = GADRequest()
        interstitial.load(request)









        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "MenuScene") {
                runTheInterStitialAd()
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the scene
                view.presentScene(scene)



                rewardBaseAd = GADRewardBasedVideoAd.sharedInstance()
                rewardBaseAd.delegate = self
                //rewardBaseAd.load(GADRequest(), withAdUnitID: "ca-app-pub-1110799225910030/4904503594")

            }



            view.ignoresSiblingOrder = true

            view.showsFPS = false
            view.showsNodeCount = false
        }
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.

    }

    override var prefersStatusBarHidden: Bool {
        return true
    }



//MARK:     Video ad
    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
                            didRewardUserWith reward: GADAdReward) {

    }
    func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) {
        //print.text?.append("Reward based video ad is received.\n")
    }
    func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        //print.text?.append("Opened reward based video ad.\n")
    }
    func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        //print.text?.append("Reward based video ad started playing.\n")
    }
    func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
    }
    func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        //print.text?.append("Reward based video ad will leave application.\n")
    }
    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
                            didFailToLoadWithError error: Error) {
        //print.text?.append("Reward based video ad failed to load.\n")
    }





//MARK:     Interstitial ad
    func runTheInterStitialAd(){
        var runFunc = SKAction.run(showInterstitialAdInScene)
        var wait = SKAction.wait(forDuration: 1)
        var sequence = SKAction.sequence([wait, runFunc])
    }
    func showInterstitialAdInScene() {
        print("this code is working")
        if var scene = SKScene(fileNamed: "Gameoverscene") {

            //  TRUE
            if (interstitial.isReady) == true{
                interstitial.present(fromRootViewController: self)
                gameNumber = 0
            }




            //  FALSE
            if (interstitial.isReady) == false{
                interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/1033173712")
                let request = GADRequest()
                interstitial.load(request)
            }
        }
    }






    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if rewardBaseAd.isReady{
            if reklameNummer == 1{
                reklameNummer += 1
                //rewardBaseAd.present(fromRootViewController: self)
                //rewardBaseAd.load(GADRequest(), withAdUnitID: "ca-app-pub-1110799225910030/4904503594")
            }
        }
        if rewardBaseAd.isReady{
            if reklameNummer == 2{
                reklameNummer = 0
                //rewardBaseAd.present(fromRootViewController: self)
                //rewardBaseAd.load(GADRequest(), withAdUnitID: "ca-app-pub-1110799225910030/4904503594")
            }
        }



        if gameNumber == 2{
            //showInterstitialAdInScene()
        }

    }
}

【问题讨论】:

    标签: swift xcode admob interstitial


    【解决方案1】:

    一种方法是使用回调,这是一种将创建新类(场景)的类作为参数发送到新类(场景)的方法。这样新类(场景)可以调用旧类的函数。下面是如何在您的代码中实现这一点。首先,我制作了一个自定义初始化程序,它将创建类作为参数并将其分配给局部变量(myCreator)。然后在玩家死亡时调用的函数中(deathFunction()),我从创建类中调用runIntersitialAd() 函数。我还更改了从GameViewController 设置MenuScene 的代码,以将其自身(GameViewController 类)作为MenuScene 初始化中的参数发送。

    将您的场景类更改为以下内容:

    class MenuScene {
        var myCreator : GameViewController!
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        convenience init(fileNamed: String, VCwhoCreatedMe: GameViewController) {
    
           self.init(fileNamed: fileNamed, VCwhoCreatedMe: VCwhoCreatedMe)
           myCreator = VCwhoCreatedMe
        } 
    
        ....your other code
    
        deathFunction() { //whatever code you use when the player dies
           myCreator.runTheInterStitialAd() //because of the callback construction this is now possible.
           ...other code
        }
    }
    

    在您的 GameViewController 中,函数 viewDidLoad():

    if let view = self.view as! SKView? {
    
       if let scene = MenuScene(fileNamed: "MenuScene",VCwhoCreatedMe: self ) {
           runTheInterStitialAd()
    
           scene.scaleMode = .aspectFill
    
    
           view.presentScene(scene)    
           ..... other code    
       }
    }
    

    【讨论】:

    • 嗨,Eric,很抱歉让您久等了。首先感谢您抽出时间帮助我完成我的项目。但是当我把它放在我的项目中时,似乎有些奇怪。关于 fileName: (super.init(fileNamed: fileNamed)) 的部分给了我和错误。 Xcode 说:“必须调用超类 'SKScene' 的指定初始化程序”
    • 我没有考虑到 SKScene.init(fileNamed:) 不是正常的初始化程序,所以这就是发生该错误的原因。为了解决这个问题,我更新了我的答案
    • 我又遇到了一些问题。第一个问题,是 gameViewController 或 menuscene.swift 文件中的第一部分代码。如果它在gameViewController里面呢?而且我也遇到了一些错误。第一个错误是,当我编写代码时:“myCreator.runTheInterStitialAd()”它给了我错误“'GameViewController' 类型的值没有成员'runTheInterStitialAd'”。第二个错误是当我编写代码“ required init?(coder aDecoder: NSCoder) {” 它给了我错误“'super' members cannot be referenced in a root class”
    • MenuScene 的代码应该在定义 MenuScene 的实际类中,而不是 GameViewController。我的意思是:如果您想构建一个游戏,您将需要链接到您使用拖放制作的视觉 SKScene 的代码,就像我假设的那样。该代码是实现 MenuScene 部分的地方。我很确定您已经拥有此代码的一部分或链接类,因为这是定义玩家何时“死亡”的唯一方法。通过解决这个问题,所有其他错误都会消失,因为它们与错误的实现有关。
    • 乐于助人:)
    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多