AdMob doesnt show up in my SpriteKit game - ios

I tried to implement a banner with AdMob into my SpriteKit Game. I added a banner in the GameViewController.swift just like shown in the code underneath, but the banner doesn't show up in my simulator.
What am I missing? I am using Swift 4.
class GameViewController: UIViewController {
var banner:GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// 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
banner = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
banner.adUnitID = "here is my unit ID -> copy paste so cant be wrong"
banner.rootViewController = self
let req:GADRequest = GADRequest()
banner.load(req)
banner.frame = CGRect(x: 0, y: view.bounds.height - banner.frame.size.height, width: banner.frame.size.width, height: banner.frame.size.height)
view.addSubview(banner)
}
}

I think you forget to declare your ViewController as it conforms to GADBannerViewDelegate
class GameViewController: UIViewController, GADBannerViewDelegate {
can you try again after conforming to the protocol?

Related

Swift: Load the SKScene from GameMenu.sks doesn't work

i have the problem, that the GameViewController doesn't load my GameMenu. If i simulate my app it only shows a grey screen with node:0 and the fps count.
Here the Code from GameViewController.swift:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = GameMenu(fileNamed: "GameMenu") {
// 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
}
}
I tried also the line:
if let scene = SKScene(fileNamed: "GameMenu")
but it changed nothing.
I also added the Custom Class "GameMenu" to the GameMenu.sks
I think the problem is this if let scene = SKScene(fileNamed: "GameMenu") line.
I have read a lot about this problem but i my case nothing was successful.
I also tried this:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let gkScene = GKScene(fileNamed: "GameMenu.sks") {
if let skScene = gkScene.rootNode as? SKScene {
// Set the scale mode to scale to fit the window
skScene.scaleMode = .aspectFill
// Present the scene
view.presentScene(skScene)
}
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
but nothing happend.
You have to load it as GKScene from .sks file and get SKScene from its rootNode attribute.
if let gkScene = GKScene(fileNamed: "GameMenu") {
if let skScene = gkScene.rootNode as? SKScene {
// TODO
}
}

Cannot make app go to main menu (Swift 4 SpriteKit)

I am trying to direct the user to a main menu file as soon my game loads (made with Swift 4 SpriteKit). However, when I try to redirect the user to this .swift file (where a label should be displayed onto the users screen) the screen is simply blank except the for the node and FPS counter.
Here's my code in the MainMenuScene.swift file I created:
import Foundation
import SpriteKit
class MainMenu: SKScene {
override func didMove(to view: SKView) {
print("In scene")
let myLabel = SKLabelNode(fontNamed: "The Bold Font")
myLabel.fontColor = SKColor.blue
myLabel.text = "My Label"
myLabel.fontSize = 50
myLabel.position = CGPoint(x: 0, y: 0)
myLabel.zPosition = 1
self.addChild(myLabel)
}
}
Here's my code in the GameViewController.swift
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "MainMenuScene") {
// 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
view.showsPhysics = true
}
}
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
}
}
I changed the
if let scene = SKScene(fileNamed: "GameScene")
to
if let scene = SKScene(fileNamed: "MainMenuScene")
That line of code should redirect the user to MainMenuScene.swift, but when I run the program the screen is simply blank.
Anyone know what im doing wrong?
Thanks!
Try using the code below after
super.viewDidLoad()
Also make sure that your Menu class is called "MainMenuScene" and that you changed the scene name to the same at MainMenuScene.sks
CODE:
if let scene = GKScene(fileNamed: "MainMenuScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! MainMenuScene? {
scene.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
//view.showsFPS = true
//view.showsNodeCount = true
}
}
}

Swift: How to get the size of a Spritekit Scene to present a SKNode fullscreen?

