none
C# - Need help with variables RRS feed

  • Question

  • How can i use the original value of x1 in the second while loop? Any way to reset the variable to it´s original state?

    Game.ShowSceneAndAddTurtle();
                double x1 = Game.SceneBounds.Left;
                double x2 = Game.SceneBounds.Right;
                double y1 = Game.SceneBounds.Top;
                double y2 = Game.SceneBounds.Bottom;

                Turtle.Say("Horizontal saw");

                Turtle.MoveTo(x1,0);
                Turtle.TurnOnPen();
                Turtle.SetPenColor(RGBAColors.Red);

                while (x1 < x2)
                {
                    Turtle.MoveBy(50, 50);
                    Turtle.MoveDown(50);
                    x1 = x1 + 50;
                }
                Turtle.Say($"{x1}");
                Turtle.TurnOffPen();
                Turtle.MoveToCenter();
                Turtle.Sleep(5000);
                Game.ClearScene();

                Turtle.MoveTo(0, y1);
                Turtle.TurnOnPen();

                while (y1 > y2)
                {
                    Turtle.MoveBy(50, -50);
                    Turtle.MoveLeft(50);
                    y1 = y1 - 50;
                }
                y1 = y1 * -1;
                Turtle.TurnOffPen();
                Turtle.MoveToCenter();
                Turtle.Sleep(5000);
                Game.ClearScene();

                
    Wednesday, March 25, 2020 9:55 PM

All replies

  • Greetings Elgrym.

    I'm not sure what you mean, because the second loop does not use x1. In general, I think you are looking for something like this.

    double startingX = Game.SceneBounds.Left;
    
    // Start at the beginning.
    double x1 = startingX;
    while(whatever)
    {
       // Code goes here.
    }
    
    // More code here.
    
    // Go back to the start.
    x1 = startingX;
    while(somethingElse)
    {
       // And so on.
    }

    Wednesday, March 25, 2020 10:18 PM
  • Hi Elgrym,

    You can store x1 value into another variable to hold x1 value for example 

    double x1 = Game.SceneBounds.Left;
    double x1OrginalValue = x1;


     
    
    //Before second  While loop 
    
    x1 = x1OriginalValue; 
    
    while (y1 > y2)
                {
                    Turtle.MoveBy(50, -50);
                    Turtle.MoveLeft(50);
                    y1 = y1 - 50;
    
    
    
    //Use x1 inside this loop
    
    
                }


    Please remember to mark the replies as answers if they helped you :) ~

    Wednesday, March 25, 2020 10:23 PM
  • The two existing responses say essentially the same thing, posted essentially in parallel. If they do not help then you need to clarify the question.


    Sam Hobbs
    SimpleSamples.Info


    Wednesday, March 25, 2020 11:41 PM
  • Hi Elgrym,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 9, 2020 5:41 AM