Archive for the ‘Flash Tutorials’ Category

29
Dec

External Interface class

Posted By wassim in Flash, Flash Tutorials

This new addition to ActionScript is a very wanted feature, older versions of flash scripting language relied completely on fscommand to comunicate with the flash ocx container, fscommand is very limited in functionality, the new externalInterface is much more versatile.

Let’s consider the following scenario, you want to get the type of browser the user is using and to adjust certain properties of your flash movie accordingly, there is no flash command that does just that, however there is one in javascript.

So the job now is to code a simple function in javaScript and call it from flash, flash should be able to call the function and to get the return value of it.

That’s where externalInterface jumps in.

Here’s a fast code snipet:

In javaScript:

[js]
function tellFlash(){
var n=navigator.appName;
return n;
}

[/js]

now n holds the name of the navigator we want to know, in flash we will call this javascript function and we’ll get the return value n, assign the n held text as the text of a dynamic text box, here’s the code that does that:

[as]

import flash.external.ExternalInterface;// we start by importing the class

btn.onPress = function() {

txt.text = ExternalInterface.call(“tellFlash”, “”);

};

[/as]

and that’s about it!

Now what if we want to do the opposite? i.e call an ActionScript function from the container , from javascript for example, how do we do that?

Easy, we register a flash function as callable using the code:

[as]

ExternalInterface.addCallback(“methodName”, instance, method);

[/as]

where:

methodName:String – The name by which the ActionScript function can be called from JavaScript. This name does not need to match the actual name of the ActionScript method.

instance:Object – The object to which this resolves in the method. This object is not necessarily the object on which the method can be found — you can specify any object (or null).

method:Function – The ActionScript method to be called from JavaScript.

to show this in action, I’ve placed a small red rectangle on top of the swf and wrote the following code:

ExternalInterface.addCallback(“obey”, null, obey);
function obey() {
square._visible =false;
}

so whenever javascript calls the function obey, the red rectangle whose instance name is “square” will simply disappear since the function obey() does just that, see the code below:

function obey() {
square._visible =false;
};

one last step, the javascript that will call this actionscript function from:
function callExternalInterface() {
thisMovie(“externalInterfaceExample”).obey();
}

And that’s all it takes.

I’ve added a more advanced sample where javascript prompts you for input then sends the input text back to flash.

Click here to see a sample swf showing externalInterface in action.

5
Dec

External Communication Tutorial

Posted By wassim in Flash, Flash Tutorials

Annoucement: Expect an external communication tutorial soon

Hello, the externalInterface class is going to be the topic of the next tutorial to be posted decembre 10.

So stay tuned :)

24
Nov

Diacritics stripper (Remover) tutorial.

Posted By wassim in Flash, Flash Tutorials

Hello and salam;

I have been working onthis project for a couple of hours now and I thought I should share with you my findings since I tried searching the internet and found nothing at all in this area, what I am trying to show you is how to remove Arabic diacritics from Arabic text dynamically in Flash using ActionScript.

Now what I am about to explain is not an undocumented feature, a hidden API or some sort of a hack, it’s simply my approach to solve this problem. In fact this method will work in flash player version 5 and up.

This method for removing Arabic diacritics is based on the charCode and fromCharCode methods of the String class in ActionScript.

The thing is, all Arabic specific diacritics have an AScii char code between 1611 and 1618, Arabic characters as you might know are double bytes characters and thus each character is represented by two bytes instead of one as it is the case of latin characters.

So in pseudocode, this is how to remove the diacritics dynamically:

- start by storing all the charcodes of the input text in an Array. You will have to make a for loop for that to grab them using the charCodeAt method of the String class.

- loop through the Array and check the charcodes, if a charcode is between 1611 and 1618 just omit it, store the results in a new Array.
- loop through the purified Array and convert the charcodes back to characters using the fromCharCode method, store the results in yet a new Array.

- Finally , joing the elements of the last array and voila!

Below is a sample of the diacritics remover actionscript in action.

Diacritics Stripper