Communicating between Flash and C#

Recently I was tasked with developing part of a game in Flash that was work in conjunction with another part – developed in C#. The two needed to pass information back and forth. Unfortunately documentation on this is scarce.

Basically the game was to be compiled in Visual Studio and was to be comprised of two main components, a shooter game and a trivia game. The shooter game was a pre-existing game developed in C# and the trivia portion was to be all new and developed in Flash. Part of he UI would also be in Flash and part in C#. The users success in the shooter stage of the game determined a bonus in the form of additional time allotted to answer the trivia questions that followed.

All of these components developed on completely different platforms needed to communicate back and forth from to one another keep score of both parts of the game (shooter and trivia) and ultimately track user session data (via a text file written to the C: drive) upon completion of the experience. User Name, Bonus, Trivia Questions Asked and the chosen Answers all needed to be variables that were passed back and forth.

Nesting Flash in C# with AxShockwaveFlash
The control used to embed flash in a Windows Forms app is called ‘AxShockwaveFlash’  and I have skipped instructions on how to insert this into Visual Studio as a resource or how to connect to Flash to AxShockwaveFlash because that isn’t the focus of this article.

Getting data from C# to Flash
Once Flash is in C#, Flash vars is a good way to get preexisting data from C# to Flash. Setting this up was easy and would be the same if you were going to do it with HTML, but instead C# acts as a shell and provides the FlashVars to the .swf file.

example of AS3 code to handle Flash Vars for the Flash Var bonusTime

  2. var bonus:Number;
  3. if (root.loaderInfo.parameters.bonusTime != null) {
  4. bonus = root.loaderInfo.parameters.bonusTime;
  5. } else {
  6.  bonus = 0
  7. }

Once you expose your FlashVars you can proceed to populate your data from C#.

Getting Data from Flash to C#

The best way to do this was to use“yourMethod”) to speak from Flash to C#.

AS 3 example

  1. // import
  2. import flash.external.ExternalInterface;
  3. //method
  4."MethodName", 100)

C# example

AxShockwaveFlash FlashPlayer = new AxShockwaveFlash();
FlashPlayer .FlashCall += new _IShockwaveFlashEvents_FlashCallEventHandler(OnFlashCall);

Whenever an is called, the FlashCall event is triggered:

private void OnFlashCall(object sender, _IShockwaveFlashEvents_FlashCallEvent e)
       string request = e.request;

To send commands from AS using the method, the following event handler is attached to the AxShockwaveFlash control

The ExternalInterface call from AS3 shown above delivers a string of XML formatted as follows:

  1. <invoke name="MethodName" returntype="xml"><arguments><number>100</number></arguments></invoke>

We can then load the request as we please and process it accordingly in C#.

Unfortunately there is no other way I know for C# to speak to Flash other than Flash Vars. This can be an annoying limitation to the communication process.

Well hopefully this brief explanation helps someone out and certainly if anyone else has light to shed on the subject by all means comment.

Posted on November 12, 2010 at 9:33 pm by Runtime · Permalink
In: ActionScript 3, Flash, Tutorial

Leave a Reply

You must be logged in to post a comment.