嗨,大家好,我正在開發一款遊戲,並且希望添加一個屏幕,當用戶點擊某些對象時會打開屏幕。屏幕將顯示在左側的圖像,右上角的標題和標題下的文字。XNA彈出文本對話框
我有這幾個問題, 1)我將如何創建彈出框? (優選具有圓形邊緣) 2)如何將換行文本(圖中的前部,而不是在彈出框外,我甚至可能需要垂直滾動條) 3)如何將說明不同的屏幕分辨率(將他們甚至有所作爲?)
感謝您的任何和所有幫助, 最大
嗨,大家好,我正在開發一款遊戲,並且希望添加一個屏幕,當用戶點擊某些對象時會打開屏幕。屏幕將顯示在左側的圖像,右上角的標題和標題下的文字。XNA彈出文本對話框
我有這幾個問題, 1)我將如何創建彈出框? (優選具有圓形邊緣) 2)如何將換行文本(圖中的前部,而不是在彈出框外,我甚至可能需要垂直滾動條) 3)如何將說明不同的屏幕分辨率(將他們甚至有所作爲?)
感謝您的任何和所有幫助, 最大
圓邊等最好是通過使你想要的彈出是什麼樣子的形象和其樣式,你想怎麼處理。
可以說,你300像素
然後從那裏,你會想包裝內的文字,並可能有它滾動,或做了個盒子,300像素,但是你想通過檢查文本高度和寬度來處理它找到在哪裏自動換行...
例如,你可以做這樣的功能,將採取你的全文,敷基於一些測量。
private string WrapText(string text)
{
string[] words = text.Split(' ');
StringBuilder sb = new StringBuilder();
float linewidth = 0f;
float maxLine = 250f; //a bit smaller than the box so you can have some padding...etc
float spaceWidth = spriteFont.MeasureString(" ").X;
foreach (string word in words)
{
Vector2 size = spriteFont.MeasureString(word);
if (linewidth + size.X < 250)
{
sb.Append(word + " ");
linewidth += size.X + spaceWidth;
}
else
{
sb.Append("\n" + word + " ");
linewidth = size.X + spaceWidth;
}
}
return sb.ToString();
}
如果你的文字是多大一從上到下視圖 - 你將不得不拿出某種形式的滾動文字,或「繼續」按鈕按下加載文本例如下一個頁面。
最後,只要解決方案進行,它取決於平臺,你想建立你的遊戲和藝術的平臺,例如一個對話框的300x300圖像可能不會很好的工作,如果這是你正在建設的東西XNA 4適用於手機,但對於Xbox版本來說可以,但在邏輯上最適合PC版本。雖然使用視口,你可能會使用相同的藝術(只是可能是值得的,使用更小的圖像爲一個更小的屏幕
爲解決方案類似的問題,你可能想看看像tile safe area一些事情,並閱讀了一下約viewports。它也建議您設置您的遊戲在1280×720這與720p原始分辨率,應該在所有的電視工作,併爲您提供您的藝術參考運行。
public Game1()
{
graphics = new GraphicsDeviceManager(this);
this.graphics.PreferredBackBufferWidth = 1280;
this.graphics.PreferredBackBufferHeight = 720;
Content.RootDirectory = "Content";
}
應該希望至少把你推向正確的方向
我沒碰過XNA多年,但我建議你看一看遊戲狀態樣品。現在可能過時了一點,但會給你管理你的遊戲,「彈出窗口」,菜單等的狀態,一個好的起點
http://creators.xna.com/en-US/samples/gamestatemanagement
這一點對於穩健我的遊戲,我只需要一個彈出窗口,它是遊戲的一部分。例如:你點擊一個房子,彈出窗口會出現一張房子的圖片,標題中的所有者以及房子的簡圖。 – Ben 2010-05-16 19:34:19
@Mazzzzz - XNA是相當低級的,沒有像其他窗口框架一樣的內置東西。因此,你必須自己構建大部分,或者可能找到已經完成的人。 @Chris指針是構建屏幕的好起點,你可以借用一些技術來構建遊戲中的「窗口」。 – 2010-05-16 20:56:58
你應該只考慮精靈批量對象,它處理的UI在遊戲狀態管理例子
對於任何看,h ere是我編寫的完整代碼,代碼有點像雞抓,但是它完成了工作。
不要注意我把裝載機fillText方法main函數中,你應該將這個外(所以它不會重複調用),如果您使用此代碼
public void DisplayPopup (string Title, string Text, string AssetPicturePath, SpriteBatch batch)
{
FillText = new Texture2D(game.GraphicsDevice, 1, 1);
FillText.SetData(new Color[] { Color.White });
//Draw rectangle, center screen,
Rectangle MainBox;
MainBox.Width = 700;
MainBox.Height = 400;
MainBox.X = game.Window.ClientBounds.Width/2 - MainBox.Width/2;
MainBox.Y = game.Window.ClientBounds.Height/2 - MainBox.Height/2;
//Draw Title
Rectangle TitleBox;
TitleBox.Width = 650;
TitleBox.Height = (int)ArialFont.MeasureString(Title).Y;
Padding = MainBox.Width/2 - TitleBox.Width/2;
TitleBox.X = (int)Padding + MainBox.X;
TitleBox.Y = (int)Padding + MainBox.Y;
//Draw Line Between Title and TextBox
Rectangle TextSeperator;
TextSeperator.Width = MainBox.Width - (int)Padding * 2;
TextSeperator.Height = 1;
TextSeperator.X = MainBox.X + (int)Padding;
TextSeperator.Y = TitleBox.Y + (int)(Padding * 1.2);
//Draw PictureBox
Rectangle PictureBox;
if (AssetPicturePath != string.Empty)
PictureBox.Width = 200;
else
PictureBox.Width = 0;
PictureBox.Height = 250;
PictureBox.X = MainBox.X + (int)Padding;
PictureBox.Y = MainBox.Y + TitleBox.Height + (int)Padding * 2;
MainBox.Height = PictureBox.Y - MainBox.Y + PictureBox.Height + (int)Padding;
//Draw TextBody
Rectangle TextBody;
if (AssetPicturePath == string.Empty)
TextBody.Width = MainBox.Width - ((int)Padding * 2);
else
TextBody.Width = MainBox.Width - ((int)Padding * 3) - PictureBox.Width;
TextBody.Height = MainBox.Height - ((int)Padding * 3) - TitleBox.Height;
if (AssetPicturePath == string.Empty)
TextBody.X = PictureBox.X;
else
TextBody.X = PictureBox.X + PictureBox.Width + (int)Padding;
TextBody.Y = TitleBox.Y + TitleBox.Height + (int)Padding;
//Draw MainBox
batch.Draw(FillText, MainBox, Color.Wheat);
//Draw PictureBox
//batch.Draw(FillText, PictureBox, Color.Green);
if (AssetPicturePath != string.Empty)
batch.Draw(game.Content.Load<Texture2D>(AssetPath + AssetPicturePath.TrimStart(new char[] { '/' })), PictureBox, Color.White);
//Draw TitleBox
//batch.Draw(FillText, TitleBox, Color.BlueViolet);
batch.DrawString(ArialFont, Title, new Vector2(TitleBox.X, TitleBox.Y),Color.Blue);
//Draw Line Between Title And TextBody
batch.Draw(FillText, TextSeperator, Color.Gray);
//Draw TextBody
//batch.Draw(FillText, TextBody, Color.Indigo);
int LineNumber = 0;
foreach (string Line in WrapText(Text, TextBody.Width))
{
batch.DrawString(ArialFont, Line, new Vector2(TextBody.X, TextBody.Y + (LineNumber * ArialFont.MeasureString(Line).Y)), Color.Black);
LineNumber++;
}
}
private object[] WrapText(string text, float Length)
{
string[] words = text.Split(' ');
ArrayList Lines = new ArrayList();
float linewidth = 0f;
float spaceWidth = ArialFont.MeasureString(" ").X;
int CurLine = 0;
Lines.Add(string.Empty);
foreach (string word in words)
{
Vector2 size = ArialFont.MeasureString(word);
if (linewidth + size.X < Length)
{
Lines[CurLine] += word + " ";
linewidth += size.X + spaceWidth;
}
else
{
Lines.Add(word + " ");
linewidth = size.X + spaceWidth;
CurLine++;
}
}
return Lines.ToArray();
}
+1以確保完整性。 但是,ArrayList已被棄用,您應該在此處使用List
謝謝!包裝正是我正在尋找的,我只需要找到一個圓潤的邊緣教程,我是金! – Ben 2010-05-17 05:27:08