程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#開發WPF/Silverlight動畫及游戲系列教程(Game Tutorial):(四十三)(4)

C#開發WPF/Silverlight動畫及游戲系列教程(Game Tutorial):(四十三)(4)

編輯:關於C語言

當鼠標在右鍵快捷按鈕圖標上點擊時,我將availableMagic中的所有對象以圖標的形式添加進名為rightButtonMagicList的WrapPanel中,由於此Demo中圖標的寬高均為27像素,因此我約束rightButtonMagicList寬為270,即一行如果超過10個圖標則自動換行。最後為它賦予相應的事件,當點中某個魔法技能,主角當前的右鍵默認魔法更改為此魔法,且右鍵快捷按鈕圖標的魔法也換成它:

……
//右鍵魔法選擇
rightButtonMagicIcon.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) {
 if (rightButtonMagicList == null) {
  rightButtonMagicList = new WrapPanel() {
   Orientation = OrIEntation.Horizontal,
  Width = 270, //一行放10個圖標,每個27寬};
  foreach (Magic magic in availableMagic) {
   QXIcon magicIcon = new QXIcon(IconTypes.Highlights) {
                             BodySource = new ImageBrush() { ImageSource = Super.GetImage(string.Format("/Image/Magic/{0}/0.png", magic.Code)) },
   NewSource = new ImageBrush() { ImageSource = Super.GetImage("/Image/Icon/34.png") },
   Width = 27,
   Height = 27,
   Tip = magic.Name,
   Tag = magic,
  };
   magicIcon.MouseLeftButtonDown += delegate(object ss, MouseButtonEventArgs ee) {
    QXIcon icon = ss as QXIcon;
    Magic m = icon.Tag as Magic;
    rightButtonMagicIcon.BodySource = icon.BodySource;
    rightButtonMagicIcon.Tip = m.Name;
    Leader.CurrentMagic.Code = m.Code;
    Leader.CurrentMagic.Level = m.Level;
    rightButtonMagicList.Visibility = Visibility.Collapsed;
    ee.Handled = true; };
   rightButtonMagicList.Children.Add(magicIcon);
   }
   BottomMenu.Children.Add(rightButtonMagicList);
   Canvas.SetLeft(rightButtonMagicList, 437); Canvas.SetTop(rightButtonMagicList, -Math.Ceiling(rightButtonMagicList.Children.Count / (rightButtonMagicList.Width / 27)) * 27);
 } else {
    if (rightButtonMagicList.Visibility == Visibility.Collapsed) {
      rightButtonMagicList.Visibility = Visibility.Visible;
      Canvas.SetTop(rightButtonMagicList, -rightButtonMagicList.ActualHeight);
    } else {
      rightButtonMagicList.Visibility = Visibility.Collapsed;
    }
 }
 e.Handled = true;
……

具體邏輯大家仔細看就明白了~還算比較簡單的。實際效果如下:

另外的,大家不妨自行修改一下循環代碼,多加幾個魔法上去體驗一下WrapPanel的強大:

游戲主界面菜單已初具雛形,接下來的任務就是去完善它的各面板及相應功能~敬請關注。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved