2013-04-26 153 views
0

我想在XNA中創建3D菜單,它顯示NotImplementedException錯誤。有四個按鈕可供選擇的菜單。我錯過了這個項目中的任何東西嗎?我是XNA的新手。代碼如下。提前致謝。XNA項目中的3D菜單錯誤

//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); 
     } 
    } 

enter image description here

回答

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 
    } 
+0

是的,點擊後這個錯誤即將到來。但我找不到我失蹤的東西; – 2013-04-26 11:34:45

0

實際上沒有問題,您的代碼按預期工作。如果應用程序發現錯誤,則拋出異常,例如OutOfBoundsExceptionNullReferenceException。您也可以使用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只是一個最初的例外,告訴他們需要更換與拋出的異常開發商他們自己的代碼。