So, I've found two ways of displaying google ads in my SpriteKit game... Both ways work, but I'm concerned that one of them is a more efficient etc.
My game just has 1 ViewController and 1 GameScene. The difference from the two implementations are when and where they are created.
Both require:
import GoogleMobileAds
var googleBannerView: GADBannerView!
This was the way I implemented it first:
in: GameViewController.swift
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
googleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
googleBannerView.rootViewController = self
let request: GADRequest = GADRequest()
googleBannerView.loadRequest(request)
googleBannerView.frame = CGRectMake(0, view.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)
self.view.addSubview(googleBannerView!)
if let skView = self.view as? SKView { //cast root as SKView
if skView.scene == nil {
//create and present gameScene
}
}
}
This is the second way:
in: GameScene.swift
override func didMoveToView(view: SKView) {
googleBannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
googleBannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
googleBannerView.rootViewController = self.view?.window?.rootViewController
let request: GADRequest = GADRequest()
googleBannerView.loadRequest(request)
googleBannerView.frame = CGRectMake(0, view.bounds.height - googleBannerView.frame.size.height, googleBannerView.frame.size.width, googleBannerView.frame.size.height)
self.view?.addSubview(googleBannerView!)
addChild(worldNode) //make whole world a node, create game nodes, in worldNode
switchToMainMenu() //go to menu menu
}
I think the only real difference is how you set the rootViewController, and given my game only has the 1 ViewController, both work...
Which would be the better implementation?
You should definitely be doing this via the view controller. This makes more sense from a design standpoint. The GameScene's job is to worry about game content only. The view controller's job is to manage and control views (Hence the name "View Controller.")
Related
I am trying to use a SKView in my login UI for some animations. The SKView is added as a subview to a LoginContainerView as well as the view containing the UITextFields and the sign in button. All of the subviews are positioned with autolayout and the FireScene attached to the SKView has it's scaleMode set to .resizeFill.
The issue I'm having is very clear to observe from this clip: https://streamable.com/5it17 .
Clip explanation: Whenever I double tap any of the UITextFields (to trigger the
UIMenuController / Paste button) the FireScene freezes for a brief
amount of time (yet it's very noticeable) and the FPS drops down to around 30 for 1-2 seconds.
The only node that the FireScene has added as its child node is a modified fireflies SKEmitterNode template. I'm fairly certain that this has nothing to do with the actual code (as it is very straightforward and concise) and is probably a UIKit mixed with SpriteKit bug.
Relevant code:
class FireScene: SKScene {
// MARK: - Properties
var fireSparkEmitter: SKEmitterNode? = nil
// MARK: - Inherited
override func didChangeSize(_ oldSize: CGSize) {
// position the emitter to scene's center
fireSparkEmitter?.position = CGPoint(x: size.width/2, y: size.height/2)
}
override func didMove(to view: SKView) {
if let emitter = fireSparkEmitter {
emitter.resetSimulation()
} else {
guard let emitter = SKEmitterNode(fileNamed: "FireSparks.sks") else { return }
addChild(emitter)
fireSparkEmitter = emitter
}
}
}
class LoginContainerView: UIView {
/// ...
let fireBackgroundScene: FireScene = {
let scene = FireScene(size: .zero)
scene.scaleMode = .resizeFill
return scene
}()
/// ...
// MARK: - Inherited
// called after view initialization
func setupSubviews() {
let skView = SKView(frame: .zero)
skView.translatesAutoresizingMaskIntoConstraints = false
addSubview(skView)
skView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
skView.topAnchor.constraint(equalTo: topAnchor).isActive = true
skView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
skView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
skView.showsFPS = true
skView.presentScene(fireBackgroundScene)
}
}
PS: I've tried to profile the app with Time Profiler and it seems that the bottleneck is the UITextField gesture handling by UIKit itself.
Your issue seems related to a different layout syncronization between SKView and the other UIKit objects.
SKView have a property called isAsynchronous (default = true) that indicates
whether the content is rendered asynchronously.
You can try to set:
skView.isAsynchronous = false
to syncronize your SKView with the core animation updates.
This could be not enough because you'll syncronize SKView with the Core Animations of your project but there are always the UIKit-style animations that could be out of sync.
If this property don't solve your issue you can change also the preferredFramesPerSecond property. Remember that when you try to mix two frameworks (sprite-kit and UIKit) you will inevitably incur to unpleasant hitches.
I have created a UI elements on main.storyboard which i require to be hidden until the game is over and the once the player tap the screen to dismiss. Main.storyboard is linked to GameViewController therefor all my IBOutlets and IBActions are in there and all my game code is in GameScene. How can i link the view controller to the scene for that the popup image and buttons only appear when it is game over. Would greatly appreciate some help, I have been stuck on this for quite some time now.
This seems to be quite a common problem people have with SpriteKit games so lets go through the difference between SpriteKit games and UIKit apps.
When you make a regular UIKit app, e.g. YouTube, Facebook, you would use ViewControllers, CollectionViews, Views etc for each screen/menu that you see (Home screen, Channel screen, Subscription channel screen etc). So you would use UIKit APIs for this such as UIButtons, UIImageViews, UILabels, UIViews, UICollectionViews etc. To do this visually we would use storyboards.
In SpriteKit games on the other hand it works differently. You work with SKScenes for each screen that you see (MenuScene, SettingsScene, GameScene, GameOverScene etc) and only have 1 ViewController (GameViewController). That GameViewController, which has a SKView in it, will present all your SKScenes.
So we should add our UI directly in the relevant SKScenes using SpriteKit APIs such as SKLabelNodes, SKSpriteNodes, SKNodes etc. To do this visually we would use the SpriteKit scene level editor and not storyboards.
So the general logic would be to load your 1st SKScene as usual from the GameViewController and than do the rest from within the relevant SKScenes. Your GameViewController should basically have next to no code in it beyond the default code. You can also transition from 1 scene to another scene very easily (GameScene -> GameOverScene).
If you use GameViewController for your UI it will get messy really quickly if you have multiple SKScenes because UI will be added to GameViewController and therefore all SKScenes. So you would have to remove/show UI when you transition between scenes and it would be madness.
To add a label in SpriteKit it would be something like this
class GameScene: SKScene {
lazy var scoreLabel: SKLabelNode = {
let label = SKLabelNode(fontNamed: "HelveticaNeue")
label.text = "SomeText"
label.fontSize = 22
label.fontColor = .yellow
label.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return label
}()
override func didMove(to view: SKView) {
addChild(scoreLabel)
}
}
To make buttons you essentially create a SKSpriteNode and give it a name and then look for it in touchesBegan or touchesEnded and run an SKAction on it for animation and some code after.
enum ButtonName: String {
case play
case share
}
class GameScene: SKScene {
lazy var shareButton: SKSpriteNode = {
let button = SKSpriteNode(imageNamed: "ShareButton")
button.name = ButtonName.share.rawValue
button.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
return button
}()
override func didMove(to view: SKView) {
addChild(shareButton)
}
/// Touches began
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node = atPoint(location)
if let nodeName = node.name {
switch nodeName {
case ButtonName.play.rawValue:
// run some SKAction animation and some code
case ButtonName.share.rawValue:
let action1 = SKAction.scale(to: 0.9, duration: 0.2)
let action2 = SKAction.scale(to: 1, duration: 0.2)
let action3 = SKAction.run { [weak self] in
self?.openShareMenu(value: "\(self!.score)", image: nil) // image is nil in this example, if you use a image just create a UIImage and pass it into the method
}
let sequence = SKAction.sequence([action1, action2, action3])
node.run(sequence)
default:
break
}
}
}
}
}
To make this even easier I would create a button helper class, for a simple example have a look at this
https://nathandemick.com/2014/09/buttons-sprite-kit-using-swift/
You can also check out Apple's sample game DemoBots for a more feature rich example.
This way you can have things such as animations etc in the helper class and don't have to repeat code for each button.
For sharing, I would actually use UIActivityController instead of those older Social APIs which might become deprecated soon. This also allows you to share to multiple services via 1 UI and you will also only need 1 share button in your app. It could be a simple function like this in the SKScene you are calling it from.
func openShareMenu(value: String, image: UIImage?) {
guard let view = view else { return }
// Activity items
var activityItems = [AnyObject]()
// Text
let text = "Can you beat my score " + value
activityItems.append(text as AnyObject)
// Add image if valid
if let image = image {
activityItems.append(image)
}
// Activity controller
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
// iPad settings
if Device.isPad {
activityController.popoverPresentationController?.sourceView = view
activityController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
activityController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
}
// Excluded activity types
activityController.excludedActivityTypes = [
UIActivityType.airDrop,
UIActivityType.print,
UIActivityType.assignToContact,
UIActivityType.addToReadingList,
]
// Present
view.window?.rootViewController?.present(activityController, animated: true)
}
and then call it like so when the correct button was pressed (see above example)
openShareMenu(value: "\(self.score)", image: SOMEUIIMAGE)
Hope this helps
create reference of GameViewController in GameScene class like this way
class GameScene: SKScene, SKPhysicsContactDelegate {
var referenceOfGameViewController : GameViewController!
}
in GameViewController pass the reference like this way
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
scene.referenceOfGameViewController = self
// Present the scene
view.presentScene(scene)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
by using this line you can pass reference to GameScene class
scene.referenceOfGameViewController = self
Now In GameScene class you can access all the variable of GameViewController like this way
referenceOfGameViewController.fbButton.hidden = false
referenceOfGameViewController.gameOverPopUP.hidden = false
I'm making an a game in XCode 7 using Swift 2. I have a variable that I want to pass from the start screen (which is an UIViewController) to the game scene (which is an SKScene). I want the player to select a character in a UIView and play with it in the SKScene. I also want the score that's in the SKScene to show in the game-over screen that's an UIView. I've seen tutorials for passing data between two UIViewControllers and between two SKScenes, but none of them work for this case.
How can I pass a variable from an UIViewController to a SKScene (and vice versa)?
I ran over this same problem yesterday.
Swift 5.2, xcode 12 and targeting iOS 14. Searched high and low. Eventually found the userData attribute of the SKScene.
Note: The app is based on SwiftUI and the SKScene is inside a UIViewRepresentable:
struct SpriteKitContainer: UIViewRepresentable {
typealias UIViewType = SKView
var skScene: SKScene!
#Binding var timerData : ModelProgressTimer
Not that I am passing a binding into the View - this contains a number of data items I wanted to make available to the scene.
I placed code in two places to populate skScene.userData:
func makeUIView(context: Context) -> SKView {
self.skScene.scaleMode = .resizeFill
self.skScene.backgroundColor = .green
debugPrint("setting user data")
self.skScene.userData = [:]
self.skScene.userData!["color"] = UIColor(timerData.flatColorTwo!)
self.skScene.userData!["running"] = timerData.isRunning
self.skScene.userData!["percent"] = timerData.percentComplete
let view = SKView(frame: .zero)
view.preferredFramesPerSecond = 60
// view.showsFPS = true
// view.showsNodeCount = true
view.backgroundColor = .clear
view.allowsTransparency = true
return view
}
func updateUIView(_ view: SKView, context: Context) {
self.skScene.userData = [:]
self.skScene.userData!["running"] = timerData.isRunning
self.skScene.userData!["color"] = UIColor(timerData.flatColorTwo!)
self.skScene.userData!["percent"] = timerData.percentComplete
view.presentScene(context.coordinator.scene)
}
Then, inside the skScene object, I am able to retrieve the userData values:
var item: SKShapeNode! = nil
override func sceneDidLoad() {
let scaleUp = SKAction.scale(by: 2.0, duration: 1.0)
let scaleDown = SKAction.scale(by: 0.5, duration: 1.0)
scaleUp.timingMode = .easeInEaseOut
scaleDown.timingMode = .easeInEaseOut
let sequence = SKAction.sequence([scaleUp,scaleDown])
actionRepeat = SKAction.repeatForever(sequence)
item = SKShapeNode(circleOfRadius: radius)
addChild(item)
}
override func didChangeSize(_ oldSize: CGSize) {
item.fillColor = self.userData!["color"] as! UIColor
item.strokeColor = .clear
let meRunning = self.userData!["running"] as! Bool
if meRunning {
item.run(actionRepeat)
} else {
item.removeAllActions()
}
}
This draws a circle that "pulses" if the "running" boolean is set in the userdata (a Bool value).
If you haven't already found the answer, I did find a way to do this. I was doing something very similar.
In my case I have a UIView that gets called as a pause menu from my scene. I have made the class and variable names a little more ambiguous so they can apply to your situation as well.
class MyView: UIView {
var scene: MyScene!
var button: UIButton!
init(frame: CGRect, scene: MyScene){
super.init(frame: frame)
self.scene = scene
//initialize the button
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))
}
func buttonTapped(){
self.removeFromSuperview() // this removes my menu from the scene
scene.doTheThing() // this calls the method on my scene
}
Here are the relevant parts of my scene.
class MyScene: SKScene {
func doTheThing(){
//this is a function on the scene. You can pass any variable you want through the function.
}
}
In your situation, it sounds like the first screen is a UIView and the second screen is the SKScene.
You may want to make your SKScene first, pause it, and then add the UIView in front of the Scene. Once the character is selected, you can remove the UIView and add your character into the scene.
I hope that this answers your question. If it doesn't let me know.
From the level select scene:
let scene = GameScene(fileNamed:"GameScene")
scene?.userData = [:]
scene?.userData!["level"] = 21
self.view?.presentScene(scene!)
From within the game scene:
let level = self.userData!["level"] as! Int
I've been looking all over for an answer to this and I've found lots of examples in objective C (google developer docs, etc.) and some answers in swift, but not using spritekit, and being a novice, I just haven't been able to bridge the gaps in these tutorials to put it all together for my project. Honestly, I don't really care if the ad is called on app launch, or game over, I'd just be happy being able to call the ad period, though I guess on game launch is preferred.
Pretty sure I've got it all set up correctly in gameviewcontroller.swift, but I don't know how to actually call it. Here is the code from my gameviewcontroller.swift:
import UIKit
import SpriteKit
import GoogleMobileAds
class GameViewController: UIViewController {
var interstitial : GADInterstitial!
func createAndLoadAd() -> GADInterstitial {
var ad = GADInterstitial()
ad.adUnitID = "ca-app-pub-4471013071748494/2980967718"
var request = GADRequest()
request.testDevices = [""]
return ad
}
Beyond this ... I also have this if statement to call the ad, but I don't know where it should be ... or if it's complete (in the tutorial, it was in a button, but I need it to be automatic, naturally:
if (self.interstitial.isReady) {
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadAd()
}
Anyone have any experience with this? If anyone could help me complete this by letting me know where to place that above if statement, or if there is more that needs to be done ... your help will be much appreciated. Thank you.
Ok so I started a new project (Swift project) which is Game Project
In GameViewController:
class GameViewController: UIViewController
{
var interstitial = GADInterstitial()
override func viewDidLoad()
{
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene
{
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(5 * Double(NSEC_PER_SEC)))
//Creation of the ad and its request
self.interstitial = GADInterstitial()
self.interstitial.adUnitID = "ca-app-pub-4798156420380453/7714267723"
var request = GADRequest()
// request.testDevices = [""]
self.interstitial.loadRequest(GADRequest());//The method u were missing
dispatch_after(delayTime, dispatch_get_main_queue())
{
self.showAd()
}
}
}
func showAd()
{
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)//Whatever shows the ad
}
}
It is my first swift code so don't judge me if I did something wrong
But when I lunch it after ±5 seconds i see the ad
Here is your example.
I am not familiar with Swift at all ,
but all you need to do is to get the interstitial delegate when it dismissed -> and request for the next interstitial, So in next time you will get the new AD
Feel free to ask anything !
P.S. Don't forget to change the admob ID to your ID.
My solution based on above comments was the following:
1) Add this in the GameViewController.swift
protocol AdmobInterstitialDelegate{
func showInterstitial()
}
...
// add this delegate in GameViewController definition
class GameViewController: UIViewController, GADInterstitialDelegate, AdmobInterstitialDelegate {
...
// and assign it to the GameScene class when you create its instance
let scene = GameScene()
scene.adDelegate = self
// do not forget to implement the delegated method itself
func showInterstitial(){
if (interstitial!.isReady) {
interstitial!.presentFromRootViewController(self)
}
}
and then you can use it in GameScene
class GameScene: SKScene {
....
var adDelegate: AdmobInterstitialDelegate?
....
// and finally - show the ad
override func didMoveToView(view: SKView) {
self.adDelegate?.showInterstitial();
....
hope this helps.
keep the if condition in viewDidAppear it will automatically execute
override func viewDidAppear(animated: Bool) {
if (interstitial.isReady) {
interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadInterstitial()
}
I am developing a game with 3 different SKScenes(scene1, scene2, scene3). In GameViewController i initialize all of them like this:
class GameViewController: UIViewController {
var hero = Hero()
var skView: SKView!
var scene1: SKScene!
var scene2: SKScene!
var scene3: SKScene!
override func viewDidLoad() {
super.viewDidLoad()
// init scenes
scene1 = SKScene(size: view.bounds.size, hero: hero)
scene2 = SKScene(size: view.bounds.size, hero: hero)
scene3 = SKScene(size: view.bounds.size, hero: hero)
scene1.scaleMode = .AspectFill
scene2.scaleMode = .AspectFill
scene3.scaleMode = .AspectFill
// set view
skView = self.view as SKView
// present the first scene
skView.presentScene(scene1)
}
My idea is to present the first scene at first and present(swith to) the other scene later(i.e. when hero is stronger). In each scene the hero sprite was added like this:
func addHero() {
let heroSprite = SKSpriteNode(imageNamed: "hero")
hero.sprite = heroSprite
heroSprite.position = CGPoint(x: size.width/4, y: size.height/2)
addChild(heroSprite)
}
And in update method, hero position is updated by touching.
func update() {
if touching {
hero.position++
}
}
Hero class looks like this:
class Hero {
var sprite: SKSpriteNode?
}
The problem is:
The hero is movable by touching when only the first scene(scene1) is initialized. Which means, the hero is not movable any more with code above.
Can anyone give me some advice what have i done wrong? Thanks in advance!
PS: The complete codes can be found in Github.
The issue is solved by another related question: Sprites must be cleaned before switch scenes with SpriteKit and Swift?
By switching scenes, be sure not to add the sprites twice by checking the init status. For example in "didMoveToView", "init" or "setup" method:
if isInit {
do nothing
} else {
add sprites
isInit = true
}