Reputation: 279
I am calling a Storyboard-Animation in my code like MyAnim.Begin();
But when I call that method again (say, when I click to do it again), it interrupts the current running animation and starts from newly from the beginning.
Is there a way, to launch the same animation multiple times without interrupting each other?
I made an explosion effect and I want to let it explode over and over again.
Upvotes: 1
Views: 149
Reputation: 6973
The Storyboard object contains a completed event. This gets fired when the storyboard has finished running.
I'd suggest something like this:
private object _locker=new object(); private bool _isProcessing; private int _waitingAnimations;
public void Setup()
{
MyAnim.Completed+=CompletedExecution;
}
private void CompletedExecution(object sender, EventArgs args)
{
bool runAnotherAnim;
lock(_locker)
{
runAnotherAnim=_waitingAnimations>0;
if(_waitingAnimations>0)
_waitingAnimations--;
}
if(runAnotherAnim)
MyAnim.Begin();
else
_isProcessing=false;
}
public void AddAnimation()
{
lock(_locker)
{
if(_isProcessing)
{
_waitingAnimations++;
}
else
{
_isProcessing=true;
MyAnim.Begin();
}
}
}
You'll need to have a look at the arguments provided by the Completed event as I'm doing this from memory, but I've used this before and it does exactly what you're looking to do.
Upvotes: 1