After having researched SpriteKit implementations and QAs, I felt it was better for me to ask the question myself. I'm trying to create a Dance Formations App where there is an option to play through all formations created. I'm Using SKAction to move each of the nodes in the formation. For example, the code loops through an array of formations, for formation 1: the 3 dancers move to their respective positions, for formation 2, the 3 dancers move to their next respective positions together, and so on.
The problem is that as I go through the for loop - each of the nodes move position is being updated asynchronously, so the dancers never go from Formation 1 -> 2 -> 3, they end up going from Formation 1 -> Last formation positions. Please help!
let action = SKAction.run{
//Calculating formationArray to get next Formation done here
for dancer in formationArray {
if let toUpdateIndex = currNodes.firstIndex(where: { $0.nodeId == dancer.id }) {
let next = CGPoint(x: CGFloat(dancer.xPos), y: CGFloat(dancer.yPos))
let action = SKAction.move(to: next , duration: 3.0)
currNodes[toUpdateIndex].run(action)
////In the above line, each node is receiving it's own position to move to
}
}
let sequence = SKAction.sequence([action, action, action])
self.run(sequence)
The problem is that as I go through the for loop - each of the nodes move position is being updated asynchronously, so the dancers never go from Formation 1 -> 2 -> 3, they end up going from Formation 1 -> Last formation positions. Please help!
let action = SKAction.run{
//Calculating formationArray to get next Formation done here
for dancer in formationArray {
if let toUpdateIndex = currNodes.firstIndex(where: { $0.nodeId == dancer.id }) {
let next = CGPoint(x: CGFloat(dancer.xPos), y: CGFloat(dancer.yPos))
let action = SKAction.move(to: next , duration: 3.0)
currNodes[toUpdateIndex].run(action)
////In the above line, each node is receiving it's own position to move to
}
}
let sequence = SKAction.sequence([action, action, action])
self.run(sequence)