简体   繁体   English

Windows 8应用程序:“捕捉”拖放游戏

[英]Windows 8 App: “Snapping” Drag and Drop for Card Game

After trying to use the following snippet to move cards (Images right now) around I was not satisfied with the result. 尝试使用以下代码段移动卡(当前为图像)后,我对结果不满意。

Card.ManipulationDelta += (o, args) => {
    var dragableItem = o as Image;
    if (dragableItem == null) return;
    var translateTransform = dragableItem.RenderTransform as TranslateTransform;

    if (translateTransform == null) return;
    translateTransform.X += args.Delta.Translation.X;
    translateTransform.Y += args.Delta.Translation.Y;
};
Card.RenderTransform = new TranslateTransform();

The control had a funny behavior to be accelerated and would move / slide a bit after "dropping" it. 该控件有一个有趣的行为,可以加速,并且在“放下”它后会移动/滑动一点。 Although cool I do not want this behavior and therefore changed my mind: what I am looking for is a solution to define specific areas for one active card, a bench for a few more cards and stacks for the deck, such that one can freely drag one card but it can only be dropped if it is above these certain areas otherwise it will get back to the area designated for the hand cards. 尽管很酷,但我不希望这种行为,因此改变了主意:我正在寻找一种解决方案,为一个活动卡定义特定区域,为多个卡定义一个长凳,为甲板定义堆栈,以便一个人可以自由拖动一张卡片,但是只有在这些特定区域之上时才能将其丢弃,否则它将返回到为手动卡片指定的区域。

What could I try to implement this desired behavior? 我该如何尝试实现这种期望的行为?

I think you and I are in the same boat, I'm working on a Metro card game application and I'm not relly happy with what I've found as far as Drag and Drop goes. 我认为您和我在同一条船上,我正在开发Metro纸牌游戏应用程序,并且我对拖放的发现并不满意。 My inital approach was going to be to have a grid \\ stackpanel or other underlying framework that the user could drag a card image ( acually a custom control ) over and then the image would snap to that framework when the user let go. 我的初始方法是拥有一个网格\\ stackpanel或其他基础框架,用户可以将卡片图像(默认为自定义控件)拖动到该框架上,然后当用户放开该图像时,图像将捕捉到该框架。 I have not yet found a suitable way to obtain this behavior however because it seems like drag-drop of controls from one parent to another is not supported in Metro. 我还没有找到一种合适的方法来获得此行为,但是,因为Metro似乎不支持将控件从一个父对象拖放到另一个父对象。

As far as your question goes, the sliding effect that you are refering to is most likely intertia, You can disable this by not setting the TranslateInertia mode, for example 就您的问题而言,您所指的滑动效果很可能是Intertia,您可以通过不设置TranslateInertia模式来禁用它,例如

    Ellipse el = new Ellipse();
    el.Fill = new SolidColorBrush(Windows.UI.Colors.Magenta);
    el.ManipulationMode = (ManipulationModes.All ^ MainipulationModes.TranslateInteria);

    //more code to add your ManipulationDelta handler etc.

You can also gain some control over the Interia by adding a handler for MainipulationInertiaStarting, though just setting e.Handled = true in the handler does not disable interia entirely for me as others have suggested. 您还可以通过为MainipulationInertiaStarting添加处理程序来获得对Interia的某些控制,尽管仅在处理程序中设置e.Handled = true不会像其他人建议的那样完全禁用我的interia。

I'd love to hear back from you and see what approach you've come up with for snapping cards, at this point I'm considering just using a large Canvas object and writing my own customer handlers for mouse moved to get card objects to be draggable and then snap into a row or other location on the playing board. 我希望收到您的回音,看看您想出哪种方法来抓卡,这时我正在考虑仅使用一个大型Canvas对象并编写自己的客户处理程序以将鼠标移动到以获取卡对象可拖动,然后卡入游戏板上的一行或其他位置。

Cheers, 干杯,

James 詹姆士

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM