0

我正在尝试在 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);
        }
    }

在此处输入图像描述

4

2 回答 2

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
    }
于 2013-04-26T09:08:59.363 回答
0

实际上没有问题,您的代码按预期工作。如果应用程序捕获到错误,则会引发异常,例如 anOutOfBoundsException或 a NullReferenceExceptionthrow您还可以使用关键字和新的错误实例引发自己的异常。

下面的那一行明确告诉您程序抛出错误,因此如果您删除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只是一个初始异常,告诉开发人员他们需要用自己的代码替换抛出的异常。

于 2013-05-20T11:32:42.550 回答