How to display a Game Center leaderboard in SpriteKit? - swift

I'm pretty new to Swift and I'm having some trouble implementing a leaderboard into my game.
So far, I'm able to authenticate local player and set up leaderboard in iTunes Connect.
However, I'm unable to display the leaderboard itself, if I run the below code, it will abort when I click on GK Leaderboard button in SKScene. So, the question is how can I display the GK leaderboard from SKScene? Thanks!
GameViewController.swift
import SpriteKit
import GameKit
class GameViewController: UIViewController, GKGameCenterControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let scene = SceneMenu(size: view.frame.size)
scene.scaleMode = .aspectFill
scene.backgroundColor = .white
let view = view as! SKView
view.presentScene(scene)
view.showsFPS = true
view.showsNodeCount = true
authenticateLocalPlayer()
}
func authenticateLocalPlayer() {
GKLocalPlayer.local.authenticateHandler = { viewController, error in
}
}
func showLeaderboard() {
let gcVC = GKGameCenterViewController(leaderboardID: "com.generic.leaderboard", playerScope: .global, timeScope: .allTime)
gcVC.gameCenterDelegate = self
present(gcVC, animated: true)
}
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true)
}
}
SceneMenu.swift
import SpriteKit
import GameKit
class SceneMenu: SKScene {
override init(size: CGSize) {
super.init(size: size)
let btnGK = SKLabelNode(text: "GameKit")
btnGK.name = "btn_gk"
btnGK.fontSize = 20
btnGK.fontColor = SKColor.blue
btnGK.fontName = "Avenir"
btnGK.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(btnGK)
let btnLeaderboard = SKLabelNode(text: "GK Leaderboard")
btnLeaderboard.name = "btn_leaderboard"
btnLeaderboard.fontSize = 20
btnLeaderboard.fontColor = SKColor.blue
btnLeaderboard.fontName = "Avenir"
btnLeaderboard.position = CGPoint(x: size.width / 2, y: size.height / 2 - 50)
addChild(btnLeaderboard)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let location = touch.location(in: self)
let action = atPoint(location)
switch action.name {
case "btn_gk":
print("btn_gk")
GKLeaderboard.submitScore(10, context: 0, player: GKLocalPlayer.local, leaderboardIDs: ["com.generic.leaderboard"]) { _ in }
case "btn_leaderboard":
print("btn_leaderboard")
GameViewController().showLeaderboard()
default:
print("nothing")
}
}
}
}

I can’t provide any code right now but what you really should be doing is using something like UIKit to create a UIView for the leader board and then embed that into a SKScene.
There is an interface something like SKView that is backed by a UIView which makes this fairly easy to do.
Keep Sprite Kit for the game stuff. Use UIKit for the UI stuff. 😃

Related

Create a moving Button for a Game in Swift

