XNA Framework - 尝试正确地重载System.EventHandler <platformer.playerindexeventargs> </platformer.playerindexeventargs>

时间:2011-12-02 01:25:17

标签: c# xna-4.0

我是C#和XNA的新手。我目前正在开发游戏的选项菜单。选项菜单执行以下操作:

  1. 声音(打开或关闭)
  2. 亮度(增加/减少)
  3. 记分牌(查看5个最高分)
  4. 控件(显示如何使用控件)
  5. 我在互联网上找到了一些处理亮度的代码,但它是使用Game.cs类实现的。我试图在我的OptionsMenuScreen.cs类(继承自Gamescreen.cs)中使用该代码。

    我遇到的问题是我的名为BrightnessSelected的EventHandler超载了一些额外的参数来处理触摸输入,以便用户可以使屏幕变亮/变暗。这种重载导致了一些问题,因为它基于System.EventHandler中提供的模板,该模板只接受一个参数。

    如何让模板允许我超载?或者还有另外一个吗?我真的希望这是有道理的,并且我以非复杂的方式表达了我的问题。感谢大家!

    代码如下:

    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Input.Touch;
    using Microsoft.Xna.Framework.Media;
    #endregion
    
    namespace Platformer
    {
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
    
        #region Fields
        GraphicsDevice graphicsDevice;
        SpriteBatch spriteBatch;
        Texture2D whiteTexture;
        int brightnessStart;
        int contrastStart;
        BlendState brightnessBlend;
        BlendState contrastBlend;
    
        MenuEntry sound1;
        MenuEntry brightness;
        MenuEntry scoreboard;
        MenuEntry controls;
    
        static bool soundonoff = true;
    
        #endregion
    
        #region Initialization
    
        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            sound1 = new MenuEntry(string.Empty);
            brightness = new MenuEntry(string.Empty);
            scoreboard = new MenuEntry(string.Empty);
            controls = new MenuEntry(string.Empty);
    
            SetMenuEntryText();
    
            // Hook up menu event handlers.
            brightness.Selected += brightnessSelected;
            sound1.Selected += sound1Selected;
            scoreboard.Selected += scoreboardSelected;
            controls.Selected += controlsSelected;
    
            // Add entries to the menu.
            MenuEntries.Add(sound1);
            MenuEntries.Add(brightness);
            MenuEntries.Add(controls);
            MenuEntries.Add(scoreboard);
        }
    
        protected void Initialize()
        {
            // TODO: Add your initialization logic here
    
    
            brightnessStart = 255;
            contrastStart = 128;
    
            brightnessBlend = new BlendState();
            brightnessBlend.ColorSourceBlend = brightnessBlend.AlphaSourceBlend = Blend.Zero;
            brightnessBlend.ColorDestinationBlend = brightnessBlend.AlphaDestinationBlend            
            = Blend.SourceColor;
    
            contrastBlend = new BlendState();
            contrastBlend.ColorSourceBlend = contrastBlend.AlphaSourceBlend = Blend.DestinationColor;
            contrastBlend.ColorDestinationBlend = contrastBlend.AlphaDestinationBlend = `Blend.SourceColor;`
        }
    
        public virtual void LoadContent()
        {
            whiteTexture = new Texture2D(graphicsDevice, 1, 1);
            whiteTexture.SetData<Color>(new Color[] { Color.White });
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(graphicsDevice);
        }
    
        public virtual void Draw(GameTime gameTime)
        {
            graphicsDevice.Clear(Color.CornflowerBlue);
    
            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.Immediate, brightnessBlend);
            spriteBatch.Draw(whiteTexture, new Rectangle(0, 0, 480, 800), new Color(brightnessStart, `brightnessStart, brightnessStart, 255));`
            spriteBatch.End();
    
            spriteBatch.Begin(SpriteSortMode.Immediate, contrastBlend);
            spriteBatch.Draw(whiteTexture, new Rectangle(0, 0, 480, 800), new Color(contrastStart, `contrastStart, contrastStart, 255));`
            spriteBatch.End();
    
            graphicsDevice.BlendState = BlendState.Opaque;
    
            base.Draw(gameTime);
        }
        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            sound1.Text = "Sound " + (soundonoff ? "on" : "off");
            brightness.Text = "Brightness ";
            scoreboard.Text = "Scoreboard ";
            controls.Text = "Controls ";
        }
    
        #endregion
    
        #region Handle Input
    
        /// <summary>
        /// Event handler for when the Sound menu entry is selected.
        /// </summary>
        /// 
        protected void Update(GameTime gameTime)
        {
    
            foreach (TouchLocation t in TouchPanel.GetState())
            {
                if ((t.State == TouchLocationState.Pressed)
                        || (t.State == TouchLocationState.Moved))
                {
                    if (t.Position.Y <= 400)
                        brightnessSelected(t, ref brightnessStart);
                }
                else
                    brightnessSelected(t, ref contrastStart);
            }
        }
        void sound1Selected(object sender, PlayerIndexEventArgs e)
        {
            soundonoff = !soundonoff;
            SetMenuEntryText();
        }
        /// <summary>
        /// Event handler for when the Brightness menu entry is selected.
        /// </summary>
        void brightnessSelected(object sender, PlayerIndexEventArgs e, TouchLocation t, ref int adjustee)
        {
            if (t.Position.X < 240)
                adjustee -= 3;
            else
                adjustee += 3;
    
            adjustee = Math.Max(Math.Min(adjustee, 255), 0);
            SetMenuEntryText();
        }
    
        /// <summary>
        /// Event handler for when the Scoreboard menu entry is selected.
        /// </summary>
        void scoreboardSelected(object sender, PlayerIndexEventArgs e)
        {
            //need to find a way to transition to HighScoreScreen.cs here
            //ScreenManager.AddScreen(new HighScoreScreen(), e.PlayerIndex);
            SetMenuEntryText();
        }
    
        /// <summary>
        /// Event handler for when the Control menu entry is selected.
        /// </summary>
        void controlsSelected(object sender, PlayerIndexEventArgs e)
        {
            //ScreenManager.AddScreen(new ControlScreen(), e.PlayerIndex);
            SetMenuEntryText();
        }
    
        #endregion
    }
    }
    

