gpt4 book ai didi

swift - 如何在 xcode 中多次运行函数?

转载 作者:行者123 更新时间:2023-11-28 07:52:23 24 4
gpt4 key购买 nike

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

如果有一个更新函数,我会像这样创建一个 bool 值:

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()
}

}
}

最佳答案

执行此操作的一种方法是使用回调,这是一种将创建新类(场景)的类作为参数发送到该新类(场景)的方法。这样新类(场景)就可以调用旧类的函数。下面是如何在您的代码中实现它。首先,我制作了一个自定义初始化程序,它将创建类作为参数并将其分配给局部变量 (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
}
}

关于swift - 如何在 xcode 中多次运行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49341039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com