Unable to disable, then click again after SKAction animation

I am working on an interactive animation scene. I want all touches on the stage to be disabled when recording. Then, when the objects (which are subclasses of nodes) in the scene finish rotating / moving, I want to turn on all touches on the screen again to allow interaction. I disabled user interaction with this code:

override func didMove(to view: SKView) {
    setupNodes()
    view?.isUserInteractionEnabled = false
    spinLocations()
}

This is the code inside the scene file for spinLocations:

func spinLocations() {
    var allLocationArrays = [[String : CGPoint]]()
    var previousArray = hiddenLocationPositions
    for _ in 0...SearchConstant.numSpins {
        let freshArray = generateNewLocationArray(previous: previousArray)
        allLocationArrays.append(freshArray)
        previousArray = freshArray
    }
    for (item, _) in hiddenLocationPositions {
        let node = fgNode.childNode(withName: item) as! LocationNode
        node.spin(position: allLocationArrays) // this is function below
    }
    hiddenLocationPositions = previousArray
}

This is the code for animations in the node class:

func spin(position: [[String : CGPoint]]) {
    var allActions = [SKAction]()
    for array in position {
        let action = SKAction.move(to: array[self.name!]!, duration: 2.0)
        allActions.append(action)
    }
    let allActionsSeq = SKAction.sequence(allActions)
    self.run(SKAction.sequence([SKAction.wait(forDuration: 5.0), allActionsSeq, SKAction.run {
        self.position = position[position.count - 1][self.name!]!
        },]))
}

This is the code for transmitting touches of the main scene from this class:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let parent = self.parent else { return }
}

As you can see, touch is not disabled here.

"waitForDuration" SKAction runBlock, ; , , , .

, , , (, , ). runBlock, , , . , "waitForDuration."?

+4
1

, , , :

1)

2) Spin a node

3) node ,

( / , , ):

class Object:SKSpriteNode{

    func spin(times:Int,completion:@escaping ()->()) {

       let duration = 3.0
       let angle = CGFloat(M_PI) * 2.0

       let oneRevolution = SKAction.rotate(byAngle: angle , duration: duration)
       let spin = SKAction.repeat(oneRevolution, count: times)

       let sequence = SKAction.sequence([spin,SKAction.run(completion)])

       run(sequence, withKey:"spinning")
    }

}

class WelcomeScene: SKScene {


    override func didMove(to view: SKView) {


        view.isUserInteractionEnabled = false
        print("Touches Disabled")

        let object = Object(texture: nil, color: .purple, size: CGSize(width: 200, height: 200))

        addChild(object)

        object.spin(times: 3, completion: {[weak self] in

            self?.view?.isUserInteractionEnabled = true
            print("Touches Enabled")
        })

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch detected")
    }

    deinit {
        print("Welcome scene deinited")
    }
}

, , ... :

let sequence = SKAction.sequence([spin,SKAction.run(completion)])

, . ... , , ... , , , node , , .

+3

Source: https://habr.com/ru/post/1674722/


All Articles