C # development Wpf/silverlight animation and games series (Game Tutorial): (40) to Silverlight transplant ②
Third, new features:
1 Add the game music and sound objects:
public static MediaElement gameMusic, gameAudio;
2 Add game mouse cursor:
//设置游戏鼠标光标
GameCursor.Stretch = Stretch.Fill;
GameCursor.Source = Super.GetImage("/Image/Cursor/0.png");
Updates the mouse cursor picture position in the mouse movement event, based on the hit test:
//鼠标移动(悬停)事件
private void Game_MouseMove(object sender, MouseEventArgs e) {
……
Point p = e.GetPosition(Root);
GameCursor.SetValue(Canvas.LeftProperty, p.X); GameCursor.SetValue(Canvas.TopProperty, p.Y);
……
}
This method realizes the mouse picture follows the cursor movement, but obviously affects the program overall performance (CPU consumption will increase obviously), if which friend can provide the better solution, looks the message to me.
3 Add click Water Droplets:
When the mouse left click on the screen, as long as it does not point to object objects, then display the cursor in the position of water droplets, and play its own animation. At present my Silverlight game engine uses only one cursor drop:
QXDecoration hitCursor;
/// <summary>
/// 加载光标点击水滴
/// </summary>
private void LoadHitCursor() {
hitCursor = new QXDecoration() {
Code = 1,
EndFrame = 9,
CenterX = 32,
CenterY = 32,
};
hitCursor.Visibility = Visibility.Collapsed;
Add(hitCursor);
}
//鼠标左键事件
private void Game_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
……
//出现光标水滴
hitCursor.Visibility = Visibility.Visible;
hitCursor.FrameCounter = 0;
hitCursor.Timer.Start();
hitCursor.Coordinate = p;
……
}