我目前正在为大学项目制作视频游戏。我们的老师为我们提供了很多关于他没有时间讨论的主题的教程。我的游戏菜单包含所有必需的图像,一个.JPG菜单背景和三个.PNG按钮文件(它们呈圆形)。
我跟随了Oyyou的tutorial并且设法让一切工作除了画出我拥有的其他2个按钮。我注意到问题出在Draw(SpriteBatch spriteBatch)方法中,
spriteBatch.Draw(Content.Load<Texture2D>(@"Textures\MainMenu"), new Rectangle(0, 0, 800, 600), Color.White);
btnStartGame.Draw(spriteBatch);
btnExitGame.Draw(spriteBatch);
btnEnterCode.Draw(spriteBatch);
菜单屏幕上显示的唯一按钮是第一个要声明的按钮(btn startGame)。如果我将其评论出来,则会显示下一个。所有三个按钮都处于活动状态,如果单击按钮进行编程,则它们是不可见的。以前有人偶然发现了这个问题吗?
//按要求代码隐藏按钮类:还要! TileEngine在主游戏项目和Level Edtior项目中都被引用,但在这种情况下不需要它。
关于除数/但是加倍,我现在意识到使3个不同的类与此相同是好的,因为每个按钮具有不同的宽度和高度。要使它们正确只是试验和错误,然后在Game1.cs中通过调用buttonName.SetPosition调整每个按钮的位置(new Vector2(x,y);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TileEngine;
namespace TimerGame.MenuButtons
{
class ButtonStart // the other 2 classes have different name of course
{
Texture2D texture;
Vector2 position;
Rectangle rectangle;
Color colour = new Color(255, 255, 255, 255);
public Vector2 size;
public ButtonStart(Texture2D newTexture, GraphicsDevice graphics)
{
texture = newTexture;
size = new Vector2(graphics.Viewport.Width / 5.4f, graphics.Viewport.Height / 8.7f); // these doubles varry as each button has different dimensions
}
bool down;
public bool isClicked;
public void Update(MouseState mouse)
{
rectangle = new Rectangle((int)position.X, (int)position.Y,
(int)size.X, (int)size.Y);
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRectangle.Intersects(rectangle)) // makes buttons flash softly
{
if (colour.A == 255) down = false;
if (colour.A == 201) down = true; // minimum amout to drop color
if (down) colour.A += 3; // amount by which to drop
else colour.A -= 3;
if (mouse.LeftButton == ButtonState.Pressed)
isClicked = true;
}
else if (colour.A < 255)
{
colour.A += 3; // amount by which to increase
isClicked = false;
}
}
public void SetPosition(Vector2 newPosition)
{
position = newPosition;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, colour);
}
}
}