As I mentioned in one of the post responses on this blog here is an examples of how you can wire up events in your game object. As usual I have tried to keep this as simple as I can to try and make it clear whats going on.
I guess the first thing to do is describe what an event is, well an event can be anything that occurs during the execution of your game, an enemy shoots, a bomb detonates, the player passes the mouse over a game object (as in this example) but rather than checking ALL your objects each loop you just want to be told when this happens so you can do what you need to do to handle the event. I guess this is a pretty basic C# skill, but if you are not a C# developer you may not know how to do this, so here is my example of Events and how I wire them up.
So off to the code, I created a new project called GenericXNAEvents, off this project I created the object I am going to use in this example, a simple object that will display a coloured rectangle and have events attached to it that tell me when the mouse is passed over it, clicked on it and when the mouse leaves it. The first thing I do is create a delegate that defines my event, I make this public so it is accessible outside of my class as this dictates how the event handler will be written by those "subscribing" to the events. This done I then create my events, these are logical instances of the delegate that are going to be used to "Fire" my events when they occur, "Subscribers" will also be able to attach (wire) them selves to these events.
As you can see I have three events, one for when the mouse is over this object, one for when it moves off this object and one for when this object is clicked.
Now, I create the conditions that fire my events in the Update method.
First thing I do is create the rectangles that will be used to check for intersection with the mouse, if the mouse intersects the object then I set my boolean flag marking that the mouse is over me to true, then check to see if anyone has subscribed to my OnMouseOver event, if they have I call there event handlers. If I am not intersecting the mouse and the mouse has been over me, then the mouse must have moved off of me, so I set my flag to false, check to see if anyone has subscribed to my OnMouseOut event, if they have I call there event handlers. Finally, if the mouse is over me and it has been clicked I check if anyone has subscribed to my OnMouseClick event and if they have, call there event handlers.
So that's the mechanics of it, how do I now subscribe to these events??
In the constructor of my Game1 Class I create three instances of my EventObjects, placed at different locations on the screen with different names and colours, I then "wire" up my event handlers.
I now need to create my event handlers, these handlers MUST have the same pattern (return value and parameters) as the FiredEvent delegate defined earlier or you will get a compile error. Also note that I have appended (+=) the event handlers to the objects events, this means you can have a single event fire more than one event handler, you can associate a single handler if you like (=), and this will replace ALL previously set handlers to the one you specify.
As you can see I am checking to see what the sender is before I do anything with it, you could have objects with a similar event definition call this event handler also, but in this case we just have the one.
So effectively you can have an event call multiple event handlers and have event handler called by multiple objects and object types. Pretty cool eh?! Thanks Microsoft :)
Full solution example can be found here.
Any questions, please post them here and I will do my best to answer them.