Windows 8 系列 --Block Game --随笔
定义方块的类:
View Code
public class Block : Canvas { private Uri _baseUri = new Uri("ms-appx:///"); /// <summary> /// 宝石的图片 /// </summary> public string ImageSource { set { BitmapImage bitmap = new BitmapImage(new Uri(_baseUri, value)); ImageBrush ib = new ImageBrush(); ib.ImageSource = bitmap; this.Background = ib; } } private int _type; public int Type { get { return _type; } set { this._type = value; this.ImageSource = string.Format("Images/{0}/{0}_{1}.png", value, 1); } } DispatcherTimer dispactherTimer = new DispatcherTimer(); /// <summary> /// 当前播放帧数 /// </summary> int count = 1; private int _column; private int _row; public int Column { get { return _column; } set { _column = value; Grid.SetColumn(this, value); } } public int Row { get { return _row; } set { _row = value; Grid.SetRow(this, value); } } public bool isMouseLeftButtonDown { get; set; } public Block(int type, int column, int row) { this.Type = type; this.Column = column; this.Row = row; this.PointerPressed += Block_PointerPressed; this.PointerReleased += Block_PointerReleased; this.PointerEntered += Block_PointerEntered; this.PointerExited += Block_PointerExited; isMouseLeftButtonDown = false; dispactherTimer.Tick += dispactherTimer_Tick; dispactherTimer.Interval = TimeSpan.FromMilliseconds(25); } public Block(int column, int row) { this.Column = column; this.Row = row; this.Type = 0; } void dispactherTimer_Tick(object sender, object e) { BitmapImage bitmap = new BitmapImage(new Uri(_baseUri, string.Format("Images/{0}/{0}_{1}.png", Type, count))); ImageBrush ib = new ImageBrush(); ib.ImageSource = bitmap; this.Background = ib; count = count == 20 ? 1 : count + 1; } void Block_PointerExited(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) { RollStop(); } void Block_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) { Roll(); } void Block_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) { isMouseLeftButtonDown = false; this.ReleasePointerCapture(e.Pointer); } void Block_PointerPressed(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) { isMouseLeftButtonDown = true; this.ReleasePointerCapture(e.Pointer); } public void Hint() { Roll(); } private void Roll() { dispactherTimer.Start(); } private void RollStop() { dispactherTimer.Stop(); count = 1; this.Type = this.Type; } }
画布Xaml:
View Code
<UserControl x:Class="Win8_Bejeweled.Controls.BlockMainGame" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Win8_Bejeweled.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Width="512" Height="512"> <UserControl.Resources> <Storyboard x:Name="sbAll" Completed="sbAll_Completed_1"> </Storyboard> </UserControl.Resources> <Grid x:Name="LayoutRoot" PointerPressed="LayoutRoot_PointerPressed_1" PointerMoved="LayoutRoot_PointerMoved_1" PointerReleased="LayoutRoot_PointerReleased_1" > <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.Background> <!-- 注意路劲前面加 /--> <ImageBrush ImageSource="/Images/board.png" /> </Grid.Background> </Grid> </UserControl>
cs:
View Code
public sealed partial class BlockMainGame : UserControl { /// <summary> /// 动画是否正在播放 /// </summary> public bool AnimationIsPlaying { get; set; } /// <summary> /// 点击坐标 /// </summary> private PointerPoint mouseUpBeforePoint; /// <summary> /// 宝石矩阵 /// </summary> public Block[,] blocks = new Block[MaxColumn, MaxRow]; const int MaxColumn = 8; const int MaxRow = 8; /// <summary> /// 更新分数 /// </summary> public Action<int> UpdateScore; /// <summary> /// 获得分数 /// </summary> public Func<int> GetScore; Random random = new Random(); public Block this[int column, int row] { get { return blocks[column, row]; } set { if (value != null) { value.Column = column; value.Row = row; blocks[column, row] = value; if (!this.LayoutRoot.Children.Contains(value)) { this.LayoutRoot.Children.Add(value); } } else { blocks[column, row] = null; } } } public BlockMainGame() { this.InitializeComponent(); } public void InitGame() { this.Visibility = Visibility.Collapsed; for (int row = 0; row < MaxRow; row++) { for (int column = 0; column < MaxColumn; column++) { int type = GetRandomNum(); Block block = new Block(type, column, row); this[column, row] = block; } } #region 替换可以消除的石头 while (true) { var nearBlock = GetErasableBlock(); if (nearBlock != null) { foreach (Block block in nearBlock) { int type = GetRandomNum(); this.LayoutRoot.Children.Remove(block); this[block.Column, block.Row] = new Block(type, block.Column, block.Row); } } else { break; } } #endregion #region 初始化动画 Storyboard sbDown = new Storyboard(); Resources.Add("sbDown", sbDown); sbDown.Completed += (object sender, object e) => { var sb = sender as Storyboard; sb.Stop(); Resources.Remove("sbDown"); foreach (var item in blocks) { item.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); } AnimationIsPlaying = false; }; for (int column = 0; column < MaxColumn; column++) { for (int row = 0; row < MaxRow; row++) { Block block = this[column, row]; block.RenderTransform = new TranslateTransform(); block.RenderTransform.SetValue(TranslateTransform.YProperty, -1024.0); AddAnimationToStoryboard(sbDown, block.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(2500 - row * 200)); } } this.Visibility = Visibility.Visible; BeginAnimation(sbDown); #endregion } public int GetRandomNum() { long tick = DateTime.Now.Ticks; Random r = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); return r.Next(1, 8); } private void sbAll_Completed_1(object sender, object e) { Storyboard sb = sender as Storyboard; sb.Stop(); AnimationIsPlaying = false; } /// <summary> /// 向Storyboard添加动画 /// </summary> /// <param name="dobj">动画目标</param> /// <param name="property">被改变的值</param> /// <param name="to">动画完毕后的最终值</param> /// <param name="Duration">动画持续时间</param> /// <param name="eh">动画的完成事件处理程序</param> public void AddAnimationToStoryboard(DependencyObject dobj, string property, double to, TimeSpan Duration, EventHandler<object> eh) { DoubleAnimation da = new DoubleAnimation(); Storyboard.SetTarget(da, dobj); Storyboard.SetTargetProperty(da, property); da.To = to; da.Duration = new Duration(Duration); da.Completed += eh; sbAll.Children.Add(da); } /// <summary> /// 向Storyboard添加动画 /// </summary> /// <param name="dobj">动画目标</param> /// <param name="property">被改变的值</param> /// <param name="to">动画完毕后的最终值</param> /// <param name="Duration">动画持续时间</param> public void AddAnimationToStoryboard(DependencyObject dobj, string property, double to, TimeSpan Duration) { DoubleAnimation da = new DoubleAnimation(); Storyboard.SetTarget(da, dobj); Storyboard.SetTargetProperty(da, property); da.To = to; da.Duration = new Duration(Duration); sbAll.Children.Add(da); } /// <summary> /// 向Storyboard添加动画 /// </summary> /// <param name="sb">目标Storyboard</param> /// <param name="dobj">动画目标</param> /// <param name="property">被改变的值</param> /// <param name="to">动画完毕后的最终值</param> /// <param name="Duration">动画持续时间</param> /// <param name="eh">Storyboard完成事件处理程序</param> public void AddAnimationToStoryboard(Storyboard sb, DependencyObject dobj, string property, double to, TimeSpan Duration) { DoubleAnimation da = new DoubleAnimation(); Storyboard.SetTarget(da, dobj); Storyboard.SetTargetProperty(da, property); da.To = to; da.Duration = new Duration(Duration); sb.Children.Add(da); } /// <summary> /// 开始动画,使用默认的sbAll /// </summary> public void BeginAnimation() { AnimationIsPlaying = true; sbAll.Begin(); } /// <summary> /// 开始指定的Storyboard动画 /// </summary> public void BeginAnimation(Storyboard sb) { AnimationIsPlaying = true; sb.Begin(); } /// <summary> /// 获得可以被消除的宝石列表 /// </summary> /// <returns>可以被消除的宝石列表,返回值为null表示不可消除</returns> public List<Block> GetErasableBlock() { List<Block> columnSameBlock = new List<Block>(); List<Block> rowSameBlock = new List<Block>(); #region 检测横着排列的是否可消除并保存可消除的宝石到columnSameBlock for (int row = 0; row < MaxRow; row++) { int sameNum = 0; bool find = false; for (int column = 0; column < MaxColumn; column++) { while (true) { if (column < MaxColumn - 1 && this[column, row].Type == this[column + 1, row].Type) { sameNum++; column++; } else { if (sameNum >= 2) { for (; sameNum >= 0; sameNum--) { columnSameBlock.Add(this[column - sameNum, row]); } find = true; break; } else { sameNum = 0; } break; } } if (find) break; } if (find) break; } #endregion #region 检测竖着排列的是否可消除并保存可消除的宝石到rowSameBlock for (int column = 0; column < MaxColumn; column++) { int sameNum = 0; bool find = false; for (int row = 0; row < MaxRow; row++) { while (true) { if (row < MaxRow - 1 && this[column, row].Type == this[column, row + 1].Type) { sameNum++; row++; } else { if (sameNum >= 2) { for (; sameNum >= 0; sameNum--) { rowSameBlock.Add(this[column, row - sameNum]); } find = true; break; } else { sameNum = 0; } break; } } if (find) break; } if (find) break; } #endregion //if (columnSameBlock.Count >= 3) //{ // Block lastBlock = null; // foreach (var tempBlock in columnSameBlock) // { // if (lastBlock == null) // { // lastBlock = tempBlock; // } // if (tempBlock.Row != lastBlock.Row) // { // columnSameBlock.Clear(); // break; // } // lastBlock = tempBlock; // } //} if (rowSameBlock.Count >= 3) { foreach (var tempBlock in columnSameBlock) { if (tempBlock.Column == rowSameBlock[0].Column) { columnSameBlock.Clear(); break; } } } #region 合并两个集合并返回 if (columnSameBlock.Count >= 3 || rowSameBlock.Count >= 3) { List<Block> returnBlocks = new List<Block>(); foreach (var tempBlock in columnSameBlock) { returnBlocks.Add(tempBlock); } foreach (var tempBlock in rowSameBlock) { if (!columnSameBlock.Contains(tempBlock)) //不允许重复添加 { returnBlocks.Add(tempBlock); } } return returnBlocks; } else { return null; } #endregion } /// <summary> /// 调换两个Block的位置 /// </summary> /// <param name="block1">第一个Block</param> /// <param name="block2">第二个Block</param> public void ExChangeLocation(Block block1, Block block2) { int block1Column = block1.Column; int block1Row = block1.Row; int block2Column = block2.Column; int block2Row = block2.Row; this[block1Column, block1Row] = block2; this[block2Column, block2Row] = block1; } /// <summary> /// 让第一个Block的Zindex值比第二个大 /// </summary> /// <param name="addedBlock">被加Zindex值的Block</param> /// <param name="referenceBlock">参考值</param> public void AddingZIndex(Block addedBlock, Block referenceBlock) { Canvas.SetZIndex(addedBlock, Canvas.GetZIndex(referenceBlock) + 1); } /// <summary> /// 获取指定Block的指定方向的相邻Block /// </summary> /// <param name="movedBlock">指定的Block</param> /// <param name="direction">方向</param> /// <returns>相邻的Block</returns> public Block GetBlockByDirection(Block movedBlock, Direction direction) { Block returnBlock = null; switch (direction) { case Direction.Up: if (movedBlock.Row != 0) { returnBlock = this[movedBlock.Column, movedBlock.Row - 1]; } break; case Direction.Down: if (movedBlock.Row != MaxRow - 1) { returnBlock = this[movedBlock.Column, movedBlock.Row + 1]; } break; case Direction.Left: if (movedBlock.Column != 0) { returnBlock = this[movedBlock.Column - 1, movedBlock.Row]; } break; case Direction.Right: if (movedBlock.Column != MaxColumn -1) { returnBlock = this[movedBlock.Column + 1, movedBlock.Row]; } break; } return returnBlock; } /// <summary> /// 从LayoutRoot.Children中移除Block列表 /// </summary> /// <param name="Block">要移除的Block列表</param> public void RemoveListBlock(List<Block> removeblock) { Storyboard sb = new Storyboard(); sb.Completed += (object sender, object e) => { var sb2 = sender as Storyboard; sb.Stop(); AnimationIsPlaying = false; foreach (var block in removeblock) { block.Opacity = 0.0; this.LayoutRoot.Children.Remove(block); this[block.Column, block.Row] = null; } Resources.Remove("RemoveBlock"); FillNull(); UpdateScore(45 + (removeblock.Count - 3) * 45); }; Resources.Add("RemoveBlock", sb); foreach (var item in removeblock) { AddAnimationToStoryboard(sb, item, "Opacity", 0.0, TimeSpan.FromMilliseconds(200)); } BeginAnimation(sb); } private void FillNull() { List<Block> changedBlock = new List<Block>(); Storyboard sb = new Storyboard(); sb.Completed += (object sender, object e) => { var sb2 = sender as Storyboard; sb2.Stop(); AnimationIsPlaying = false; foreach (var tempBlock in changedBlock) { tempBlock.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); } Resources.Remove("sbFill"); var erasableBlocks = GetErasableBlock(); if (erasableBlocks != null) { RemoveListBlock(erasableBlocks); } }; Resources.Add("sbFill", sb); for (int i = 0; i < MaxColumn; i++) { int nullNum = 0; int row = 0; for (int j = 0; j < MaxRow; j++) { if (this[i, j] == null) { if (j > row) row = j; nullNum++; } } for (int k = row - nullNum; k >= 0 && nullNum != 0; k--) //添加被删除Block上面的Block下落动画 { DoubleAnimation da = new DoubleAnimation(); Block tempBlock = this[i, k]; tempBlock.RenderTransform = new TranslateTransform(); tempBlock.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBlock.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBlock.Column, tempBlock.Row + nullNum] = tempBlock; changedBlock.Add(tempBlock); } for (int l = nullNum - 1; l >= 0 && nullNum != 0; l--) //创建新的Block填充已消除的 { int randomNum = GetRandomNum(); Block tempBlock = new Block(randomNum, i, l); tempBlock.RenderTransform = new TranslateTransform(); tempBlock.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBlock.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBlock.Column, tempBlock.Row] = tempBlock; changedBlock.Add(tempBlock); } } BeginAnimation(sb); } /// <summary> /// 将指定的宝石向上移动一格 /// </summary> /// <param name="Block">要移动的宝石</param> public void Up(Block block, bool isRollBack) { var near = GetBlockByDirection(block, Direction.Up); this.ExChangeLocation(block, near); block.RenderTransform = new TranslateTransform(); block.RenderTransform.SetValue(TranslateTransform.YProperty, 64.0); near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.YProperty, -64.0); EventHandler<object> neareh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); }); EventHandler<object> blockeh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); var nearableBlocks = GetErasableBlock(); if (nearableBlocks == null) { Up(near, false); } else { RemoveListBlock(nearableBlocks); } }); this.AddAnimationToStoryboard(near.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300), neareh); if (isRollBack) { this.AddAnimationToStoryboard(block.RenderTransform, "Y", 0.0,TimeSpan.FromMilliseconds(300), blockeh); } else { this.AddAnimationToStoryboard(block.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300), neareh); } this.BeginAnimation(); } /// <summary> /// 将指定的宝石向下移动一格 /// </summary> /// <param name="Block">要移动的宝石</param> public void Down(Block block, bool isRollBack) { var near = GetBlockByDirection(block, Direction.Down); this.ExChangeLocation(block, near); AddingZIndex(block, near); block.RenderTransform = new TranslateTransform(); block.RenderTransform.SetValue(TranslateTransform.YProperty, -64.0); near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.YProperty, 64.0); EventHandler<object> neareh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); }); EventHandler<object> blockeh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); var nearableBlocks = GetErasableBlock(); if (nearableBlocks == null) { Down(near, false); } else { RemoveListBlock(nearableBlocks); } }); this.AddAnimationToStoryboard(near.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300), neareh); if (isRollBack) { this.AddAnimationToStoryboard(block.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300), blockeh); } else { this.AddAnimationToStoryboard(block.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300), neareh); } this.BeginAnimation(); } /// <summary> /// 将指定的宝石向上移动一格 /// </summary> /// <param name="Block">要移动的宝石</param> public void Left(Block block, bool isRollBack) { var near = GetBlockByDirection(block, Direction.Left); this.ExChangeLocation(block, near); AddingZIndex(block, near); block.RenderTransform = new TranslateTransform(); block.RenderTransform.SetValue(TranslateTransform.XProperty, 64.0); near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.XProperty, -64.0); EventHandler<object> neareh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); }); EventHandler<object> blockeh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); var nearableBlocks = GetErasableBlock(); if (nearableBlocks == null) { Left(near, false); } else { RemoveListBlock(nearableBlocks); } }); this.AddAnimationToStoryboard(near.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); if (isRollBack) { this.AddAnimationToStoryboard(block.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), blockeh); } else { this.AddAnimationToStoryboard(block.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); } this.BeginAnimation(); } /// <summary> /// 将指定的宝石向右移动一格 /// </summary> /// <param name="Block">要移动的宝石</param> public void Right(Block block, bool isRollBack) { var near = GetBlockByDirection(block, Direction.Right); this.ExChangeLocation(block, near); AddingZIndex(block, near); block.RenderTransform = new TranslateTransform(); block.RenderTransform.SetValue(TranslateTransform.XProperty, -64.0); near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.XProperty, 64.0); EventHandler<object> neareh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); }); EventHandler<object> blockeh = new EventHandler<object>((object sender, object e) => { var da = sender as DoubleAnimation; sbAll.Children.Remove(da); block.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); var nearableBlocks = GetErasableBlock(); if (nearableBlocks == null) { Right(near, false); } else { RemoveListBlock(nearableBlocks); } }); this.AddAnimationToStoryboard(near.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); if (isRollBack) { this.AddAnimationToStoryboard(block.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), blockeh); } else { this.AddAnimationToStoryboard(block.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); } this.BeginAnimation(); } /// <summary> /// 下一关 /// </summary> public void NextLevel() { #region 下一关动画 var sbLeave = new Storyboard(); Resources.Add("sbLeave", sbLeave); sbLeave.Completed += (object sender1, object ea1) => { var sb = sender1 as Storyboard; Resources.Remove("sbLeave"); sb.Stop(); this.LayoutRoot.Children.Clear(); this.InitGame(); AnimationIsPlaying = false; }; for (int column = 0; column < MaxColumn; column++) { for (int row = 0; row < MaxRow; row++) { var tempBlock = this[column, row]; tempBlock.RenderTransform = new TranslateTransform(); AddAnimationToStoryboard(sbLeave, tempBlock.RenderTransform, "Y", +700, TimeSpan.FromMilliseconds(2000 - row * 150)); } } BeginAnimation(sbLeave); #endregion } private void LayoutRoot_PointerPressed_1(object sender, PointerRoutedEventArgs e) { if (e.OriginalSource.GetType() == typeof(Block)) { this.mouseUpBeforePoint = e.GetCurrentPoint(LayoutRoot); Block tempBlock = (Block)e.OriginalSource; tempBlock.CapturePointer(e.Pointer); } } private void LayoutRoot_PointerMoved_1(object sender, PointerRoutedEventArgs e) { if (e.OriginalSource != null && e.OriginalSource.GetType() == typeof(Block) && !AnimationIsPlaying) { Block movedBlock = (Block)e.OriginalSource; if (movedBlock.isMouseLeftButtonDown) { PointerPoint mousePoint = e.GetCurrentPoint(LayoutRoot); double XCha = mousePoint.Position.X - mouseUpBeforePoint.Position.X; double YCha = mousePoint.Position.Y - mouseUpBeforePoint.Position.Y; double absXCha = Math.Abs(XCha); double absYCha = Math.Abs(YCha); #region 判断移动方向 if (absXCha > 10 || absYCha > 10) { if (XCha > 10 && YCha < 10) { if (absXCha > absYCha) { this.Right(movedBlock, true); } else { this.Up(movedBlock, true); } } else if (XCha > 10 && YCha > 10) { if (absXCha > absYCha) { this.Right(movedBlock, true); } else { this.Down(movedBlock, true); } } else if (XCha < 10 && YCha > 10) { if (absXCha > absYCha) { this.Left(movedBlock, true); } else { this.Down(movedBlock, true); } } else if (XCha < 10 && YCha < 10) { if (absXCha > absYCha) { this.Left(movedBlock, true); } else { this.Up(movedBlock, true); } } } #endregion } } } private void LayoutRoot_PointerReleased_1(object sender, PointerRoutedEventArgs e) { if (e.OriginalSource != null && e.OriginalSource.GetType() == typeof(Block)) { Block tempBlock = (Block)e.OriginalSource; tempBlock.ReleasePointerCapture(e.Pointer); } } } public enum Direction { Up = 1, Down = 2, Left = 3, Right = 4 }
学徒帮-jQuery帮帮帮
欢迎更多的前端交流、Js交流、jQuery交流