質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
SpriteKit

SpriteKitは、iOSやOS Xで使用できるApple社製の2Dゲーム開発フレームワークです。

AdMob

AdMobは、事前に指定した条件に従ってアプリに広告を表示するGoogleのサービス。開発者の向けのサービスで、広告を掲載することにより、収益を得ることが可能です。その他、見た目や雰囲気などアプリに合う広告に変更したり、広告表示の場所を指定することもできます。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1915閲覧

【Swift】AdMobのリワード動画を実装したい

mmr

総合スコア0

SpriteKit

SpriteKitは、iOSやOS Xで使用できるApple社製の2Dゲーム開発フレームワークです。

AdMob

AdMobは、事前に指定した条件に従ってアプリに広告を表示するGoogleのサービス。開発者の向けのサービスで、広告を掲載することにより、収益を得ることが可能です。その他、見た目や雰囲気などアプリに合う広告に変更したり、広告表示の場所を指定することもできます。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/06/06 15:16

編集2020/06/07 15:26

前提・実現したいこと

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

GameViewController.swift

let button:UIButton = UIButton(frame: CGRect(x:70, y: 390, width: 130, height: 50))

override func viewDidLoad() { super.viewDidLoad() button.setTitleColor(.white, for: .normal) button.addTarget(self, action: #selector(pushButton), for: .touchUpInside)

}

@objc func pushButton(sender: UIButton) {

if GADRewardBasedVideoAd.sharedInstance().isReady == true { button.isHidden = true GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self) } }

GameScene.swift

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch: AnyObject in touches { if touchNode == hint { self.view!.addSubview(button) button.isHidden = false }

GameViewControllerにコード上で透明なボタンを作り呼び出したい画面の時だけ呼び出すことでリワード動画を表示することが出来ました。

投稿2020/06/12 06:22

mmr

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問