Silverlight动画编程全解析
立即解锁
发布时间: 2025-08-26 01:20:29 阅读量: 4 订阅数: 15 

# Silverlight动画编程全解析
## 1. 炸弹拦截游戏实现
### 1.1 拦截炸弹操作
在炸弹拦截游戏中,用户可通过在炸弹到达画布底部爆炸前点击它来拯救炸弹。由于每个炸弹都是`Bomb`用户控件的独立实例,拦截鼠标点击操作很简单,只需处理`MouseLeftButtonDown`事件即可。当点击炸弹时,会触发以下步骤:
1. 获取相应的炸弹对象,并将其`IsFalling`属性设置为`false`,表示炸弹不再下落。
```csharp
private void bomb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Get the bomb.
Bomb bomb = (Bomb)sender;
bomb.IsFalling = false;
// Record the bomb's current (animated) position.
double currentTop = Canvas.GetTop(bomb);
}
```
2. 找到控制该炸弹动画的故事板并停止它。
```csharp
// Stop the bomb from falling.
Storyboard storyboard = storyboards[bomb];
storyboard.Stop();
```
3. 复用现有的故事板,添加新的动画,将炸弹移到屏幕外。
```csharp
// Reuse the existing storyboard, but with new animations.
// Send the bomb on a new trajectory by animating Canvas.Top
// and Canvas.Left.
storyboard.Children.Clear();
DoubleAnimation riseAnimation = new DoubleAnimation();
riseAnimation.From = currentTop;
riseAnimation.To = 0;
riseAnimation.Duration = TimeSpan.FromSeconds(2);
Storyboard.SetTarget(riseAnimation, bomb);
Storyboard.SetTargetProperty(riseAnimation, new PropertyPath("(Canvas.Top)"));
storyboard.Children.Add(riseAnimation);
DoubleAnimation slideAnimation = new DoubleAnimation();
double currentLeft = Canvas.GetLeft(bomb);
// Throw the bomb off the closest side.
if (currentLeft < canvasBackground.ActualWidth / 2)
{
slideAnimation.To = -100;
}
else
{
slideAnimation.To = canvasBackground.ActualWidth + 100;
}
slideAnimation.Duration = TimeSpan.FromSeconds(1);
Storyboard.SetTarget(slideAnimation, bomb);
Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("(Canvas.Left)"));
storyboard.Children.Add(slideAnimation);
// Start the new animation.
storyboard.Duration = slideAnimation.Duration;
storyboard.Begin();
```
### 1.2 炸弹计数与清理
为了跟踪拯救和掉落的炸弹数量,需要对`Storyboard.Completed`事件做出响应。该事件在动画结束时触发,处理逻辑如下:
```csharp
private int maxDropped = 5;
private int droppedCount = 0;
private int savedCount = 0;
private void storyboard_Completed(object sender, EventArgs e)
{
Storyboard completedStoryboard = (Storyboard)sender;
Bomb completedBomb = bombs[completedStoryboard];
// Determine if a bomb fell or flew off the Canvas after being clicked.
if (completedBomb.IsFalling)
{
droppedCount++;
}
else
{
savedCount++;
}
// Update the display.
lblStatus.Text = String.Format("You have dropped {0} bombs and saved {1}.",
droppedCount, savedCount);
// Clean up.
completedStoryboard.Stop();
canvasBackground.Children.Remove(completedBomb);
// Update the tracking collections.
storyboards.Remove(completedBomb);
bombs.Remove(completedStoryboard);
// Check if it's game over.
if (droppedCount >= maxDropped)
{
bombTimer.Stop();
lblStatus.Text += "\r\n\r\nGame over.";
// Find all the storyboards that are underway.
foreach (KeyValuePair<Bomb, Storyboard> item in storyboards)
{
Storyboard storyboard = item.Value;
Bomb bomb = item.Key;
storyboard.Stop();
canvasBackground.Children.Remove(bomb);
}
// Empty the tracking collections.
storyboards.Clear();
bombs.Clear();
// Allow the user to start a new game.
cmdStart.IsEnabled = true;
}
}
```
### 1.3 游戏优化方向
- **炸弹爆炸效果动画**:可以让炸弹周围的火焰闪烁,或让小块弹片在画布上飞舞。
- **背景动画**:创建一个向上移动的线性渐变,营造运动的感觉,或在两种颜色之间过渡。
- **增加深度**:给炸弹设置不同的大小,较大的炸弹`ZIndex`更高,动画时间更短,使其下落更快,还可让炸弹部分透明。
- **添加音效**:使用适时的音效来突出炸弹爆炸或拯救炸弹的效果。
- **使用动画缓动**:为动画添加缓动函数,使炸弹下落、反弹或摆动更加自然。
- **微调参数**:提供更多可调整的参数,如炸弹的时间、轨迹和频率,增加随机性。
##
0
0
复制全文
相关推荐









