简体   繁体   English

如何防止图像恢复到原始位置?

[英]How do I prevent my image from returning to its original position?

I have the beginnings of a platformer phone app as I try to teach myself C# in conjunction with a class I am taking.. I have three game buttons, left, right, and jump. 我有一个平台电话应用程序的开始,当时我尝试结合自己正在上的课来教自己C#。我有三个游戏按钮,向左,向右和跳转。 The buttons all preform their intended function but for some reason whenever I click a button the image(player aka "toon") will jump back to its origin location. 这些按钮全部执行其预期的功能,但是由于某些原因,每当我单击按钮时,图像(播放器又称为“卡通”)都会跳回到其原始位置。 The second time I click the same button the action is performed correctly, but when I switch buttons it jumps again. 第二次单击同一按钮时,操作正确执行,但是当我切换按钮时,它又跳了一次。

A final note, the exception to this is the left and right, once I press either button and get the return to origin jump, both left and right will work correctly. 最后要说明的是,左,右是一个例外,一旦按下任一按钮并返回原点跳转,则左和右都将正常工作。

Hope the code is easy enough to read. 希望代码足够容易阅读。 I feel like is should be a simple fix, but I'm new. 我觉得应该是一个简单的解决方法,但是我是新手。

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        //should probably be Event Args not routed
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Toon.Margin = new Thickness(62, Toon.Margin.Top - 15, 0,0);
            Toon.Margin = new Thickness(Toon.Margin.Left, Toon.Margin.Top, Toon.Margin.Right, Toon.Margin.Bottom);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Toon.Margin = new Thickness(Toon.Margin.Left + 15, 102, 0, 0);
            Toon.Margin = new Thickness(Toon.Margin.Left, Toon.Margin.Top, Toon.Margin.Right, Toon.Margin.Bottom);
        }

        //moving left
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            (Toon.Margin.Left,Toon.Margin.Top,Toon.Margin.Right,Toon.Margin.Bottom);
            Toon.Margin = new Thickness(Toon.Margin.Left - 15, 102, 0, 0);
            Toon.Margin = new Thickness(Toon.Margin.Left, Toon.Margin.Top, Toon.Margin.Right, Toon.Margin.Bottom);
        }
    }
}

The problem with your code is that you are mixing absolute values with values relative to your previous position. 代码的问题是,您要混合绝对值和相对于先前位置的值。 So, for example, if you want to move your image to the right, only change that value and leave all the others the same: 因此,例如,如果您想将图像向右移动,只需更改该值并使所有其他值保持不变:

Toon.Margin = new Thickness(Toon.Margin.Left + 15, Toon.Margin.Top, Toon.Margin.Right, Toon.Margin.Bottom);

The values, Toon.Margin.Top, Toon.Margin.Left, etc. are the values that the image is currently assigned. Toon.Margin.Top,Toon.Margin.Left等值是图像当前分配的值。 This also means that the following line does nothing at all: 这也意味着以下行什么都不做:

Toon.Margin = new Thickness(Toon.Margin.Left, Toon.Margin.Top, Toon.Margin.Right, Toon.Margin.Bottom);

It simply assigns the image values which it already has. 它只是分配它已经具有的图像值。 If you want to create a "Jump" function, you can use all absolute values to set the image to a specific position, like so: 如果要创建“跳转”功能,则可以使用所有绝对值将图像设置到特定位置,如下所示:

Toon.Margin = new Thickness(0,0,0,0)    // replace the 0s with numbers you like

As a final note, you shouldn't name your functions Button_Click_1 and so on, as it makes it hard to figure out what this function does. 最后一点,您不应该将函数的名称命名为Button_Click_1等等,因为这样很难弄清楚该函数的作用。 Instead name it something like Button_Click_Move_Right . 而是将其命名为Button_Click_Move_Right

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

相关问题 如何防止 C# 将临时路径作为原始路径的前缀? - How do I prevent C# from prefixing the temporary path to my original path? 如何防止控制台应用程序在满足条件之前关闭? - How do I prevent my console app from closing until its condition is met? 如何防止ContextMenu限制在其容器中(Win Forms c#) - How do I prevent my ContextMenu from being constrained to its container (Win Forms c#) 如何防止粘贴的图像溢出其边界? - How can I prevent the pasted image from overflowing its boundaries? 如何防止`DateTime.ParseExact();`返回时间 - How do I prevent `DateTime.ParseExact();` from returning time 将翻转的图像恢复为其原始视图 - Returning flipped image to its original view 显示另一张表格时,如何防止原始表格失去焦点? - How do I prevent the original form from losing focus when I show another form? 如何显示和定位在页面代码背后初始化的图像? - How do I show and position an Image initialized in the codebehind on my page? 如何从原始设计器设置中获取控件的 position 和大小? - How do I get a control's position and size from the original designer settings? 如何从列左侧的 ListView 中 position 图像? - How do I position the Image from the ListView at the left from the column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM