[英]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.