Sequencing – No, I don’t want everything running massively parallel!!

Just thought I’d check in with a problem I had with Pattern Popper and the eventual solution.

Pattern Popper is a follow the leader, memory sequence game. The computer displays a pattern and the player repeats the pattern. After every turn, the computer adds an additional step to the pattern resulting in the user having to remember an ever increasing sequence.

My problem arose when I tried to make the buttons flash in correct sequence with enough time in between each step that a human can discern the sequence. Note for the obvious, this is sequential, not parallel… with pauses in between each step before starting the next step.

I tried sleeping in between each step, then running a timer in between each step. What would happen is that the system would run through all the steps at lightning speed, and stack up all the waits for later. Using println() statements, I could see this happening. Totally unsatisfactory!

My next attempt was to call the animate function. The button and it’s “pressed” star graphic are occupying the same space. The apha on the button is 1 where the pressed star graphic’s alpha is 0. I’d animate the transition over 0.3 seconds to the opposite. Then set them back to their original alpha state.

What happened was that the system raced through the buttons, animating all it could do simultaneously. This, again, was a mess. A person could not discern a pattern… especially when one button was trying to do several animations simultaneously.

The fix was to use the animations completion block to not only shift back the graphics to their original state, but to make a recursive call of this function for the next step.  The subsequent animation is paused and triggered by the current animation completing.

Here is what it looked like when done.

func followMeSequence(thisSequence: Int)
// beginning state of the button
// Some other code in here too, but not part of this conversation
            targetButton.alpha =  1
            targetImage.alpha = 0
            // fade in the star and fade out the button
            // completion block calls this method recursivly for next button
            UIView.animateWithDuration(Double(speed), delay: 0.0, options: options, animations: {
                targetButton.alpha = 0
                targetImage.alpha = 1
                }, completion:{(value: Bool) in
                    targetButton.alpha = 1
                    targetImage.alpha = 0
                    let newSequence = thisSequence + 1
                    self.followMeSequence( newSequence)

As a note, the call to the sound function is in the animate code block, not the completion.  If I put the sound call in the completion, it triggered after the button appeared.  On the other hand, placing the sound function call before the animate block resulted in a sound triggering before the button appeared.

The Adventure Continues…


Share ...