4

我为我正在制作的一些游戏制作了一个骰子(在 c# 中),它是一个用户控件,它使用故事板来显示多个图像(如幻灯片),因此它看起来像一个滚动的 3D 骰子。问题是在特定关键帧处启动和停止它。为此使用 Pause() 和 Resume() 似乎是合乎逻辑的,但我不知道如何在确切的关键帧处暂停。

有些人使用单独的 dispatcherTimer 来执行此操作,但这还不够精确,无法在确切的关键帧处停止。(例如,如果你抛出 4 它必须在 4 图像上停止)。

所以,如果有这样的方法就好了:

TimeSpan keyTime = new TimeSpan(0,0,0,0,750); // 750 miliseconds
myStoryBoard.ResumeTo(keyTime); // <- doesn't exist as far as I know

这是我在 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>
.....

还有一些图像可以使事情更清楚:

1234

4

1 回答 1

2

尝试这个...

我的例子是一个旋转箭头,我可以将它停在指定的角度。

<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
于 2012-04-28T00:29:44.323 回答