I just finished my first game and I'm trying this code on my viewController to integrate an ad banner :
import iAd
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var skView = self.originalContentView as SKView
var scene:SKScene = GameScene(size: skView.bounds.size)
skView.showsFPS = false
skView.showsNodeCount = false
skView.showsPhysics = false
let turbulence = SKFieldNode.turbulenceFieldWithSmoothness(0.1, animationSpeed: 0.1)
scene.addChild(turbulence)
let noise = SKFieldNode.turbulenceFieldWithSmoothness(0.4, animationSpeed: 0.1)
scene.addChild(noise)
skView.presentScene(scene)
self.canDisplayBannerAds = true
}
On my firstViewController (the game presentation) it works but not on my gameViewController and I can't understand why.
Someone can help me ?
Thank you all !!!
Related
Apple's documentation states I can't change the position using the position getter/setter. I've tried the anchor point suggestion but still unable to move my scene's position in Scene Editor to the top. Does anyone have any ideas?
Implementation has been done via Scene Editor, currently the only code submitted to help scene present is below:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities
sceneNode.graphs = scene.graphs
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFit
print(sceneNode.anchorPoint)
print(sceneNode.position)
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
In AppDelegate bypass storyboard.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = GameViewController()
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
In GameViewController
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(skView)
setupSKView()
}
let skView: SKView = {
let view = SKView()
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .fill
view.presentScene(scene)
}
return view
}()
func setupSKView(){
skView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height / 2)
}
}
I try to make a view controller, where i.e top half of the screen is SKScene, and bottom half of the screen is normal scene, I mean with buttons etc. Is this possible? I created a Single View application, and I am adding some code snippets below, which is the code from viewcontroller.swift file. But it doesn't create boxes to the tapped locations and also it doesn't move as expected a free fall motion when tapped. Maybe is there an issue about that I created a Single view application rather than choosing Game application when first creating the project? Maybe Metal enable/disable needed? I am not professional, therefore just guesses.
import UIKit
import SwiftUI
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let box = SKSpriteNode(color: .red, size: CGSize(width: 0.04, height: 0.04))
box.physicsBody = SKPhysicsBody(rectangleOf: box.frame.size)
box.position = location
addChild(box)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
scene.backgroundColor = .clear
let transition = SKTransition.fade(withDuration: 1)
view.presentScene(scene, transition: transition)
}
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
view.allowsTransparency = true
view.backgroundColor = UIColor.clear
}
}
}
I tried to add a subview to my GameViewController (because I want to add a banner ad at the bottom). I created it as follows:
if let view = self.view as! SKView? {
let subView = SKView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 415, height: 685)))
view.addSubview(subView)
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = SKSceneScaleMode.aspectFit
subView.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
view.showsDrawCount = false
addBannerViewToView(bannerView)
}
}
This works initially - it presents my GameScene. But later, I ask my GameScene to present another subclass of SKScene that I created, which I call Shop, using the following code:
func initializeShop() {
let shop = Shop(size: self.size)
let transition = SKTransition.fade(withDuration: 0.1)
self.view?.presentScene(shop!, transition: transition)
}
And it simply does not present. It remains on the current view.
This works completely fine if I do NOT add a subview to the initial view in the GameViewController, i.e.
if let view = self.view as! SKView? {
if let scene = SKScene(fileNamed: "GameScene") {
scene.scaleMode = SKSceneScaleMode.aspectFit
view.presentScene(scene)
view.ignoresSiblingOrder = true
view.showsFPS = false
view.showsNodeCount = false
view.showsDrawCount = false
}
}
So it seems like there is some property of the GameViewController's default SKView that allows presentation, but not a subview. But I can't find any documentation that says that - from what I can tell, all SKViews should be able to present SKScenes.
Any help would be much appreciated.
I have made my Viewcontroller to a SKview so I can add things.
let skview: SKView = {
let view = SKView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
self.view.addSubview(skview)
skview.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
skview.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
skview.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
skview.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
self.addchild(player) doesn't work.
You can't add a sprite to a view controller. You can't neither add a sprite to a SKView. You must first create a SKScene and present it:
let scene = SKScene(size: skview.bounds.size)
skview.presentScene(scene)
Then you can add sprites to the scene:
scene.addChild(player)
I am working on a game for macOS using Spritekit. Want to use a second scene. How do I set up App Delegate and ViewController to accomplish that?
I can make a transition to a second scene
var secondScene = SecondScene(size: self.size)
var transition = SKTransition.flipVertical(withDuration: 1.0)
secondScene.scaleMode = SKSceneScaleMode.aspectFill
self.scene!.view?.presentScene(secondScene, transition: transition)
Similar code in the SecondScene to return results in a crash during reloading of the previous scene, which loads fine the first time around.
self.scene!.view?.presentScene(FirstScene, transition: transition)
This is what my ViewController looks like.
class ViewController: NSViewController {
#IBOutlet var skView: SKView!
#IBOutlet weak var msg: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.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
}
}
}