2013-08-16 36 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 

也可以使用形狀中心來做到這一點,根據您實現的功能可以提供優勢。當使用中心時,形狀的頂部或底部計算高度的一半,左側和右側是其寬度的一半。

相關問題