I am creating a simple Game and I would like to get the actually size (width/height) of a SpriteKit scene to be able to present a SKNode ( / SKSpriteNode) to fill the whole display, but apparently
backgroundNode.size = CGSize(width: view.frame.width, height: view.frame.height)
or anything similar doesn't work. The node is presented, but is just a quarter of the actually screen size.
I present the SKScene from GameViewController.swift like this:
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = GameScene(size: view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = true
}
So how could one get the actually SpriteKit Scene size or is there another way to present an SKNode / SKSpriteNode fullscreen in a GameScene?
Update: I tried this as well:
let displaySize: CGRect = UIScreen.main.bounds
let displayWidth = displaySize.width
let displayHeight = displaySize.height
which gives me for an iPhone 8 375x667 as a size, but the Node is still displayed as a quarter of the screen.
I created a sample project with the code below. This creates a red-colored node that fills the screen. You can use UIScreen.main.bounds and I'm using it here as a global variable. I find it helpful this way since I can access it from any code file, as I often need to use the screen size in a calculation.
GameViewController.swift
import SpriteKit
let displaySize: CGRect = UIScreen.main.bounds
class GameViewController: UIViewController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let skView = self.view as! SKView
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
scene.size = view.bounds.size
skView.ignoresSiblingOrder = true
skView.presentScene(scene)
}
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
let backgroundNode = SKSpriteNode()
override func didMove(to view: SKView) {
backgroundNode.size = CGSize(width: displaySize.width, height: displaySize.height)
backgroundNode.color = .red
self.addChild(backgroundNode)
}
}

SpriteKit zPosition Background

I want to make the background for the scene in GameScene.sks. I have Tile Map Node there, but when I set background in GameScene.swift the picture is on my textures. I want it on the back of the scene.
Here is the code of GameScene.swift:
import SpriteKit
class GameScene: SKScene {
var background = SKSpriteNode(imageNamed: "BackGround")`
override func didMove (to view: SKView) {
background.zPosition = 1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
addChild(background)
}
}
Here is the code of GameViewController.swift:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// 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
}
}
override var prefersStatusBarHidden: Bool {
return true
}
Does 1 the least z position in your scene? If so, try to set ignoresSiblingOrder property of your SKView to false. Also I would suggest you to read SKScene and SKNode documentations at least. And SpriteKit documentation in general.

AdMob Banner Ad not Showing Swift 3

I am working on a SpriteKit game and am implementing banner ads via AdMob (Google Firebase). I have followed Google's tutorials here and here on setting up your first ad request, but when I run the app on either the sim or my device (iPhone 6s), the banner does not show up. Per the tutorials, I implemented the banner view via the Storyboard and the rest was set up programmatically. Below is the code of my view controller.
Q: Why isn't my banner showing?
Follow-up Q: Is it possible to show the banner only on certain SKScenes?
import UIKit
import SpriteKit
import GoogleMobileAds
class GameViewController: UIViewController, GADBannerViewDelegate {
#IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-9474695450721030/1823667708"
bannerView.rootViewController = self
let req = GADRequest()
req.testDevices = ["91fbd46dff1179ce0a5e7226cea1ee0b", kGADSimulatorID]
req.tag(forChildDirectedTreatment: true)
bannerView.load(GADRequest())
view.addSubview(bannerView)
showBanner()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let skView = self.view as? SKView {
if skView.scene == nil {
let aspectRatio = view.bounds.size.height / view.bounds.size.width
let scene = MenuScene(size: CGSize(width: 750, height: 750 * aspectRatio))
scene.scaleMode = .aspectFill
skView.ignoresSiblingOrder = true
if kDebug {
skView.showsFPS = true
skView.showsDrawCount = true
skView.showsNodeCount = true
skView.showsPhysics = true
}
let transition = SKTransition.fade(with: SKColor.black, duration: 0.5)
skView.presentScene(scene, transition: transition)
}
}
}
func showBanner() {
bannerView.isHidden = false
let request = GADRequest()
request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
bannerView.load(request)
}
func hideBanner() {
bannerView.isHidden = true
}
}
First of all, you don't need to request a new banner every time you run showAd(). Simply unhide it. The ad you load on launch will remain for the whole lifecycle, and automatically refreshes.
Second, in your viewDidLoad code, you're initializing a banner ad, then loading a completely new one. Make sure that you're loading the same request that you were setting up.
As for calling functions inside your GameViewController from an SKScene, take a look at this: Call GameViewController function from SKScene
Just in case anyone is not seeing the ads after updating the SDK. From my end, I was able to resolve it after much investigation. It is worth noticing that the function name changes after updating the SDK.
The old function is
func adViewDidReceiveAd(_ bannerView: GADBannerView)
The new function is
func bannerViewDidReceiveAd(_ bannerView: GADBannerView)

Resources