macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript
At my last post i have written about dynamic-method-calls
in C-Sharp.
At this look we take a five-minute look at the communication
between C-Sharp and Flash.

Download Example
How do we start:
1.) You need to add an shockwave-flash element to the toolbar in the designer
of Visual-Studio. Click right at the toolbar and than choose add Element and
activate Shockwave Flas Object in Com-Control-Register.
For more details go to the excellent articel by Mike Chambers:
http://www.markme.com/mesh/archives/002977.cfm
2.) We need to load the flash movie into our ActiveX-Control. Therefore
we can use axShockwave.LoadMovie(0, "absolutPathToFlash") method.
3.) We need to send something to our Flash-Movie. Therefore we can
use the axShockwave.SetVariable("VariableName", "Value") method.
4.) Inside Flash we have to receive any change of the Variable, so we
can use something like that:
/////////////////////////////////////////////////////////////////
// C-Sharp-Message-Broadcaster
/////////////////////////////////////////////////////////////////
_root.bcCSharpMessage = function (varName, oldVal, newVal)
{
arguments.callee.broadcastMessage("onCSharpMessage", newVal);
}
ASBroadcaster.initialize(_root.bcCSharpMessage);
_root.onLoad = function ()
{
this.CSharpMessage = null;
this.watch("CSharpMessage", this.bcCSharpMessage);
}
/////////////////////////////////////////////////////////////////
// Test
/////////////////////////////////////////////////////////////////
// Receive from C-Sharp
obj = new Object();
obj.onCSharpMessage = function (strMessage)
{
_root.flashoutput_txt.text += strMessage + "\n";
}
_root.bcCSharpMessage.addListener(obj);
5.) To send variables to C-Sharp, we can use:
// Send to C-Sharp
_root.buttonSend_mc.onRelease = function ()
{
fscommand("flashMessage", "Hi C-Sharp");
}
6.) To receive in C-Sharp we need code like that:
//
// Let's subscribe at the fsCommand-Delegate from the swf-ActiveX,
// well this looks horrible to remember but if you are using
// Visual-Studio you get the fastest Auto-Complete you have ever seen.
this.axShockwaveFlash1.FSCommand +=
new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(axShockwaveFlash1_FSCommand);
private void axShockwaveFlash1_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
// We only want to react if we got our command
if (e.command == "flashMessage")
{
this.richTextBox1.AppendText(e.args + "\n");
}
}
7.) Ready...;-)
Posted by hOk at August 25, 2003 08:08 PM
I' am currently learning C# and just asked me
how to call an Method dynamicly.
In Flash-Actionscript it is so simple, because we
are dealing with an scripting-language that does
not have bind all method-calls at runtime, so we
can simply write
this.myMethod = function () { trace("Dynamicly called, yes!") };
this["myMethod"]();
Realy cool and easy.
In C# we have to use some Methods from the
System- and System.Reflection-Namespace for
doing that.
using System;
using System.Reflection;
namespace TestReflectionDynamicMethodCall
{
class TestClass
{
public TestClass ()
{
String[] asArgs = {"eins", "zwei"};
Type myType = this.GetType();
MethodInfo myMethodInfo = myType.GetMethod("TestMethod"); //<--
myMethodInfo.Invoke(this, asArgs);
}
public void TestMethod (string eins, string zwei)
{
Console.WriteLine("TestMethod wurd dynamisch aufgerufen " + eins + " " + zwei);
}
}
/// <summary>
/// Zusammenfassung für Class1.
/// </summary>
class Class1
{
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main(string[] args)
{
TestClass testObj = new TestClass();
Console.ReadLine();
}
}
}
Yes, it works we only have a little bit more typing.
In mostly cases it is not an good practise to do something
like that because you can't see comfortable who is
calling who...;-)
(what a sentence)
Posted by hOk at August 20, 2003 07:37 PM
Oh yes, it pretty good i have just upgradet my
wampp and it runs fantisticly without any troubles
and has couple of new features in it.
Maybe you know the apachefriends-project, they give
us a complete server enviroment for development.
Here are the main features:
Apache 2.0.47,
MySQL 4.0.14,
PHP 4.3.3 rc2 + PEAR,
Perl 5.8.0,
mod_php 4.3.2,
mod_perl 1.99_10,
mod_ssl 2.0.47,
openssl 0.9.7b,
PHPMyAdmin 2.5.2 rc1,
Webalizer 2.01-10,
Mercury Mail Transport System for Win32 and NetWare Systems v3.32,
JpGraph 1.12.1,
FileZilla FTP Server 0.8.5,
(WEB-DAV + MOD AUTH MYSQL experimental).
It is incredible cool, i really like the new things like
the integrated Mailserver and the FTP-Server, but
the new MySQL-DB is also real cool, with it new features
like transactions, caching and so on.
Posted by hOk at August 18, 2003 10:55 PM
I have just started reading Kent Becks Test-Driven Development
and my first impression is a good one.
This technic leads to clearer and easier coding.
The TDD-Concept, i have just started reading
so don't take this for perfect, is good as simple.
It seperates the development in three steps.
The first step is to write the test for an unit
of your software.
The second step is to write the code. This step is
ready, if the test doesn't failes.
The third step is to refactore youre code, that means
that you eleminate redundant parts and etc. Martin
Fowler has a written a really good book about this.
Kent Beck has given this three steps the name:
Red-Phase, Green-Phase and Refactoring.
Well let us have an example, we will build an priority
stack. First we are going to the Red-Phase(Test-Writing)
it's real cool because we have to define the interface
too, and have an example usage for our class that
documents our code.
// We give all test-functions the suffix: "_Test"
// so we could search our object-tree to run all
// tests with one call, oh yeah how easy...;-)
PriorityStack_Test = function () {
var tmp;
//
// Create an instance
ps = new PriorityStack();
//
// Is our class existing
if (PriorityStack == undefined) trace("ERROR: The class: 'PriorityStack' doesn't exist");
// Well, this is an mostly useless test because, it doesn't tests something
// that could break or is hard to implement. But look at the point that we
// does not have irretating output if our class exist.
//
// Let's go on
ps.addItem("two", 2);
ps.addItem("four", 4);
ps.addItem("one", 1);
ps.addItem("three", 3);
ps.addItem("five", 5);
//
// Test the length-Property
if (ps.length != 5) trace("ERROR: PriorityStack.length-Property doesn't return 5");
//
// Test the pop-method
if ((tmp = ps.pop()) != "five") trace("ERROR: PriorityStack.pop-Method doesn't return 'five' it has returned: " + tmp);
if ((tmp = ps.pop()) != "four") trace("ERROR: PriorityStack.pop-Method doesn't return 'four' it has returned: " + tmp);
if ((tmp = ps.pop()) != "three") trace("ERROR: PriorityStack.pop-Method doesn't return 'three' it has returned: " + tmp);
if ((tmp = ps.pop()) != "two") trace("ERROR: PriorityStack.pop-Method doesn't return 'two' it has returned: " + tmp);
if ((tmp = ps.pop()) != "one") trace("ERROR: PriorityStack.pop-Method doesn't return 'one' it has returned: " + tmp);
//
// Test the length-Property again
if (ps.length != 0) trace("ERROR: PriorityStack.length-Property doesn't return 0");
};
Well, now we have an nice result in the output window:
ERROR: The class: 'PriorityStack' doesn't exist
ERROR: PriorityStack.length-Property doesn't return 5
ERROR: PriorityStack.pop-Method doesn't return 'five'
ERROR: PriorityStack.pop-Method doesn't return 'four'
ERROR: PriorityStack.pop-Method doesn't return 'three'
ERROR: PriorityStack.pop-Method doesn't return 'two'
ERROR: PriorityStack.pop-Method doesn't return 'one'
ERROR: PriorityStack.length-Property doesn't return 0
Now we can start to write our class:
PriorityStack = function () {
this.elements = new Array();
};
//
PriorityStack.prototype.addItem = function (data, priority) {
var e = this.elements;
var l = e.length;
//
while (l--) {
if (e[l].p <= priority) break;
}
//
e.splice(++l, 0, {d:data, p:priority});
};
//
PriorityStack.prototype.pop = function () {
return this.elements.pop().d;
};
//
PriorityStack.prototype.addProperty("length", function () { return this.elements.length }, null);
//
// Test
PriorityStack_Test();
Cool, we have no output, thats fine, now we could optimise
or/and refactor the code.
An interesting thing is that you can also write performance
tests, so that we could easier find faster algorithms.
Posted by hOk at August 14, 2003 05:07 PM
