【问题标题】:XNA Textbox loopXNA 文本框循环
【发布时间】:2013-07-23 23:23:36
【问题描述】:

所以我试图在 XNA 4.0 win 游戏上制作一个文本框(不完全是一个文本框,只是更改一个 spriteFont 文本),这是我的代码:

usernameVar.Draw(spriteBatch, newInputText);

这将在每一帧绘制 newInputText 字符串

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;
    }

因为 return null 打破了我的文本框(我不能将 null 文本添加到 spritefont ) 此外,如果我删除返回 null 添加返回“某物”,我会在设置属性上收到此错误

An unhandled exception of type 'System.StackOverflowException'

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

【问题讨论】:

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

标签: c# textbox xna


【解决方案1】:

我不确定您项目的确切结构,也不确定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

使用property 的正确方法是让公共访问器 (NewText) 执行逻辑(在这种情况下,只需获取和设置)并返回或设置一个值,在这种情况下,存储变量newText

这是一个例子:

private string newText; //Storage Field

public string Newtext //Accessor
{
    get { return newText; }
    set { newText = value; }
}

C# 3.0 有 automatic properties,所以这不是必须的 (:P)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多