XNA 文本框循环 [英] XNA Textbox loop

查看:37
本文介绍了XNA 文本框循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So im trying to make a textbox ( well not exactly a textbox, just changing a spriteFont text ) on XNA 4.0 win game heres my code so far :

usernameVar.Draw(spriteBatch, newInputText);

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

That will draw newInputText string each frame

newInputText = username.update(mouse);

这将设置字符串,但这是我的问题

That will set the string but heres my problem

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 语句之外的内容?

This textbox.cs file is giving me some errors first what should I do to avoid returning something outside the IF statement ?

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 会破坏我的文本框(我无法向 spritefont 添加空文本)此外,如果我删除 return null 添加 return "something" 我会在 set 属性上收到此错误

Since that return null breaks me textbox ( i cant add null text to the spritefont ) Also If I remove the return null adding return "something" I get this error on the set propertie

An unhandled exception of type 'System.StackOverflowException'

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

Sorry for this, im very new to C# and all this stuff, thanks

推荐答案

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

I'm not sure of the exact structure of your project, and I'm not sure the reason for the newText class, but the property that it contains called itself, over, and over, and over again.

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.

When you get or set newtext, it will get or set itself over and over again, resulting in a recursive loop. This will never end and will lead to a StackOverflowException.

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

The proper way to use a property is to have the public accessor (NewText) that will perform logic (in this case, just get and set) and return or set a value, in this case, the storage variable newText

这是一个例子:

private string newText; //Storage Field

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

C# 3.0 具有自动属性,所以这不一定必要的 (:P).

C# 3.0 has automatic properties, so this is not necessarily necessary (:P).

作为补充说明,您不必使用 String 类,stringString同样的事情,但使用string通常是首选方法.

As an added note, you do not have to use the String class, string and String are the same thing, but using string is usually the preferred method.

这篇关于XNA 文本框循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