2013-08-16 41 views
0

我想制作一个简单的匹配游戏,以帮助更好地理解运动,其中我将具有形状轮廓和形状本身。将形状拖过轮廓并释放它以使其卡入到位。它听起来很简单。我能够使用ManipulationDelta事件来移动我的形状,但是由于某些原因,我无法获取任何要拖动的Drag事件(DragOver,DragEnter,Drop)。我已经阅读了这些事件,但也许我的理解是有缺陷的。我在寻找什么事件来了解何时一种形状被拖过另一种形状?如何在Windows 8应用程序中拖放移动?

XAML

<Canvas Name="DrawCanvas"> 
    <Ellipse Name="Shape1" Fill="SteelBlue" Height="200" Width="200" ManipulationMode="All" AllowDrop="True" DragOver="Shape1_DragOver" DragEnter="Shape2_DragEnter" Drop="Shape1_Drop"/> 
    <Ellipse Name="Shape2" Height="209" Width="209" Stroke="SteelBlue" StrokeThickness="5" AllowDrop="True" Canvas.Left="594" Canvas.Top="96" /> 
</Canvas> 

我试过在Shape1和Shape2不过的dragover,dragEnter事件,删除事件,他们似乎从来没有火的每个组合。这些事件不适用于形状吗?或者在使用ManipulationDelta进行移动时可能不起作用?

谢谢,我真的很感激任何帮助或方向。

+0

在Windows8中是否有AllowDrop属性?如果是这样,你应该将它设置为true以获得提及的事件。 – LunicLynx

+0

是的。在上面的示例中,我实际上将它设置为两种形状,并且有一次我甚至将其放在了Canvas本身上。 – jrandomuser

+0

这是否有帮助:http://msdn.microsoft.com/en-us/library/windows/apps/jj863492.aspx – LunicLynx

回答

1

答案是获得画布的边界,形状的大小和X,Y。从中你可以得到它周围的4个点(topLeft,topRight,bottomLeft,bottomRight)。当一个点超出您的边界时,将其设置在边界上。这有效地保持了“边界”的形状。

translation.X = e.Delta.Translation.X; 
translation.Y = e.Delta.Translation.Y 

// Since we are checking the left point subtract your shapes width from the canvas right 
// bounds. If X is greater than this set it instead to that maximum bound. 
if (translation.X > (canvasright - shape.Width)) 
    translation.X = canvasright - shape.Width; 

/// Same for top. If Y is less than the canvas top set it instead right at the boundary. 
if (translation.Y < canvastop) 
    translation.Y = canvastop; 

// Do the same for bottom and left 

也可以使用形状中心来做到这一点,根据您实现的功能可以提供优势。当使用中心时,形状的顶部或底部计算高度的一半,左侧和右侧是其宽度的一半。

相关问题