我试图在 XNA 中做 3d 菜单,但出现以下错误:“名称 'item_OnTap' 在当前上下文中不存在。” 我是 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);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
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();
}
这是 //ModelMenuItem.cs public class ModelMenuItem 的代码:Microsoft.Xna.Framework.GameComponent {
Model modelItem;
public Vector3 Translation;
public Matrix View;
public Matrix Projection;
public int Index;
public bool Selected;
public int Offset;
public ModelMenuItem(Game game, Model model, Matrix view, Matrix projection)
: base(game)
{
// TODO: Construct any child components here
modelItem = model;
View = view;
Projection = projection;
Offset = 5;
}
public void Draw()
{
Matrix[] modelTransforms = new Matrix[modelItem.Bones.Count];
modelItem.CopyAbsoluteBoneTransformsTo(modelTransforms);
foreach (ModelMesh mesh in modelItem.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.AmbientLightColor = Color.White.ToVector3();
if (Selected)
{
effect.World = modelTransforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Translation + new Vector3(0, 0, Offset));
}
else
{
effect.World = modelTransforms[mesh.ParentBone.Index] * Matrix.CreateTranslation(Translation);
}
effect.View = View;
effect.Projection = Projection;
}
mesh.Draw();
}
}