1 个答案:

答案 0 :(得分:2)

您无法将ref int adjustee传递给事件处理程序,并期望将其值返回给被调用者。事件处理程序就是这样。可以把它想象成一个可以响应事件的回调。

接下来,您需要将EventArgs子类化并将自己的数据添加到其中,如下所示。

public class MyEventArgs : EventArgs
{
    private string msg;

    public MyEventArgs( string messageData ) 
    {
        msg = messageData;
    }
    public string Message { 
        get { return msg; } 
        set { msg = value; }
    }
}

请参阅有关处理程序和参数的文章:http://msdn.microsoft.com/en-us/library/db0etb8x.aspx

在您的情况下,您需要访问MenuEntry的代码并编辑它传递的TEventArgs类型(在这种情况下,它使用PlayerIndexEventArgs)。您可以再次继承PlayerIndexEventArgs,或者只是将其重新定义为您自己的类型。我不知道XNA课程是什么,如果没有意义,你可能需要再次考虑设计。

当你完成后,你应该有类似

的东西
int _brightness;

void brightnessSelectedEventHandler(object sender, BrightnessEventArgs e)
{
    if (e.touch != null && e.touch.position != null)
    {
        const int TOUCH_POS_THRESHOLD = 240; // this is part of UI and should really be somewhere else, but put it here for convenience
        if (e.touch.position.x < TOUCH_POS_THRESHOLD 
            adjustBrightness(-3);
        else
            adjustBrightness(3);
    }
    SetMenuEntryText();
}

void adjustBrightness(int offset)
{
    _brightness += offset;
}

请注意,这里我只是在您的类中添加一个成员变量_brightness。这就是你如何“回应”事件 - 在某处放置一些数据或调整一个值。您需要考虑数据的位置以及应从OO角度对其进行封装的位置。

祝你好运。