I have an assignment where I need to create a game with SpriteKit.
It should have a button moving randomly within a view and if I click on it I get points.
The issue is that I have no idea how to create a button in SpriteKit.
Do I need to do a workaround by using a SKSpriteNode? But how would I make it look like a standard button? Or can I actually create a button somehow for that?
SpriteKit has no built-in SKButton class. but we can build some basic functionality. as sangony said you need three parts: draw the graphics (I'm using SKShapeNode for simplicity but you could use SKSpriteNode); move the node; add picking functionality. here is some code to illustrate.
Draw
add a SKShapeNode or SKSpriteNode to your SKNode class
shape = SKShapeNode(circleOfRadius: 40)
shape.fillColor = .green
addChild(shape)
Move
SKAction is very useful. here is an example that moves to a random position, then recursively calls itself. stop/go is regulated by a boolean flag.
func movement() {
print("movement")
let DURATION:CGFloat = 2.0
let random_x = CGFloat.random(in: -200...200)
let random_y = CGFloat.random(in: -200...200)
let random_point = CGPoint(x: random_x, y: random_y)
let move = SKAction.move(to: random_point, duration: DURATION)
move.timingMode = .easeInEaseOut
let wait = SKAction.wait(forDuration: DURATION)
let parallel = SKAction.group([move,wait])
let recursion = SKAction.run {
if self.isInMotion { self.movement() }
}
let serial = SKAction.sequence([parallel, recursion])
self.run(serial)
}
Pick
Testing for user clicks in a scene is called picking. I use a PickableNode protocol which helps filter out which SKNodes you want to be clickable.
extension GameScene {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
//call `pick` on any `PickableNode` that exists at touch location
let location = touch.location(in: self)
let _ = self.nodes(at: location).map { ($0 as? PickableNode)?.pick() }
}
}
}
Here is the whole completed class
protocol PickableNode {
func pick()
}
class Button: SKNode, PickableNode {
let shape:SKShapeNode
var isInMotion:Bool = true
override init() {
shape = SKShapeNode(circleOfRadius: 40)
shape.fillColor = .green
super.init()
addChild(shape)
movement()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func movement() {
print("movement")
let DURATION:CGFloat = 2.0
let random_x = CGFloat.random(in: -200...200)
let random_y = CGFloat.random(in: -200...200)
let random_point = CGPoint(x: random_x, y: random_y)
let move = SKAction.move(to: random_point, duration: DURATION)
move.timingMode = .easeInEaseOut
let wait = SKAction.wait(forDuration: DURATION)
let parallel = SKAction.group([move,wait])
let recursion = SKAction.run {
if self.isInMotion { self.movement() }
}
let serial = SKAction.sequence([parallel, recursion])
self.run(serial)
}
func pick() {
print("i got picked")
if !isInMotion {
isInMotion = true
shape.fillColor = .green
movement()
} else {
isInMotion = false
shape.fillColor = .red
self.removeAllActions()
}
}
}

How to set up an SKScene with an SKNode with a texture in Swift Playgrounds?

I tried copying the template code from a SpriteKit project into a playground, but all I get is a grey screen when I present the Scene. Do I need to create an SKScene and if so how do I assign it to the scene class that I am using.
The following is my code to create and present the scene:
#objc func goToGameScene(){
print("GameView Loaded")
sceneView = SKView(frame: CGRect(x: 0, y: 0, width: 666, height: 500))
sceneView.backgroundColor = UIColor.black
PlaygroundPage.current.liveView = sceneView
if let view = self.sceneView 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
}
And this is my SKScene class, which has a filename of GameScene.swift.
import Foundation
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var bg = SKSpriteNode()
func didBegin(_ contact: SKPhysicsContact) {
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
let bgTexture = SKTexture(image: UIImage(named: "MainScreenBackground.png")!)
let moveBGAnimation = SKAction.move(by: CGVector(dx:-bgTexture.size().width, dy:0), duration: 11)
let shiftBackground = SKAction.move(by: CGVector(dx: bgTexture.size().width, dy:0), duration: 0)
let repeatAnimationBg = SKAction.repeatForever(SKAction.sequence([moveBGAnimation, shiftBackground]))
var q = 0
while(q < 3){
bg = SKSpriteNode(texture: bgTexture)
bg.position = CGPoint(x: bgTexture.size().width * CGFloat(q), y: self.frame.midY)
bg.size.height = self.frame.height
bg.run(repeatAnimationBg)
self.addChild(bg)
q+=1
bg.zPosition = -1
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
}
Assuming you have dragged and dropped your MainScreenBackground.png image into you Assets.xcassets folder, you can use the code below to add the image to your scene as a SKSpriteNode.
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
let bgTexture = SKSpriteNode(imageNamed: "MainScreenBackground")
self.addChild(bgTexture)
...

Main Menu In Swift

I would like to create a main menu for my game in swift.
I am using the following code:
import SpriteKit
class menuScene: SKScene {
//Adding Start Button
let startButton = SKSpriteNode(imageNamed: "playButton")
override func didMove(to view: SKView) {
//Temporary Background
backgroundColor = SKColor.darkGray
//Start Button
startButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(startButton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self); //Finding location of touch
if atPoint(location) == startButton {
if let scene = GameScene(fileNamed: "GameScene") {
scene.scaleMode = .aspectFill
view!.presentScene(scene, transition: SKTransition.doorsOpenVertical(withDuration: 1))
}
}
}
}
}
When I run this however, My app crashes and highlights if atPoint(location) == startButton {. with "Thread 1, Breakpoint 1.1"
Im not entirely sure what this is but I hope someone can help. Thanks!
Custom SKViews
Let's say, like this M.W.E., you want a menu, a difficulty, and a game scene.
Then you can make a series of custom SKViews to transition between.
GameViewController
This code loads the menuScene:
override func viewDidLoad() {
super.viewDidLoad()
let menuScene = MenuScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
menuScene.scaleMode = .resizeFill
skView.presentScene(menuScene)
}
MenuScene
class MenuScene: SKScene {
let playButton = SKLabelNode()
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.white
playButton.fontColor = SKColor.black
playButton.text = "play"
playButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
addChild(playButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if playButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let difficultyScene = DifficultyScene(size: self.size)
self.view?.presentScene(difficultyScene, transition: reveal)
}
}
}
DifficultyScene
class DifficultyScene: SKScene {
let easyButton = SKLabelNode()
let hardButton = SKLabelNode()
let menuButton = SKLabelNode()
override init(size: CGSize) {
super.init(size: size)
backgroundColor = SKColor.white
easyButton.fontColor = SKColor.black
easyButton.text = "easy"
hardButton.fontColor = SKColor.black
hardButton.text = "hard"
menuButton.fontColor = SKColor.black
menuButton.text = "menu"
easyButton.position = CGPoint(x: size.width / 2, y: size.height / 2)
hardButton.position = CGPoint(x: size.width / 2, y: size.height / 2 - easyButton.fontSize * 2)
menuButton.position = CGPoint(x: size.width / 4 * 3, y: size.height / 4)
addChild(easyButton)
addChild(hardButton)
addChild(menuButton)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let touchLocation = touch!.location(in: self)
if easyButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let gameScene = GameScene(size: self.size, difficulty: easyButton.text!)
self.view?.presentScene(gameScene, transition: reveal)
}
if hardButton.contains(touchLocation) {
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let gameScene = GameScene(size: self.size, difficulty: hardButton.text!)
self.view?.presentScene(gameScene, transition: reveal)
}
if menuButton.contains(touchLocation){
let reveal = SKTransition.doorsOpenVertical(withDuration: 0.5)
let menuScene = MenuScene(size: self.size)
self.view?.presentScene(menuScene, transition: reveal)
}
}
}
GameScene
add this to your GameScene:
init(size: CGSize, difficulty: String) {
super.init(size: size)
gameDifficulty = difficulty
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Storyboards
Alternatively, you can use Storyboards. In the M.W.E. for another S.O. question they have a basic "menu" set up.
In your case, what you would do is:
go to Main.storyboard.
on the right-hand tool bar, find view controller
drag view-controller into Main.storyboard
click on the new view-controller
click - on the right-hand tool bar - the identity inspector (looks like a business card)
change Class to GameViewController
click on view within the hierarchy on the left (under the new view controller)
click the identity inspector
change class to SKView
click on the original view controller
click on the identity inspector
change class to UIViewController
click on the view within the original UIViewController
click on identity inspector
change class to UIView
find button at the bottom of the right-hand side tool bar
drag it onto the first view
right click drag from the button to the second view
on the pop-up menu, under action segue, click show
right click drag from the button up, add horizontally center constraints
right click drag from the button to the right, add vertically center constraints
Images

Error when trying to initialize a class as a SKSpriteNode subclass and use it in GameScene - Swift 2

All the tutorials I have followed, make sprites with all their functions and everything in the same class as everything else - GameScene class. Now I am trying to divide the code from the GameScene to different classes but am having troubles with initializing the first class. Because I have almost no code, I will copy and paste all of it.
Error message:
/Users/Larisa/Desktop/Xcode/Igranje/Poskus2/Poskus2/PlayerUros.swift:17:15: Cannot invoke 'SKSpriteNode.init' with an argument list of type '(texture: SKTexture, position: CGPoint, size: () -> CGSize)'
GameScene class:
import SpriteKit
class GameScene: SKScene {
var player: PlayerUros!
override func didMoveToView(view: SKView) {
let pos = CGPointMake(size.width*0.1, size.height/2)
player = PlayerUros(pos: pos)
addChild(player)
player.rotate()
setupBackground()
}
func setupBackground() {
let BImage = SKSpriteNode(imageNamed: "bg-sky")
BImage.anchorPoint = CGPointMake(0, 0)
BImage.position = CGPointMake(0, 0)
BImage.size = CGSizeMake(self.size.width, self.size.height)
addChild(BImage)
}
}
Player class:
import Darwin
import SpriteKit
class PlayerUros: SKSpriteNode {
init(pos: CGPoint) {
let texture = SKTexture(imageNamed: "hero")
super.init(texture: texture, position: pos, size: SKTexture.size(texture))
}
func rotate() {
let rotate = SKAction.rotateByAngle(CGFloat(M_PI), duration: NSTimeInterval(1.5))
let repeatAction = SKAction.repeatActionForever(rotate)
self.runAction(repeatAction)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
and GameViewController class (not sure if it matters):
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Try using the line
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
instead of
super.init(texture: texture, position: pos, size: SKTexture.size(texture))
in your Player class. It works for me. Does it work for you?

How to switch between scenes in Sprite Kit?

What's wrong with my code? Every time I hit the button I'm getting a default grey screen. Button for transition:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause {
var pauseScene = PauseScene(size: self.size)
scene?.paused = true
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
pauseScene.scaleMode = .AspectFill
pauseScene.size = skView.bounds.size
skView.presentScene(pauseScene)
}
}
}
What I have in that scene that don't want to load:
class PauseScene: SKScene {
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")
override func didMoveToView(view: SKView) {
self.pauseBackground.anchorPoint = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(pauseBackground)
}
}
Did I miss something?
It looks like your issue is in didMoveToView: in your PauseScene. You don't want to change your anchor point for self.pauseBackground Normally anchor point is based on 0 to 1. (.5,.5) being the center of the sprite. You are setting it to something much higher than that and if you are setting the position to half the width and hight of the scene I don't see why you would want to change the anchor point at all. Looks like just deleting the line of code should fix the issue.
class PauseScene: SKScene {
let pauseBackground = SKSpriteNode (imageNamed: "pauseBackground")
override func didMoveToView(view: SKView) {
self.pauseBackground.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
self.addChild(pauseBackground)
}
}
Also I see you are changing the size of your scene after you init it. I would just init it with the size you want.
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.pause {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
var pauseScene = PauseScene(size: skView.bounds.size)
scene?.paused = true
pauseScene.scaleMode = .AspectFill
skView.presentScene(pauseScene)
}
}
}
Hopefully that helps and makes sense.
Here is an updated answer swift 5, iOS 14 Using swiftUI
import SwiftUI
import SpriteKit
struct ContentView: View {
#State var switcher = false
var scene: SKScene {
let scene = GameScene.shared
scene.size = CGSize(width: 256, height: 256)
scene.scaleMode = .fill
scene.backgroundColor = .red
scene.name = "red"
return scene
}
var scene2: SKScene {
let scene2 = GameScene2.shared
scene2.size = CGSize(width: 256, height: 256)
scene2.scaleMode = .fill
scene2.backgroundColor = .blue
scene2.name = "blue"
return scene2
}
var body: some View {
if switcher {
SpriteView(scene: scene)
.frame(width: 256, height: 256)
.ignoresSafeArea()
.background(Color.red)
.onAppear {
scene2.isPaused = true
}
.onDisappear {
scene2.isPaused = false
}
} else {
SpriteView(scene: scene2)
.frame(width: 256, height: 256)
.ignoresSafeArea()
.background(Color.blue)
.onAppear {
scene.isPaused = true
}
.onDisappear {
scene.isPaused = false
}
}
Button {
withAnimation(.easeInOut(duration: 1.0)) {
switcher.toggle()
}
} label: {
Text("switch")
}
}
}
class GameScene: SKScene {
static var shared = GameScene()
override func update(_ currentTime: TimeInterval) {
// Tells your app to perform any app-specific logic to update your scene.
print("name ",scene!.name)
}
}