Init subclass of SKSpriteNode using .sks file Xcode 8 - swift

In my GameScene.sks I have a SKSpriteNode which represents a "ball".
I need to subclass SKSpriteNode as Ball:
class Ball:SKSpriteNode {
//custom init
}
In my scene, I would like init my Ball using the SKSpriteNode in the .sks.
As I'm using Xcode 8, I tried to use the custom class in my SKSpriteNode:
self.ball = self.childNode(withName: "ball") as! Ball
But my app crashes at this line...
Also I'm not sure how to create a custom initializer for my subclass.
FYI I would prefer to avoid having something like:
class Ball:SKNode {
var sprite:SKSpriteNode!
}
let ball = Ball()
ball.sprite = self.childNode(withName: "ball") as? SKSpriteNode

I think the problem here is that you are not assigning a name to the sprite you add in the scene editor.
You could retrieve your sprite searching for type instead that searching for name
Try this
self.ball = self.children.flatMap { $0 as? Ball }.first!

Related

swift - use an integer for loop in a variable's name

I' like to doe something like this :
for i in 0...3 {
let skspritenode\(i) = SKSpriteNode(imageNamed: "Layer_\(i)")
self.addChild(skspritenode\(i))
}
that doesn't work because my skspritenode(i) is a let and not a string
and I have tried this:
for i in 0...3 {
let skspritenode = SKSpriteNode(imageNamed: "Layer_\(i)")
self.addchild(skspritenode)
}
which doesn't work because skspritenode already has a parent.
Is there a way of doing this in a for loop or do i have to declare each let separately? Thank you in advanced
Why would you need to name each node differently? skspritenode variable is scoped in for loop, thus the following code should work just fine:
for i in 0...3 {
let skspritenode = SKSpriteNode(imageNamed: "Layer_\(i)")
self.addchild(skspritenode)
}
Your point
which doesn't work because skspritenode already has a parent
is incorrect, since you're creating new SKSpriteNode on each iteration.

Use of unresolved identifier 'zombies'

I am making a zombie apocalypse-style cheese game and I am having trouble knowing where/how to declare zombies. I thought that since I had them made in my Gamescene.sks file with each the name 'zombie,' then they would be declared like that. I am trying to make the zombies(which I made 4 in the .sks file) chase the player(which I also set up the .sks file and coded in the upper lines of code.)
override func didMove(to view: SKView) {
let player = self.childNode(withName: "player") as? SKSpriteNode
for child in self.children{
if child.name == "zombie"{
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
}
After a while of trying, I found out that my .sks and .swift files were not communicating and I had to set up the view.
if let view = self.view as! SKView?
if let scene = GameScene(fileNamed: "GameScene")
scene.scaleMode = .resizeFill

How do I use .SKS SKSpriteNode in GameScene?

I have the player all set up with its action and everything in GameScene.swift. The thing is, I don't want it to be a still image so I removed
var player: SKSpriteNode = SKSpriteNode(imageNamed: "player1.png")
and I want to make it so I can use the animation SKSpriteNode I made in GameScene.sks, as the player. The SKSpriteNode in GameScene.sks is named player.
Help me out here?
You can access items created in GameScene.sks in the custom class function didMoveToView;
var player: SKSpriteNode!
override func didMove(to view: SKView) {
guard let player = childNode(withName: "player") as? SKSpriteNode else {
        fatalError("player node not loaded")
    }
    self.player = player
}

Change texture of SKNode that is an SKSpriteNode

In a game I'm making I can get the node the user is touching with
var node = self.nodeAtPoint(positionInScene)
However, this gets me an SKNode (even if the node the user touches is an SKSpriteNode) and I cannot use node.texture to change its texture. How can I change that SKNode to an SKSpriteNode or change its texture? (Actions won't work here because I need to pause the scene and they won't work when the scene is paused).
Casting is what you are looking for.
For instance:
If you are sure that the casting will be successful and your SKNode() is actually a SKSpriteNode, you can do the following:
let s = SKNode()
let ss: SKSpriteNode = (s as? SKSpriteNode)!
However, it is always better to use the optional specially in casting:
let ss2: SKSpriteNode? = s as? SKSpriteNode

swift change SKSpriteNode image in SKScene class

I defined SKSpriteNode under SKScene class, how can i change the SKSpriteNode image afterwards, for example:
class GameScene: SKScene {
var player = SKSpriteNode(imageNamed: "player")
}
I want to change the image from "player" to "player1" during some action. I do not inherit SKNode class, so i can not use SKTexture method to do it.
SKScene is a subclass of SKNode so you do in fact inherit it. You can use SKTexture to change the image like this:
player.texture = SKTexture(imageNamed: "player1")