Xblog.org

>> Home


Locations of visitors to this page

13 January 2008

Delegates and Events in C# / .NET

I have been spending a little time working on the process method and filling in the methods it calls, converting the code is getting easier the more I do it the hard part was remembering what the statment syntax was doing and converting it to an equivilent C# one, like if (bob){} where bob is an int can't be done you get a "can't convert int to bool error it it becoomes if(bob>0){} and if(!bob){} becomes if(bob==0){} simple stuff but there's allot if it.

Also I have figured out how I'm doing the sound stuff, I have created a new class as part of the SystemX namespace called SoundEventArgs then added an event using the standard Event Handler type :-

public event EventHandler<SoundEventArgs> RaisePlaySoundEvent;

In the SamplePlay(int p) method I create an instance of SoundEventArgs and set its cueName to the name of a sample I want to play and create a temporary copy of the event, check that there is a subscriber and finally call the event:-

EventHandler<SoundEventArgs> handler = RaisePlaySoundEvent;

if (handler != null)
handler(this, e);

Then in the main GameplayScreens constructor just after I create the new instance of the BertGame class and subscribe to the published Event:-

GameData = new BertGame();
GameData.RaisePlaySoundEvent += CueSound;

The CueSound method simply Plays the the requested wave file:-

void CueSound(object Sender, SoundEventArgs e)
{
BertSoundBank.PlayCue(e.CueName);
}

Now when a process call uses the SamplePlay method an event is fired and the main engine handles the playing of the sample, simple as.

The next biggie I'm going to have to look at is how best to implement the in-game menu that will let the player do things with objects in the game mainly because I have a nasty suspicion that this will make me reevaluate the current menu setup :-(

And another thing I know I've still got alot to put into the update method of Bert but it's running too fast if it keeps this up I'll be adding a sleep statment.

Labels: , ,

posted by Dave Henry at  

0 Comments:

Post a Comment

Links to this post:

Create a Link