I was pretty busy for the last week and that’s why I didn’t update here for several days.
In this application, there is an red ball bouncing between the Flash and Silverlight. The Flash is responsible to calculate the position of the ball. It will also pass the ball’s position the Silverlight application via JavaScript.
In the next sample, I will try to make the Silverlight as the master controller and demonstrate how to control Flash via JavaScript.
Vote for this sample
Comparison
Flash implementation: 20 minutes
Silverlight implementation: 20 minutes (Implemented First)
What’s the difference?
- ExternalInterface[AS3] vs RegisterScriptableObject[C#]
Source codes
JavaScript Sample 2 [Flash 9, AS3] (9.8 KiB, 693 hits)
JavaScript Sample 2 [Silverlight, C#] (10.3 KiB, 747 hits)
Flash
Silverlight
ExternalInterface[AS3] vs RegisterScriptableObject[C#]
Firstly, we have to write a JavaScript to connect to the Silverlight and update it’s internal value. The JavaScript can be easily accessed by the Class ExternalInterface.
// JavaScript
function updateProperty( x, y ) {
// JavaScriptSample2 is the Object Tag ID
var silverlightObject = document.getElementById("JavaScriptSample2");
if(silverlightObject.content != null)
silverlightObject.content.JavaScriptSample2.UpdateProperty(x, y);
}
// call the JavaScript inside AS3
ExternalInterface.call("updateProperty", ball.x, ball.y);
While in Silverlight, you have to register the class and method to the browser before they can be accessed outside the application. The registration is very simple.
// C#
// register the object to the client browser
HtmlPage.RegisterScriptableObject("JavaScriptSample2", this);
// make the method visible to the client browser
[ScriptableMember]
public void UpdateProperty(double x, double y)
{
}