2015-10-17 73 views
0

所以我正在为我的游戏设计一个菜单。这里是一个按钮的代码:C#Xna使用精灵字体实现了缩小比例效果

// button class 
public class ButtonGUI 
{ 
    public SpriteFont spriteFont; 
    string btnTxt; 
    public Rectangle btnRect; 
    Color colour; 

    public ButtonGUI(string newTxt, Rectangle newRect, SpriteFont newSpriteFont, Color newColour) 
    { 
     spriteFont = newSpriteFont; 
     btnRect = newRect; 
     btnTxt = newTxt; 
     colour = newColour; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     // TextOutliner() is a static helper class for drawing bordered spritefonts I made 
     TextOutliner.DrawBorderedText(spriteBatch, spriteFont, btnTxt, btnRect.X, btnRect.Y, colour); 
    } 
} 

// TitleScreen.cs 
ButtonGUI btnPlay, btnPlay_2; 
bool indexPlay; 
string[] menuTxt; 
SpriteFont GameFontLarge, GameFontLargeHover; 

// LoadContent() method: 
// Load both spritefonts.... 

menuTxt = new string[4]; 
menuTxt[0] = "Play Game"; 
menuTxt[1] = "Achievements"; 
menuTxt[2] = "Settings"; 
menuTxt[3] = "Exit Game"; 

btnPlay = new ButtonGUI(menuTxt[0], new Rectangle(150, 300, (int)GameFontLarge.MeasureString(menuTxt[0]).X, (int)GameFontLarge.MeasureString(menuTxt[0]).Y), GameFontLarge, Color.White); 
btnPlay_2 = new ButtonGUI(menuTxt[0], new Rectangle(150, 300, (int)GameFontLargeHover.MeasureString(menuTxt[0]).X, (int)GameFontLargeHover.MeasureString(menuTxt[0]).Y), GameFontLargeHover, Color.Yellow); 

// Update() method: 
MouseState mouseState = Mouse.GetState(); 
Rectangle mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1); 

if (mouseRect.Intersects(btnPlay.btnRect)) 
{ 
    indexPlay = true; 
    if (mouseState.LeftButton == ButtonState.Pressed) Game1.CurrentGameState = Game1.GameState.playScreen; 
} 
else indexPlay = false; 

// Draw() method: 
if (indexPlay) 
{ 
    btnPlay_2.Draw(spriteBatch); 
} 
else btnPlay.Draw(spriteBatch); 

所以我做了所有这4个不同的按钮。这两个精灵字体是相同的字体,但不同的大小。现在当我测试游戏时,当我将鼠标悬停在每个按钮上时,文本从白色变为黄色并变得更大。但由于按钮坐标是在x = left-most; y = top-most字体改变时完成的,所以较大的字体被绘制在与较小字体相同的位置处。鼠标悬停时,我想获得体面的“缩放”效果。我的意思是我知道我在初始化时将按钮位置设置为该位置,但仍然需要某种类型的算法来找到方法并在较老的中心绘制较大的字体......您会采取什么方法?

回答

0

你可以改变你悬停按钮的基础上,大小差异的位置:

var sizeDifference = btnPlay_2.btnRect.Size - btnPlay.btnRect.Size; 
btnPlay_2.btnRect.Offset(-sizeDifference.X/2, -sizeDifference.Y/2); 
+0

'Point'不包含'Width' /'Height' ... –

+0

我不能接受的定义这个答案如果它是错误的......用'X'和'Y'代替'Width'和'Height'对我来说非常合适。 –

+0

Murray可能不习惯xna,所以他只是想到了来自System Drawing命名空间的Point。 Vector2是相当于Point结构的xna – Falgantil