问题描述
我为我正在制作的一些游戏制作了一个骰子(在 c# 中),它是一个用户控件,它使用故事板来依次显示多个图像(如幻灯片),因此它看起来像一个滚动的 3D 骰子.问题是在特定的关键帧处启动和停止它.为此使用 Pause() 和 Resume() 似乎合乎逻辑,但我不知道如何在确切的关键帧处暂停.
I made a die for some game I'm making (in c#), it's a usercontrol which uses a storyboard to show several images after each other (like a slideshow) so it looks like a rolling 3D die. The problem is starting and stopping it at a specific keyFrame. It's seems logical to use Pause() and Resume() for this, but I can't figure out how to Pause at an exact keyFrame.
有些人使用单独的 dispatcherTimer 来做到这一点,但这还不够精确,无法在那个确切的关键帧处停止.(例如,如果您抛出 4,它必须停在 4 图像上).
Some people use a seperate dispatcherTimer to do this, but this isn't precise enough to stop it at that exact keyframe. (for example, if you throw 4 it must stop on the 4 image).
所以,如果有这样的方法就好了:
So, it would be great if there was some method like this:
TimeSpan keyTime = new TimeSpan(0,0,0,0,750); // 750 miliseconds
myStoryBoard.ResumeTo(keyTime); // <- doesn't exist as far as I know
这是我在 XAML 中的故事板的片段:
Here is a snippet from my storyboard in XAML:
<Storyboard x:Key="DieStoryBoard" RepeatBehavior="Forever">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image1">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.05">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image2">
<DiscreteObjectKeyFrame KeyTime="0:0:0.05">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.10">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="image3">
<DiscreteObjectKeyFrame KeyTime="0:0:0.10">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="0:0:0.15">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
.....
还有一些图片让事情更清晰:
And some images to make things clearer:
推荐答案
试试这个...
我的例子是一个旋转箭头,我可以把它停在指定的角度.
my example is a rotating arrow,and I can stop it at a specified angle.
<Window.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="(UIElement.RenderTransform).
(TransformGroup.Children)[2].(RotateTransform.Angle)"
Storyboard.TargetName="RightPanelButton1">
<DiscreteDoubleKeyFrame KeyTime="0:0:0" Value="0.0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:1" Value="45.0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:2" Value="90.0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:3" Value="135.0"/>
<DiscreteDoubleKeyFrame KeyTime="0:0:4" Value="180.0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
Storyboard st = ((Storyboard)this.Resources["Storyboard1"]);
st.Begin();
st.Seek(new TimeSpan(0,0,2));
st.Pause();
Abbas Ahmed
这篇关于在确切的关键帧停止故事板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!