试图在XNA程序中执行图像菜单并陷入构造函数错误

时间:2013-04-23 14:10:22

标签: c# .net xna windows-phone-8 xna-4.0

这是ImageMenuGame.cs的代码。试图在Xna中做菜单,练习游戏开发,它显示错误作为构造函数错误。我是XNA的新手。 同时粘贴屏幕截图和代码。提前谢谢。enter image description here

namespace ImageMenu
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 

    public class ImageMenuGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        SpriteFont font;

        Texture2D texImageMenuItem;

        List<ImageMenuItem> Menu;

        int TotalMenuItems = 4;

        int index = 0;

        int currentIndex;


        public ImageMenuGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

            // Extend battery life under lock.
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            Menu = new List<ImageMenuItem>();


                base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            texImageMenuItem = Content.Load<Texture2D>("Imageitem");
            font = Content.Load<SpriteFont>("gameFont");

            int X = 150;
            int Y = 240;

            for (int i = 0; i < TotalMenuItems; i++)
            {

                ImageMenuItem item = new ImageMenuItem(new Vector2(X + i * (texImageMenuItem.Width + 20), Y), texImageMenuItem, spriteBatch);
                item.Index = index++;

                Menu.Add(item);


            }


        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            Vector2 tapPosition = new Vector2();
            TouchCollection touches = TouchPanel.GetState();


            if (touches.Count > 0 && touches[0].State == TouchLocationState.Pressed)
            {

                tapPosition = touches[0].Position;
                foreach(ImageMenuItem item in Menu)
                {
                    item.Update(gameTime, tapPosition);
                    if (item.Tap)
                    {
                        currentIndex = item.Index;

                    }

                }

            }

                      base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            foreach (ImageMenuItem item in Menu)
            {
                item.Draw(gameTime);
            }

            spriteBatch.DrawString(font, "CurrentIndex: " + currentIndex.ToString(), new Vector2(0,0), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

// ImageMenuItem.cs

namespace ImageMenu
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ImageMenuItem : Microsoft.Xna.Framework.GameComponent
    {
        SpriteBatch spriteBatch;

        Texture2D texture;

        public Vector2 Position;

        public Vector2 Origin;

        public bool Tap;

        float timer = 0;

        const float MinScale = 0.8f;

        const float MaxScale = 1;

        float scale = 0.8f;

        public int index = 0;

        public Rectangle Bound
        {
            get
            {
                return new Rectangle(
                    (int) (Position.X - Origin.X * scale),
                    (int) (Position.Y - Origin.Y * scale),
                    (int) (texture.Width * scale),
                    (int) (texture.Height * scale));            
            }
        }

        public ImageMenuItem(Game game, Vector2 Location, Texture2D Texture, SpriteBatch SpriteBatch)
            : base(game)
        {
            // TODO: Construct any child components here

           {
               Position = Location;
               texture = Texture;
               spriteBatch = SpriteBatch;

               Origin = new Vector2 (texture.Width / 2, texture.Height / 2);
           }



        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// 


  //  public override void Update(GameTime gameTime)
    //    {
            // TODO: Add your update code here
       //         base.Update(gameTime);
    //    }



            public void Update(GameTime gameTime, Vector2 tapPosition)
            {
            // if the tapped position within the text menu item bound,
            // set Tap to true and trigger the OnTap event
            Tap = Bound.Contains((int)tapPosition.X,
            (int)tapPosition.Y);
            // Accumulate the game elapsed time
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                 base.Update(gameTime);
            }
            public void Draw(GameTime gameTime)
            {

                if (Tap)
                {
                    if (scale <= MaxScale && timer > 200)
                    {
                        scale += 0.1f;

                        }
                        spriteBatch.Draw(texture, Position, null, Color.Red, 0f, Origin, scale, SpriteEffects.None, 0f);
                }
                    else
                    {

                        if (scale > MinScale && timer > 200)
                        {
                            scale -= 0.1f;

                        }
                            spriteBatch.Draw(texture, Position, null, Color.White, 0f, Origin, scale, SpriteEffects.None, 0f);


                    }

                }


        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎没有为ImageMenuItem ...

提供构造函数

如果您希望代码正常工作,请定义构造函数

public class ImageMenuItem {

    public ImageMenuItem(Vector2 vec, Texture2D tex, SpriteBatch sb){
    //assign
    }
}

编辑

您已定义了未设置的附加参数Game

public ImageMenuItem(Game game, Vector2 Location, Texture2D Texture, SpriteBatch SpriteBatch)
                     ^^^^^^^^

你必须传递一个游戏对象,试试

ImageMenuItem item = new ImageMenuItem(this, new Vector2(X + i * (texImageMenuItem.Width + 20), Y), texImageMenuItem, spriteBatch); // this is your Game