1
\$\begingroup\$

This piece of code is returning on a infinite base how to fix?

public GuiElement MoveElement(int x, int y)
{
    GUIRect = new Rectangle(GUIRect.X += x,GUIRect.Y += y,GUIRect.Width,GUIRect.Height);
    return MoveElement(x,y);
}

this is where i set the values i have in my Other class

        gm.Add(new GuiElement("BackGround"));      

        gm.Add(new GuiElement("Play_Button").MoveElement(150, 50));

        gm.Add(new GuiElement("HighScore_Button"));

        gm.Add(new GuiElement("Quit_Button"));

basicly i want a to be able to move some elements using that MoveElement

it creates a infinite loop

An unhandled exception of type 'System.StackOverflowException' occurred in giftman.exe

\$\endgroup\$
6
  • \$\begingroup\$ What's the base case for your recursion? What's the code supposed to do? Please don't revert the edits again. \$\endgroup\$
    – House
    Commented Feb 16, 2016 at 19:01
  • \$\begingroup\$ its supposed to return some values i have set in another another class \$\endgroup\$ Commented Feb 16, 2016 at 19:02
  • \$\begingroup\$ And the base case for the recursion? You can edit your question to include additional information. \$\endgroup\$
    – House
    Commented Feb 16, 2016 at 19:02
  • \$\begingroup\$ I don't understand the need for recursion in this case. For your statement to work, your method would have to return this; \$\endgroup\$
    – House
    Commented Feb 16, 2016 at 19:08
  • \$\begingroup\$ i might have missunderstood it however it creates a infinite loop which is what i am trying to get around \$\endgroup\$ Commented Feb 16, 2016 at 19:11

1 Answer 1

3
\$\begingroup\$

You don't need to use recursion for this (and without a base case, you'll get an infinite loop, like you are).

Just change your method to:

public GuiElement MoveElement(int x, int y)
{
    GUIRect = new Rectangle(GUIRect.X += x,GUIRect.Y += y,GUIRect.Width,GUIRect.Height);
    return this;
}

Or better yet, add a constructor to set the position of your GuiElement, so you can just call it like this:

gm.Add(new GuiElement("Play_Button", 150, 50));

Or you can just change your MoveElement method to not return anything at all:

public void MoveElement(int x, int y)
{
    GUIRect = new Rectangle(GUIRect.X += x,GUIRect.Y += y,GUIRect.Width,GUIRect.Height);
}

Then when creating it you can do this:

GuiElement playButton = new GuiElement("Play_Button");
playButton.MoveElement(150, 50);
gm.Add(playButton);
\$\endgroup\$
5
  • \$\begingroup\$ that seems to work however it doesnt move it now wierd \$\endgroup\$ Commented Feb 16, 2016 at 19:40
  • \$\begingroup\$ Depending on how you have things set up, you'll likely have to reinitialize some aspects of the GuiElement, since all you're doing in MoveElement is creating a new rectangle. For example, the renderer might only look at the rectangle once at initialization, and not update itself if the rectangle is replaced. But that's something that'll be specific to your code. \$\endgroup\$
    – House
    Commented Feb 16, 2016 at 19:42
  • \$\begingroup\$ no i am pretty sure i have solved the loading stuff but it just doesnt want to move at any point \$\endgroup\$ Commented Feb 16, 2016 at 20:13
  • \$\begingroup\$ You can ask a new question about that if you like. I believe this question was only about avoiding the infinite loop. \$\endgroup\$
    – House
    Commented Feb 16, 2016 at 20:19
  • \$\begingroup\$ nvm i found out so i mark this as solved and btw thanks. i needed to use Find assetname because i need to load in the location when it finds the specific button \$\endgroup\$ Commented Feb 16, 2016 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .