Waiting for SKAction completion inside a for loop - swift

I have a for loop that runs 10 times, each time creating a node and adding it to the scene. However, I want there to be a delay in between each node being added (add a node, wait a second, add a node, wait a second, etc.)
However, after the first 1 second, all 10 nodes are added at the same time. How can I achieve this desired effect of waiting a second in between each node being added?
Here is my code:
EDIT:
func createText(correct: Bool) {
let text = SKNode()
var line: String!
addChild(text)
if correct {
line = (GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(happyLines) as! [String])[0]
} else {
line = (GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(sadLines) as! [String])[0]
}
var xPos = self.frame.midX + 300
var yPos = self.frame.midY
var spaces = 1
// For each character in sentence, create a node of it
for character in line.characters {
runAction(SKAction.waitForDuration(1.0)) {
if spaces == 4 {
spaces = 0
print("space")
xPos = self.frame.midX + 300
yPos -= 30
}
xPos += 10
if character != " " {
let letter = SKLabelNode(fontNamed: GameScene.fontName)
letter.fontSize = 14 * GameScene.fontScale
letter.position = CGPoint(x: xPos, y: yPos)
letter.text = String(character)
text.addChild(letter)
} else {
spaces += 1
xPos += 10
}
}
}
runAction(SKAction.waitForDuration(2.0)) {
text.removeAllChildren()
text.removeFromParent()
}
}

You can achieve this using actions.
First, create actions for the delay and adding the node to the scene.
let waitAction = SKAction.wait(forDuration: 1)
let addNodeAction = SKAction.run {
let node = SKSpriteNode(imageNamed: "example")
self.addChild(node)
}
Next, create a sequence of actions so that the delay always occurs before the node is added to the scene.
let sequenceAction = SKAction.sequence([waitAction, addNodeAction])
Next, create an action that repeats the sequence 10 times.
let repeatAction = SKAction.repeat(sequenceAction, count: 10)
Finally, run this action and watch the nodes appear!
run(repeatAction)
EDIT:
To solve your second question in the comments about needing access to the current character (making each action where the node is added slightly different), loop through your characters to build a sequence of actions, and then run that action.
var actions = [SKAction]()
let waitAction = SKAction.wait(forDuration: 1)
for character in line.characters {
let addNodeAction = SKAction.run {
let node = SKSpriteNode(imageNamed: "example")
// use the character variable here
self.addChild(node)
}
actions.append(waitAction)
actions.append(addNodeAction)
}
let sequenceAction = SKAction.sequence(actions)
run(sequenceAction)

