开发者

Is it possible to add a method to a class in a collection?

I'm trying to build pages in XNA with buttons and sliders and have tried a few ideas but I seem to get stuck between keeping things 'Object-Oriented' and having the buttons and sliders remain useful without adding to much to the actual 'Button' and 'Slider' classes.

So I'm wondering if there is a magic way to instantiate a Button class and then add on a method, or some kind of link to a method so that I can iterate though my collection of buttons or sliders and if one is 'hit' execute the specific method related to that button?

Preferably I'd like to write the methods one after another in the parent class that represents the screen im drawing at the time.

Fantasy code example:

class Room // Example class to manipulate 
{ 
    public bool LightSwitch; 
    public void Leave() { /* Leave the room */ } 
} 

class Button 
{   // Button workings in here 
    public bool GotPressed(/*click location */) 
    { /* If click location inside Rect开发者_JAVA技巧 */ return true; } 

    public void magic() {} // the method that gets overidden 
} 

public class GameScreen 
{ 
    public Room myRoom; 
    private List<Button> myButtons; 

    public GameScreen() 
    { 
        myRoom = new Room(); 
        myRoom.LightSwitch = false; 
        List<Button> myButtons = new List<Button>(); 

        Button B = new Button(); 
        // set the buttons rectangle, text etc 
        B.magic() == EscapeButton(B); 
        myButtons.Add(B); 

        Button B = new Button(); 
        B.magic() == SwitchButton(B); 
        myButtons.Add(B); 
    } 

    public void Update() // Loop thru buttons to update the values they manipulate 
    {   foreach (Button B in myButtons) 
        { if(B.GotPressed(/*click*/)) { B.magic(B) } }} 
        // Do the specific method for this button  

    static void EscapeButton(Button b) 
    { myRoom.Leave(); } 

    static void SwitchButton(Button b) 
    { myRoom.LightSwitch = true; } 
} 


I think you are looking either for delegates for for events. I'd recommend to use events here:

First, create a public event with everything on your class, e.g.:

public delegate void ClickedHandler(object sender, EventArgs e);
public event ClickedHandler Clicked;
private void OnClidked()
{
  if (Clicked != null)
  {
     Clicked(this, EventArgs.Empty);
  }
}

Then, you make a method in the button class that checks, if it was clicked

public void CheckClick(Vector2 click)
{
   if (/* [i am clicked] */)
   {
     OnClicked();
   }
}

Outside the button you can subscribe to the clicked event like this:

var b = new Button();
b.Clicked += new ClickedHandler(b_Clicked);

/* [...] */

private void b_Clicked(object sender, EventArgs e)
{
   /** do whatever you want when the button was clicked **/
}

To find more about events, go here: http://www.csharp-station.com/Tutorials/lesson14.aspx . Hope this helps.


C# has extension methods, which may fit your needs.

Extension methods are defined in some static class with an special syntax. A sample of that could be that:

public static char GetLastChar(this string some) 
{
       return some[some.length - 1];
}

string a = "hello world";
char someChar = a.GetLastChar();

You can learn more here:

  • http://msdn.microsoft.com/en-us/library/bb311042.aspx


I have vague understanding of the requirements of game programming but I saw a presentation about this framework recently - http://dynobjects.codeplex.com/ It sounds like ti solves a similar if not the same problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