2012-04-01 89 views
4

我爲我製作的一些遊戲(在C#中)製作了一個模組,它是一個用戶控件,它使用故事板在多個圖像之後顯示多個圖像(如幻燈片),因此它看起來像一個滾動的3D死。問題是在特定的keyFrame上啓動和停止它。爲此,使用Pause()和Resume()似乎是合乎邏輯的,但我無法弄清楚如何在確切的keyFrame上暫停。在精確keyFrame處停止故事板

有些人使用一個單獨的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

+1

也許這會更容易爲每個案件組成動畫? – 2012-04-01 17:42:38

+0

@MaratKhasanov這實際上可行,有36個案例,但由於您可以使用Seek()更改起點,所以我需要6. tx!儘管我仍然想知道是否有適當的課程。 – Jesse 2012-04-01 18:22:10

+0

你有沒有想過如何最終做到這一點? – 2012-05-10 08:25:06

回答

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