How about this? (not tested :)
for (i, character) in line.characters.enumerate() {
runAction(SKAction.waitForDuration(Double(i) * 1.0)) {
// rest of your for loop
So you enumerate through the characters, where i is the i-th character, starting at 0. With each character, you just multiply its index position in the string with the 1 second interval to give you the wait duration.

Related

SceneKit: How to arrange buttons in ascending order using for in loop?

The task is to add 10 buttons (0...9) with labels using for in loop.
I created buttons based on class ButtonPrototype. I assigned label to each button via counter inside for in loop.
It works, but there is incorrect labels order:
I need another order:
How can I implement correct order?
Code:
func createButtons() {
for y in 0...1 {
for x in 0...4 {
counterForLoop += 1
self.button = ButtonPrototype(pos: .init( CGFloat(x)/7, CGFloat(y)/7, 0 ), imageName: "\(counterForLoop)")
parentNode.addChildNode(button)
parentNode.position = SCNVector3(x: 100,
y: 100,
z: 100)
}
}
}
The following approach perfectly makes the trick:
for y in 0...1 {
for x in 0...4 {
let textNode = SCNNode()
let ascendingOrder: String = "\(((x+1)+(y*5)) % 10)"
let geo = SCNText(string: ascendingOrder, extrusionDepth: 0.5)
geo.flatness = 0.04
geo.firstMaterial?.diffuse.contents = UIImage(named: ascendingOrder)
textNode.geometry = geo
textNode.position = SCNVector3(x*10, -y*10, 0)
sceneView.scene?.rootNode.addChildNode(textNode)
print(ascendingOrder)
}
}
You have at least two problems with your code. Your smallest button label is in the lower left and you want it to be in the lower right, and your labels go 0-9, and you want them to go from 1 to 10 (but display 10 as “0”).
To reverse the x ordering, change X to 10-x in your creation of a position, and change your imageName to “((counterForLoop+1)%10)”:
self.button = ButtonPrototype(
pos: .init(
CGFloat(10-x)/7,
CGFloat(y)/7,
0),
imageName: "\((counterForLoop+1)%10)")
By the way, you should add a SceneKit tag to your question. That seems more important than either the label tag or the loops tag.

Step Counter Sprite Kit

I am trying to implement a step counter in my sprite Kit game.
And it should work like this:
The counter adds 1 to a value each second.
Every fifth second the duration (in this case 1) gets divided by 1.1
But if I create a func that returns the new duration, the repeat forever SKAction only uses this value for one time and then the duration never changes again.
you should make an action that calls itself, rather than using SKAction.repeatForever(...). you can recalculate values that way. not sure i entirely understand your use case, but here is an example that fires an event after a duration, and modifies that duration every fifth cycle.
var isLoopEnabled:Bool = true
var counter:Int = 0
var duration:TimeInterval = 1.0
func updateDuration() {
duration /= 1.1
}
/*
creates an event loop. the action waits, fires, then calls itself again (before exiting)
turn the loop off using the isLoopEnabled flag
*/
func loop() {
let wait = SKAction.wait(forDuration: duration)
let run = SKAction.run {
self.counter += 1 //increment counter
//update duration every fifth count
if self.counter % 5 == 0 {
self.updateDuration()
}
}
let end = SKAction.run{
print("\(self.counter) -- duration: \(self.duration)")
guard self.isLoopEnabled else { return } //flag allows you to exit loop
self.loop() //repeats by calling itself
}
let sequence = SKAction.sequence([ wait, run, end ])
self.run(sequence, withKey:"loop action")
}
override func didMove(to view: SKView) {
loop()
}

Error with Dispatch Queue Swift

I am trying to create a genetic algorithm for running race cars around a race track. Each car gets random instructions that apply a force to the car and rotate the car by a certain number of degrees. In order to space out the new instructions given to each car, I used a delay time in dispatch Queue that adds 0.2 seconds to the previous instruction.
e.g.
0 seconds - first instruction
0.2 seconds - second instruction
0.4 seconds -third instruction
and so on...
The problem I have is that after several instructions have been carried out I start to notice a longer delay between instructions, to the point where a new instruction is applied after say 2 seconds.
Here is my code below.
func carAction(newCar: [[CGFloat]], racecar: SKSpriteNode) {
var caralive = true
let max = 1000
var count = 0
let delayTime = 200000000
var deadlineTime = DispatchTime.now()
while count < max {
let angleChange = newCar[count][1]
let speedChange = newCar[count][0]
count += 1
deadlineTime = deadlineTime + .nanoseconds(delayTime)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
if caralive == true {
print(DispatchQueue.main)
racecar.physicsBody?.angularVelocity = 0
let rotate = SKAction.rotate(byAngle: (angleChange * .pi / 180), duration: 0.2)
racecar.run(rotate)
let racecarRotation : CGFloat = racecar.zRotation
var calcRotation : Float = Float(racecarRotation) + Float(M_PI_2)
let Vx = speedChange * CGFloat(cosf(calcRotation))
let Vy = speedChange * CGFloat(sinf(calcRotation))
let force = SKAction.applyForce(CGVector(dx: Vx, dy: Vy), duration: 0.2)
racecar.run(force)
let total = self.outerTrack.count
var initial = 0
while initial < total {
if racecar.intersects(self.outerTrack[initial]) {
racecar.removeFromParent()
self.numberOfCars -= 1
initial += 1
caralive = false
break
} else {
initial += 1
}
}
} else {
// print(self.numberOfCars)
}
}
}
The 2D array newCar is a list of all the instructions.
Any help would be massively appreciated as I have been trying to figure this out for ages now!!
Many thanks in advance, any questions just feel free to ask!
You should do something like this instead:
func scheduledTimerWithTimeInterval(){
// Scheduling timer to Call the function "updateCounting" with the interval of 1 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("moveCarsFunction"), userInfo: nil, repeats: true)
}
And call scheduledTimerWithInterval once
originally answered here

"Attemped to add a SKNode which already has a parent:" in Repeat Loop. Any simple work around?

I am pretty Newbie to programming. And I am trying to pile up the random blocks dynamically till it hits the upper frame. But it seems that Swift doesn't let me to do so. Did I miss anything please? Any input are appreciated.
let blocks =[block1,block2,block3,block4,block5,block6,block7,block8,block9,block10,block11,block12]
var block:SKSpriteNode!
let blockX:Double = 0.0
var blockY:Double = -(self.size.height/2)
repeat{
block = blocks.randomBlock()
block.zPosition = 2
block.position = CGPoint(x:blockX, y:blockY)
block.size.height = 50
block.size.width = 50
self.addChild(block)
blockY += 50
} while( block.position.y < self.size.height)
extension Array {
func randomBlock()-> Element {
let randint = Int(arc4random_uniform(UInt32(self.count)))
return self[randint]
}
}
you need to have someway of tracking which blocks have been selected and ensure that they don't get selected again. The method below uses an array to store the indexes of selected blocks and then uses recursion to find a cycle through until an unused match is found.
private var usedBlocks = [Int]()
func randomBlock() -> Int {
guard usedBlocks.count != blocks.count else { return -1 }
let random = Int(arc4random_uniform(UInt32(blocks.count)))
if usedBlocks.contains(random) {
return randomBlock()
}
usedBlocks.append(random)
return random
}
in your loop change your initializer to
let index = randomBlock()
if index > -1 {
block = blocks[index]
block.zPosition = 2
block.position = CGPoint(x:blockX, y:blockY)
}
remember that if you restart the game or start a new level, etc. you must clear all of the objects from usedBlocks
usedBlocks.removeAll()

Choosing to spawn different SKSpriteNodes

I am making a game and I have objects which fall from the top of the screen to the bottom. I want to spawn choose between the objects and drop one of them. I currently the drop all at the same time.
func ShapePicker() -> SKSpriteNode{
let shapeArray = [purpleOctagon, coin, greenTriangle, orangeHexagon]
let MaxValue = self.size.width / 2 - 200
let MinValue = self.size.width / 3 * 0.95
let rangeMax = UInt32(MaxValue)
let rangeMin = UInt32(MinValue)
purpleOctagon.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
self.addChild(purpleOctagon)
greenTriangle.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
self.addChild(greenTriangle)
coin.position = CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax), y: self.size.height)
self.addChild(coin)
return shapeArray[Int(arc4random_uniform(UInt32(shapeArray.count)))]
}
I would like the program to randomly .addChild because right now it just puts them on the screen.
Your code implies that you want all of them to be on the screen, and then one randomly drops... So you do want to continue to .addChild. What you want, is for them to NOT drop all at once.
So, you need to change the .physicsBody.pinned to true to keep them at the top of the screen.
Then, in your update() you can check for how much time has passed, etc, and after a certain # of seconds you can do an arc4random_uniform and use that result to change the .pinned property of one of the nodes (thus causing that one and that one only to fall).
So, if the coin is 0, triangle is 1, and octagon is 2, then, in your .update keep track of the time elapsed and after say 3 seconds, do a random check 0-2, and that check will perform:
switch result {
case 0: // coin
coin.physicsBody?.pinned = false // makes it drop
case 1: // triangle
...
Just make sure that your nodes are in the proper scope so that you can do the logic in update()
If I read your Q wrong, and you only want to spawn and then drop just one, then you would still need the above switch statement, but instead of changing physicsBody you would do .addChild instead.
so inside of your func would be more like:
// This can be global or inside of your GameScene:
var myGlobalCurrentTime: CFTimeInterval
override func update(currentTime: CFTimeInterval) {
myGlobalCurrentTime = myTimerUpdateTime(currentTime)
func myDropFunc() {
... // Initialize your nodes
let result = myRandomNumber(3)
switch result {
case 0:
coin.position
= CGPoint(x: CGFloat(arc4random_uniform(rangeMin) + rangeMax),
y: self.size.height)
self.addChild(coin)
case 1:
...
}
}
// Execute the func:
myDropFunc()
}
I'm still a bit confused by your code and question, so please clarify in the comments so I can update this answer if needed.
You can move the addChild call out of this function, it returns a SKSpriteNode, which you can then add.
let sprite = ShapePicker()
addChild(sprite)
Or, just add the one randomly chosen and return it. You don't need to addChild nodes that you're not planning on using