我正在尝试在XNA中构建3d菜单,它显示NotImplementedException错误。一个菜单有四个按钮可供选择。我想念这个项目中的任何内容吗?我是XNA的新手。代码如下。提前致谢。
//ModelMenuGame.cs
public class ModelMenuGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
Model menuItemModel;
Vector3 cameraPosition;
Matrix view;
Matrix projection;
List<ModelMenuItem> Menu;
int TotalMenuItems = 4;
Rectangle LeftRegion;
Rectangle RightRegion;
int currentIndex = 0;
public event EventHandler OnTap;
public ModelMenuGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{
cameraPosition = new Vector3(-40, 10, 40);
view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 1000.0f);
Menu = new List<ModelMenuItem>();
LeftRegion = new Rectangle(0, 0, GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height);
RightRegion = new Rectangle(GraphicsDevice.Viewport.Width / 2, 0, GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height);
OnTap = new EventHandler(Item_OnTap);
currentIndex = currentIndex % TotalMenuItems;
if (currentIndex < 0)
{
currentIndex = TotalMenuItems - 1;
}
else if(currentIndex > TotalMenuItems - 1)
{
currentIndex = 0;
}
foreach (ModelMenuItem item in Menu)
{
if (item.Index == currentIndex)
{
item.Selected = true;
}
else
{
item.Selected = false;
}
}
menuItemModel = Content.Load<Model>("ModelMenuItem3D");
font = Content.Load<SpriteFont>("gameFont");
for (int i = 0; i < TotalMenuItems; i++ )
{
int X = -20;
ModelMenuItem item = new ModelMenuItem(this, menuItemModel, view, projection);
item.Translation = new Vector3(X + (i * 20), 0, 0);
item.Index = i;
Menu.Add(item);
}
Menu[0].Selected = true;
base.Initialize();
}
private void Item_OnTap(object sender, EventArgs e)
{
throw new NotImplementedException();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Vector2 tapPosition = new Vector2();
TouchCollection touches = TouchPanel.GetState();
if (touches.Count > 0 && touches[0].State == TouchLocationState.Pressed)
{
tapPosition = touches[0].Position;
Point point = new Point((int)tapPosition.X, (int)tapPosition.Y);
if (LeftRegion.Contains(point))
{
--currentIndex;
OnTap(this, null);
}
else if (RightRegion.Contains(point))
{
++currentIndex;
OnTap(this, null);
}
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
foreach (ModelMenuItem item in Menu)
{
item.Draw();
}
spriteBatch.Begin();
spriteBatch.DrawString(font , "Current Index :" + currentIndex.ToString(),new Vector2(0, 0), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
答案 0 :(得分:1)
if (LeftRegion.Contains(point))
{
--currentIndex;
OnTap(this, null);
}
else if (RightRegion.Contains(point))
{
++currentIndex;
OnTap(this, null);
}
因为OnTap每次都会抛出异常。
private void Item_OnTap(object sender, EventArgs e)
{
throw new NotImplementedException(); //This line throws a NotImplementedException
}
答案 1 :(得分:0)
实际上没有问题,您的代码按预期工作。如果应用程序捕获错误,则会引发异常,例如OutOfBoundsException
或NullReferenceException
。您还可以使用throw
关键字和错误的新实例抛出自己的异常。
下面的这一行明确告诉你程序抛出错误,所以如果删除throw new NotImp...
,错误将不再发生。见下文。
要摆脱错误,请更改:
private void Item_OnTap(object sender, EventArgs e)
{
throw new NotImplementedException(); //This line throws a NotImplementedException
}
到此:
private void Item_OnTap(object sender, EventArgs e)
{
// Put your code here to handle the tap event...
}
NotImplementedException
只是一个初始的例外,告诉开发人员他们需要用自己的代码替换抛出的异常。