前提・実現したいこと
swiftでspriteKitを使ったゲームを作っています。
AdMobのリワード動画を出したいのですが、呼び出し方が分かりません。
お願いします。
発生している問題・エラーメッセージ
GameViewControllerに書いたGADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)を他のswiftファイルのGameSceneで呼び出そうとするとエラーメッセージInstance member 'HINT' cannot be used on type 'GameViewController'; did you mean to use a value of this type instead?が出てしまい呼ぶことが出来ません。
該当のソースコード
GameViewController.swift import SpriteKit import GoogleMobileAds class GameViewController: UIViewController, GADRewardBasedVideoAdDelegate{ override func viewDidLoad() { super.viewDidLoad() GADRewardBasedVideoAd.sharedInstance().delegate = self GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313") if let view = self.view as! SKView? { // Load the SKScene from 'GameScene.sks' if let scene = SKScene(fileNamed: "GameTitle") { // Set the scale mode to scale to fit the window scene.scaleMode = .aspectFill // Present the scene view.presentScene(scene) } view.ignoresSiblingOrder = true view.showsFPS = true view.showsNodeCount = true } } func HINT() { if GADRewardBasedVideoAd.sharedInstance().isReady == true { GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self) } } func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) { print("Reward received with currency: (reward.type), amount (reward.amount).") } func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) { print("Reward based video ad is received.") } func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Opened reward based video ad.") } func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad started playing.") } func rewardBasedVideoAdDidCompletePlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad has completed.") } func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad is closed.") GADRewardBasedVideoAd.sharedInstance().load(GADRequest(), withAdUnitID: "ca-app-pub-3940256099942544/1712485313") } func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad will leave application.") } func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) { print("Reward based video ad failed to load.") } } AppDelegate.swift class AppDelegate: UIResponder, UIApplicationDelegate{ var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. GADMobileAds.sharedInstance().start(completionHandler: nil) return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } } GameScene.swift import AVFoundation import SpriteKit import UIKit import GoogleMobileAds class GameScene: SKScene { hint.position = CGPoint(x:240 , y: 500) hint.size = CGSize(width:75, height: 75) hint.zPosition = 1 self.addChild(hint) // タッチしたときの処理 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch: AnyObject in touches { // タッチされた位置にあるものを調べて let location = touch.location(in: self) let touchNode = self.atPoint(location) if touchNode == hint { GameViewController.HINT() }
試したこと
Main.storyboardを使ってGameViewControllerの画面にButtonを設置してHINTとコードを紐付けしたら動画を再生できたのですが、全画面にButtonが出てしまいます。storyboardを使わずに開発をしていたのでstoryboardは使わないようにしたいです。
補足情報(FW/ツールのバージョンなど)
Xcode11.5 Swift5.2.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。