Init multiple SKSpriteNodes - swift

I'm trying to add multiple SKSpriteNodes to my SKSpriteNode subclass. In my case it is important that I can call the childNodes out of the Scene.
Is there a way to create such global available Sprites in a loop?

When you add your sprites to the parent node remember to assign them a name
let parent = SKSpriteNode()
let child0 = SKSpriteNode()
child0.name = "child0"
parent.addChild(child0)
let child1 = SKSpriteNode()
child1.name = "child1"
parent.addChild(child1)
let child2 = SKSpriteNode()
child2.name = "child2"
parent.addChild(child2)
Later on you can retrieve them this way using the name
func foo() {
guard let child1 = parent.childNodeWithName("child1") as? SKSpriteNode else { return }
child1.alpha = 0.5
}

Related

how to print nodes names

im new for swift sprite kit and I just learning about nodes and this stuff. as I play with the nodes I wonder how can I print to console all the names of the nodes that I currently have on my scene. I read and try the enumerateChildNodes but I didn't get it right and failed while doing that.
is there any option to do what im trying to?
If you want to visit all the nodes in your scene you can define a visit method
import SpriteKit
extension SKNode {
func visit() {
print(self.name)
children.forEach { $0.visit() }
}
}
Finally in your scene just write
class Scene:SKScene {
override func didMove(to view: SKView) {
super.didMove(to: view)
visit() // <--
}
}
Example
let scene = Scene()
scene.name = "scene"
let a = SKNode()
a.name = "a"
let b = SKNode()
b.name = "b"
let c = SKNode()
c.name = "c"
let d = SKNode()
d.name = "d"
a.addChild(b)
b.addChild(c)
scene.addChild(a)
scene.addChild(d)
scene.visit()
Result
Optional("scene")
Optional("a")
Optional("b")
Optional("c")
Optional("d")

How to change node value

