C# XNA文本框循环

C# XNA文本框循环,c#,textbox,xna,C#,Textbox,Xna,因此,我正在尝试在XNA 4.0 win游戏上制作一个文本框(也不完全是文本框,只是更改了一个spriteFont文本),以下是我目前的代码: usernameVar.Draw(spriteBatch, newInputText); 这将在每一帧中绘制新的InputText字符串 newInputText = username.update(mouse); 这将设置字符串,但这是我的问题 class Textbox { public Texture2D texture; pu

因此,我正在尝试在XNA 4.0 win游戏上制作一个文本框(也不完全是文本框,只是更改了一个spriteFont文本),以下是我目前的代码:

usernameVar.Draw(spriteBatch, newInputText);
这将在每一帧中绘制新的InputText字符串

newInputText = username.update(mouse);
这将设置字符串,但这是我的问题

class Textbox
{
    public Texture2D texture;
    public Rectangle rectangle;
    public bool isClicked;

    public Textbox(Texture2D newTexture, Rectangle newRectangle)
    {
        texture = newTexture;
        rectangle = newRectangle;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle, Color.White);
    }
    public String update(MouseState mouse)
    {
        Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
        var textboxText = new newText();

        if (mouseRectangle.Intersects(rectangle))
        {
            isClicked = true;
            if (isClicked)
            {
                textboxText.newtext = "a";
                Connection.sendPacketBool("ae", textboxText.newtext);
                return textboxText.newtext;  
            }
        }

        return null;
    }
}
class newText
{
    public String newtext
    {
        get 
        { 
            return this.newtext; 
        }
        set 
        { 
            this.newtext = value; 
        }
    }
}
这个textbox.cs文件给了我一些错误。首先,我应该做些什么来避免返回IF语句之外的内容

public String update(MouseState mouse)
    {
        Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
        var textboxText = new newText();

        if (mouseRectangle.Intersects(rectangle))
        {
            isClicked = true;
            if (isClicked)
            {
                textboxText.newtext = "a";
                Connection.sendPacketBool("ae", "a");
                return "yo";  
            }
        }

        return null;
    }
因为返回null会破坏我的文本框(我不能将null文本添加到spritefont) 另外,如果我删除了returnnull并添加return“something”,那么在set属性上会出现这个错误

An unhandled exception of type 'System.StackOverflowException'

很抱歉,我对C#和所有这些东西都很陌生,谢谢

我不确定您的项目的确切结构,也不确定创建
newText
类的原因,但它包含的属性一遍又一遍地调用它自己

class newText
{
    public String newtext
    {
        get 
        { 
            return this.newtext; //Get newtext again, which gets it again, and again, etc
        }
        set 
        { 
            this.newtext = value; //Set newtext, which calls set again, and again...
        }
    }
}
当您获取或设置
newtext
时,它会一次又一次地获取或设置自身,从而产生一个递归循环。这将永远不会结束,并将导致
StackOverflowException

使用a的正确方法是使用公共访问器(
NewText
),该访问器将执行逻辑(在本例中,仅获取和设置)并返回或设置一个值,在本例中为存储变量
NewText

以下是一个例子:

private string newText; //Storage Field

public string Newtext //Accessor
{
    get { return newText; }
    set { newText = value; }
}
C#3.0有,所以这不一定是必需的(:p)


作为补充说明,您不必使用
String
类,
String
String
是首选方法,但使用
String
通常是首选方法。

我不确定项目的确切结构,也不确定创建
newText
类的原因,但是它包含的属性一遍又一遍地调用它自己

class newText
{
    public String newtext
    {
        get 
        { 
            return this.newtext; //Get newtext again, which gets it again, and again, etc
        }
        set 
        { 
            this.newtext = value; //Set newtext, which calls set again, and again...
        }
    }
}
当您获取或设置
newtext
时,它会一次又一次地获取或设置自身,从而产生一个递归循环。这将永远不会结束,并将导致
StackOverflowException

使用a的正确方法是使用公共访问器(
NewText
),该访问器将执行逻辑(在本例中,仅获取和设置)并返回或设置一个值,在本例中为存储变量
NewText

以下是一个例子:

private string newText; //Storage Field

public string Newtext //Accessor
{
    get { return newText; }
    set { newText = value; }
}
C#3.0有,所以这不一定是必需的(:p)


作为补充说明,您不必使用
String
类,
String
String
是最常用的方法,但是使用
String
通常是首选方法。

当您有无限循环或递归时,会发生堆栈溢出。如何使用
Update()
方法?问题似乎不在方法内。当有无限循环或递归时,会发生堆栈溢出。如何使用
Update()
方法?问题似乎不在方法之内。