简体   繁体   中英

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. 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. 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. Instead name it something like Button_Click_Move_Right .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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