Reputation: 1926
I want to animate a dice being rolled, but don't want to use the Morph>>step
methods because I want more control over when the roll finishes. I know that I can use Delay>>wait
within a forked block to see my animation, but then how should I call this method from other methods to ensure I get the final numberRolled
?
Here's my roll
method:
roll
| n t |
numberRolled := nil.
[
t := 10 + (10 atRandom).
t timesRepeat: [
n := 6 atRandom.
self showNumber: n.
(Delay forSeconds: 0.1) wait.
].
numberRolled := n.
] fork.
So if I call this from a method like guessLower
the roll
method returns instantly because the real work is completed in the forked process.
guessLower
previousNumberRolled := numberRolled.
self roll.
"this next line is called before the dice has finished rolling"
self checkWin: (numberRolled < previousNumberRolled)
My current solution is to modify roll
method to take a block, which that executes after the rolling has finished e.g. rollAndThen: aBlock
but is there a more elegant / simpler solution?
Upvotes: 3
Views: 290
Reputation: 4623
In Morphic it is a Really Bad Idea to use Delays and explicit looping.
But it is really simple to make the step method do what you want: Inside you simply check if it should continue rolling or not. Then you do self stopStepping. self checkWin: ...
.
Upvotes: 1