簡體   English   中英

如何將方法作為參數傳遞?

[英]How do I pass a method as a parameter?

我一直在修改游戲引擎,其中一部分一直在為UI元素構建類。 我的目標是使添加按鈕到UI變得非常簡單,只需一行包含按鈕的位置以及當有人按下該按鈕時調用的方法。 我只是不知道如何傳遞將在按下按鈕時觸發的目標方法。 我可以使用方法來訂閱委托事件,但是不能將它們包裝在我創建的按鈕對象列表中。

我已經將代碼簡化為我要在此處完成的工作。 主要的難點是我不確定為addButton()的方法參數輸入什么作為對象類型,以便能夠傳遞可以訂閱委托事件的另一種方法。 如果我嘗試Void或Object,則會出現轉換錯誤。

public Class UserButton
{
    public delegate void triggerEvent();
    public triggerEvent ButtonPress; //This should fire off the event when this particular button is pressed.

    public UserButton(Point buttonlocation, Point buttonsize, string buttontext, Texture2d Texture)
    {
        //Store all this data
    }
}

public Class UserInterface  //This class is for the buttons that appear on screen.  Each button should have a location and a method that it calls when it's "pressed"
{

    List<UserButton> ButtonList = new List<UserButton>(); //List of all the buttons that have been created.  


        //Add a button to the list.
    public void addButton(Point location, Point buttonsize, Texture2d texture, Method triggeredEvent) //Compile fails here because I can't figure out what type of object a method should be.
    {
        UserButton button = new UserButton(Point location, Point buttonsize, Texture2d texture);

        button.ButtonPress += triggeredEvent; //The target method should subscribe to the triggered events. 

        ButtonList.Add(button);

    }

    public void checkPress(Point mousePos) //Class level method to sort through all the buttons that have been created and see which one was pressed.
    {
        foreach (UserButton _button in ButtonList)
        {
            if (_button.ButtonBounds.Contains(mousePos))
            {
                _button.ButtonPress(); //Call the event that should trigger the subscribed method.
            }
        }
    }
}

public class Game1 : Game
{

    //Main methods

    //Load
    protected override void LoadContent()
    {
        UI = new UserInterface(); //Create the UI object
        UI.addButton(new Point(32,32), Texture,toggleRun()); //Pass in the method this button calls in here.
    }   

    private void toggleRun() //The button should call this method to start and stop the game engine.
    {
        if (running)
        {
            running = false;
        } else {
            running = true;
        }


        protected override void Update(GameTime gameTime)
        {
            if (MouseClick) //simplified mouse check event
            {
                UI.checkPress(mousePos); //Pass the current mouse position to see if we clicked on a button.
            }
        }
    }

您的“ triggeredEvent”參數應與“ triggerEvent”的委托類型相同:

public void addButton(Point location, Point buttonsize, Texture2d texture, triggerEvent triggeredEvent) 

要將方法作為參數傳遞,請在不調用方法名稱的情況下使用方法名稱(這僅在方法“ toggleRun”沒有任何重載方法並且與“ triggerEvent”委托的簽名匹配時才有效):

UI.addButton(new Point(32,32), Texture, toggleRun);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM