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

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?

Related

How to display a Game Center leaderboard in SpriteKit?

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. 😃

How to pass a string to a SKLabelNode in a SKScene(filenamed:) animation file?

Previously, I passed properties into my SKScene by creating initializers in my SKScene, then setting those initializers when i create my SKScene property in my SceneEditViewController. Did that make sense? I hope so.
However, instead of creating an SKScene manually, I am now using Scene Editor. My animation scene has a SKLabelNode and i want to pass text received from a different viewController into the SKScene's SKLabelNode. Below i provided my previous method in the "if" portion of my if-else. In the "else" portion, i'm attempting the new method but need help.
When trying to set a property from my SKScene, xcode states, "Value of type 'SKScene' has no member 'selectedTextSceneUse1'"
Included below:
SceneEditViewController
Extension enabling me to receive a text string from another viewController
GameScene.swift (swift file connected to my GameScene.sks)
I'm using swift 5.0, please let me know if you need additional information.
import UIKit
import Foundation
import SpriteKit
import GameplayKit
class SceneEditViewController: UIViewController, UITabBarControllerDelegate {
var selectedSceneTextFinal: String?
if selectedSceneInt == 0 {
let animationScene3 = AnimationInventory18(size: CGSize(width:animationScreen.bounds.width, height:animationScreen.bounds.height), chosenChateracterSceneUse: chosenCharacterFinal, selectedTextSceneUse: selectedSceneTextFinal, chosenBackgroundSceneUse: chosenBackgroundFinal)
animationScene3.scaleMode = .aspectFit
animationScene3.backgroundColor = .black
animationScene3.anchorPoint = CGPoint(x: 0.5, y: 0.52)
animationScene3.position = CGPoint(x: 0, y: 0)
let transition = SKTransition.fade(withDuration: 1.0)
animationScreen.presentScene(animationScene3, transition: transition)
DispatchQueue.main.asyncAfter(deadline: .now() + 20){
self.animationScreen.presentScene(nil)
print("Scene Removed")
}
} else {
if let scene = SKScene(fileNamed: "GameScene") as GameScene {
scene.scaleMode = .aspectFit
scene.selectedTextSceneUse1 = selectedSceneTextFinal
animationScreen.presentScene(scene)
DispatchQueue.main.asyncAfter(deadline: .now() + 10){
self.animationScreen.presentScene(nil)
print("Scene Removed")
}
}
}
}
extension SceneEditViewController: textUpdateDelegate {
func didUpdateText(textFromView: String){
selectedSceneTextFinal = textFromView
}
}
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var textNode: SKLabelNode?
// Not receiving the value passed from selectedSceneTextFinal
var selectedTextSceneUse1: String?
// MARK: Camera Node
let cameraNode = SKCameraNode()
// Initializer
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func check() {
self.camera = cameraNode
cameraNode.position = CGPoint(x: 0, y: -300)
self.addChild(cameraNode)
print(selectedTextSceneUse1)
self.textNode?.text = selectedTextSceneUse1
}
override func sceneDidLoad() {
self.lastUpdateTime = 0
check()
}
}
You should cast your SKScene :
if let scene = SKScene(fileNamed: "GameScene") as? GameScene {
In these cases, as you get GameScene from file you need to implement the init?(coder..) :
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
Be sure to also setup the class type of the scene in the sks file. (custom class inspector)
If you want to setup sklabelnode text hen it change you could add observer on selectedSceneTextFinal :
class SceneEditViewController: UIViewController, UITabBarControllerDelegate {
var gameScene : GameScene?
var selectedSceneTextFinal: String? {
didSet {
// Update gameScene value when controller value is changed
gameScene?.selectedTextSceneUse1 = selectedSceneTextFinal
}
}
...
if let scene = SKScene(fileNamed: "GameScene") as GameScene {
gameScene = scene
scene.scaleMode = .aspectFit
scene.selectedTextSceneUse1 = selectedSceneTextFinal
...
class GameScene: SKScene {
var textNode: SKLabelNode?
// Update textNode on update of selectedTextSceneUse1
var selectedTextSceneUse1: String? {
didSet {
// If nit done initialise textNode from sks file (label node must have a name)
if textNode == nil {
textNode = childNode(withName: "name of LabelNode in sks file") as? SKLabelNode
}
textNode?.text = selectedTextSceneUse1
}
}

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)
...

Changing texture for subclass of SKSpriteNode?

The update function in the class below, which is a subclass of SKSpriteNode, is supposed to change the texture of the class. According to this SO answer, updating the texture property is sufficient for changing a SKSpriteNode's texture. However, this code doesn't work.
Any ideas why?
class CharacterNode: SKSpriteNode {
let spriteSize = CGSize(width: 70, height: 100)
var level = 0
init(level: Int) {
self.level = level
super.init(texture: nil, color: UIColor.clear, size: spriteSize)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(level: Int) {
self.level = level
self.texture = textureForLevel(level: level)
}
func textureForLevel(level: Int) -> SKTexture {
return SKTexture(imageNamed: "TestTexture")
}
Your code works well. In fact this SO answer is correct.
About SKSpriteNode subclass, the update method is a new custom function added by you, because the instance method update(_:) is valid only for SKScene class, this just to say that this function is not performed if it is not called explicitly.
To make a test about your class you can change your code as below( I've changed only textureForLevel method only to make this example):
class CharacterNode: SKSpriteNode {
let spriteSize = CGSize(width: 70, height: 100)
var level = 0
init(level: Int) {
self.level = level
super.init(texture: nil, color: UIColor.clear, size: spriteSize)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(level: Int) {
self.level = level
self.texture = textureForLevel(level: level)
}
func textureForLevel(level: Int) -> SKTexture {
if level == 3 {
return SKTexture(imageNamed: "TestTexture3.jpg")
}
return SKTexture(imageNamed: "TestTexture.jpg")
}
}
class GameScene: SKScene {
override func didMove(to view: SKView) {
let characterNode = CharacterNode(level:2)
characterNode.update(level:2)
addChild(characterNode)
characterNode.position = CGPoint(x:self.frame.midX,y:self.frame.midY)
characterNode.run(SKAction.wait(forDuration: 2.0), completion: {
characterNode.update(level:3)
})
}
}
As you can see when you launch GameScene a characterNode sprite will be shown in the center of the screen. After 2 seconds, the texture will change.

tilemap in sprite kit

This is about drawing a TileMap in SpriteKit. I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:['nil'] position:{64, 0} size:{0, 0} rotation:0.00'
This is my GameViewController.swift:
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: CGSize(width: 1024, height: 768))
// 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)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
This is my GameScene.swift:
import SpriteKit
class GameScene: SKScene {
let dungeon = SKNode()
let readableMap: [[Int]] = [
[01, 00, 01, 01],
[01, 00, 01, 01],
[01, 00, 01, 01],
[00, 01, 00, 01]]
override init(size: CGSize) {
super.init(size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
drawMap()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
func drawMap() {
let map = readableMap.reverse()
let tiles = [[Tile]](count: map.count, repeatedValue: [Tile](count: map[0].count, repeatedValue: Tile()))
for i in 0..<map.count {
for j in 0..<map[0].count {
let tile = tiles[j][i]
tile.position = CGPoint(x: j * Int(tile.tileSize.width), y: i * Int(tile.tileSize.height))
dungeon.addChild(tile)
}
}
}
}
this is the class in Tile.swift:
import SpriteKit
class Tile: SKSpriteNode {
let tileSize: CGSize = CGSize(width: 64, height: 64)
let tileTexture: SKTexture = SKTexture(imageNamed: "o")
}
what could be the reason? Thanks to anyone, I'm still learning.