func createBall(){
ballS = SKNode()
let ball = SKSpriteNode(imageNamed: "Ball")
saw.zPosition = 2
ballS.addChild(ball)
self.addChild(ballS)
}
This creates a Ball node with z position 2. But while running the game, I want to create another ball that has a z position at 1. How can I do this, or do I have to make a whole new function that makes a ball node with z position 1?
If each ball sprite has to be a child of the ballS node, then you could do the following.
Define ballS as a property (i.e. after the class definition but before any functions)
let balls = SKNode()
in didMove(to:), add the ballS Node:
addChild(ballS)
Create a addBall function: (don't call it createBall unless all it does create the ball)
func addBall(withZPosition zPos: Int) {
let newBall = SKSpriteNode(imageNamed: "Ball")
newBall.zPosition = zPos
ballS.addChild(newBall)
}
Then simply call addBall(withZPosition:) to create and add a new ball:
addBall(withZPosition: 2)
If you want more control over the ball that is created (e.g. you want to change some of it's properties before adding it to the scene), you can make a createBall function that creates a new ball and returns the new node:
func createBall(withZPosition zPos: Int) -> SKSpriteNode {
let newBall = SKSpriteNode(imageNamed: "Ball")
newBall.zPosition = zPos
return newBall
}
and use this as follows:
let newBall = createBall(withZPosition: 2)
newBall.size = CGSize(x: 50, y:50)
ballS,addchild(newBall)
U can have a global variable so that whenever you want to change u set it and create the ball
so create a variable
var myZposition = 2
and create a ball at the start in didmove to view by calling createBall()
func createBall(){
ballS = SKNode()
let ball = SKSpriteNode(imageNamed: "Ball")
saw.zPosition = myZPosition
ballS.addChild(ball)
self.addChild(ballS)
}
then after these if you want to change the zPosition just set it at the end of didmove to view
myZposition = 1
Then whenever u create balls from here your zPosition would be 1. This would be an easy setup if you have lots of balls and implement a change in zPosition for reaching a specific area of the game
Hope this helped
I'd suggest to use an argument, like:
func createBall(z: Int) {
ballS = SKNode()
let ball = SKSpriteNode(imageNamed: "Ball")
saw.zPosition = z
ballS.addChild(ball)
self.addChild(ballS)
}
And use it as:
createBall(z: 2)

Adding multiple sprite nodes to the scene using single function/method in swift

Let's say I have 10 nodes, where all of the nodes are the dots image, which are node1 thru node10. I create node1 as the following:
func createNode1() -> SKNode {
let spriteNode = SKNode()
spriteNode.position = CGPointMake(CGRectGetMidX(self.frame)/1.35, CGRectGetMidY(self.frame)/1.32)
let sprite = SKSpriteNode(imageNamed: "dot_1")
sprite.zPosition = 3.0
sprite.name = "A1_Dot"
spriteNode.addChild(sprite)
return spriteNode
}
I create the rest of nodes by creating 9 more functions, where next one would be as func createNode2etc, all the way up to 10 functions, where the only difference between them is node's name and its location. Basically each node has different location in the scene and of course different image name. Is there a way to load of the 10 nodes to the scene at once and manipulate node's locations at the time of use.? I'm looking for a way to load all 10 nodes to scene using a single function or method and assign node's positions within this same function. Thanks.
You need to use a loop to iterate through an array of positions, and move your code that adds the node to the scene into the loop:
let positions = [CGPointMake(CGRectGetMidX(self.frame)/1.35, CGRectGetMidY(self.frame)/1.32), ... /*add your 9 other positions here*/]
positions.enumerate().forEach { (index, point) in
let spriteNode = SKNode()
spriteNode.position = point
let sprite = SKSpriteNode(imageNamed: "dot_\(index + 1)")
sprite.zPosition = 3.0
sprite.name = "A\(index + 1)_Dot"
spriteNode.addChild(sprite)
// Add spriteNode to the scene here
}
You can either use a loop as Jugale suggested or you could just pass the values you want into the method
For example
func createNode1(imageNamed imageNamed: String, name: String, pos: CGPoint) -> SKNode {
let spriteNode = SKNode()
spriteNode.position = pos
let sprite = SKSpriteNode(imageNamed: imageNamed)
sprite.zPosition = 3.0
sprite.name = name
spriteNode.addChild(sprite)
return spriteNode
}
And now in your scene you can add the nodes like so
let node1Pos = ...
node1 = createNode1(imageNamed: "...", name: "A1_Dot", pos: node1Pos)
let node2Pos = ...
node2 = createNode1(imageNamed: "...", name: "A1_Dot", pos: node2Pos)
I am saying ImageNamed twice in the create Node function because when you pass stuff into functions Swift by default does not require the first description to be typed when calling the method. (see below)
So if would say imageNamed only once than you would call it like so.
node1 = createNode1("...", pos: node1Pos)
Also your creating node function could be simplified, unless you specifically want to return a SKNode in the method.
func createNode1(imageNamed imageNamed: String, name: String, pos: CGPoint) -> SKSpriteNode {
let sprite = SKSpriteNode(imageNamed: imageNamed)
sprite.position = pos
sprite.zPosition = 3.0
sprite.name = name
addChild(sprite)
return sprite
}
Either way is acceptable solution. Here are full details. In baseScene we create function createNodes and call that function in didMoveToView where nodeA1 is given position and added to the scene as shown below.
override func didMoveToView(view: SKView) {
let nodeA1Pos = CGPointMake(CGRectGetMidX(self.frame)/1.5, CGRectGetMidY(self.frame)/2.0)
nodeA1 = createNodes(imageNamed: "dot_1", name: "A1_Dot", pos: nodeA1Pos)
addChild(nodeA1)
}
Then in Level1Scene which is subclass of baseScene we just give a new position to nodeA1 which will override position originally set in baseScene:
override func didMoveToView(view: SKView) {
super.didMoveToView(view)
nodeA1.position = CGPointMake(CGRectGetMidX(self.frame)/1.3, CGRectGetMidY(self.frame)/0.67)
}
This way of subclassing saves a lot of time and code as only one function is used to generate all common sprite nodes.
All thanks to crashoverride777 !!!!

Detect if a SKSpriteNode left the screen at the bottom and call a function

I have tried to find a way to detect if a SKSpriteNode left the screen (I would like to call a Game Over function).
I have declared the node within a function (It is not global if I get that right?) and made and SKAction that moves the Node out of the screen and removes it afterwards.
This is what I came up with:
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
Now what I need is to call GameOver() if the node left the screen.
I am very thankful for every answer!
In your scene you have to remember the reference for node you want to check and then in update method you just do the following:
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
Edit:
class MyScene: SKScene {
// ....
//here is the list of nodes which you want to check
var nodesToCheck = [SKSpriteNode]()
//here is your spawn function
func spawnNewNode() {
var node = SKSpriteNode()
let nodeTexture = SKTexture(imageNamed: "node")
nodeTexture.filteringMode = .Nearest
node = SKSpriteNode(texture: nodeTexture)
let nodeFalling = SKAction.moveToY(-70, duration: 1.6)
let nodeRemoving = SKAction.removeFromParent()
node.runAction(SKAction.sequence([nodeFalling, nodeRemoving]))
self.addChild(node)
nodesToCheck.append(node)
}
//and here is the update method
override func update(currentTime: NSTimeInterval) {
super.update(currentTime)
// ... every other update logic
for node in nodesToCheck {
if node.position.y < -node.size.height/2.0 {
node.removeFromParent()
gameOver()
}
}
}
func gameOver() {
println("Damn!")
}
}
Dont forget to remove your node from nodeToCheck array when they are no longer scene members.

How to randomly choose a SKSpriteNode?

I want to choose from 4 enemies using random and present it on scene. For that purpose I've made this:
func enemyPicker() -> SKSpriteNode {
var enemyArray = [mouse, robot, drone, block, bird]
var countArray = UInt32(enemyArray.count)
var pickOneEneny = arc4random_uniform(countArray)
var randomElement = Int(pickOneEnemy)
return enemyArray.randomElement
}
But Xcode says to me that SKSpriteNode does not have a member named randomElement. And it surely doesn't, but how would I say to my function that I need it to pick and assign that random Int to an actual enemy from array?
I tried to use this answer but it's not working for me. I also tried to change -> SKSpriteNode to SKTexture, String and "T" and had not any luck with it.
My SpriteNodes are declared like:
var mouse = SKSpriteNode()
let mouseAtlas = SKTextureAtlas(named: "mouse")
var mouseArray = [SKTexture]()
mouseArray.append(mouseAtlas.textureNamed("mouse_0"));
mouseArray.append(mouseAtlas.textureNamed("mouse_1"));
mouseArray.append(mouseAtlas.textureNamed("mouse_2"));
mouse = SKSpriteNode(texture: mouseArray[0]);
self.mouse.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMidY(self.frame) - 138)
self.mouse.size = CGSizeMake(self.mouse.size.width, self.mouse.size.height + mouse.size.height / 2)
self.mouse.name = "mouse"
self.addChild(mouse)
func enemyPicker() -> SKSpriteNode {
let enemyArray = [mouse, robot, drone, block, bird]
return enemyArray[Int(arc4random_uniform(UInt32(enemyArray.count)))]
}