Symmetri Developer Blog

February 14, 2008

Weird security problems with SoundMixer.computeSpectrum()

Flash/Flex/Actionscript - By Shourov Bhattacharya

Ever tried to use SoundMixer.computeSpectrum() on a dynamically loaded audio file in AS3? I have run into some strange security problems. In my Flash CS3 IDE, it works fine the first time I run the movie, but then shutting down the movie and running it again throws a security exception “SecurityError: Error #2121: Security sandbox violation: SoundMixer.computeSpectrum: ... cannot access .” This is despite the fact that the Flash movie definitely has explicit access to the local filesystem, and there is no real violation of the sandbox model as far as I can see.

Running the same movie in the browser, I find that IE7 with Flash Player 9 runs everything fine, but Flash Player in Firefox throws the same error.

I haven’t yet found a solution, other than catching the exception and trying to do something intelligent at that point. In my case I am using the spectrum to animate an “equalizer” type waveform clip; if the computeSpectrum() call fails, I simply fake the waveform instead (it looks surprisingly good with random data).

There is also a (probably) related problem that does not allow two separate Flash movies within the same browser process to access sound files for manipulation simultaneously - this webpage suggests that we are probably looking at a Flash Player bug for an explanation of that one. I have a feeling what I am seeing is a bug as well …

February 8, 2008

AS3 event handler methods now retain class scope

Flash/Flex/Actionscript - By Shourov Bhattacharya

Another reason to use AS3 rather than AS2 - scope within event handler functions that are implemented as class methods no longer reverts to the calling object but retains class scope (the following is copied from Adobe Flash CS3 documentation):

If you used components in ActionScript 2.0, you may remember that when a class method was passed in to UIEventDispatcher.addEventListener(), the scope of the method was bound to the component that broadcast the event instead of the class in which the listener method was defined. In other words, if you used this technique in ActionScript 2.0, the this keyword would refer to the component broadcasting the event instead of the ChildSprite instance.

This was a significant issue for some programmers because it meant that they could not access other methods and properties of the class containing the listener method. As a workaround, ActionScript 2.0 programmers could use the mx.util.Delegate class to change the scope of the listener method. This is no longer necessary, however, because ActionScript 3.0 creates a bound method when addEventListener() is called.

February 6, 2008

Simulated globals in Actionscript 3

Flash/Flex/Actionscript - By Shourov Bhattacharya

I have never really understood the argument against global variables. Yes, they can be used in sloppy ways by sloppy programmers, in which case they do contribute to making code difficult to read and maintain. But to banish them altogether seems to me to be an ideological stance rather than one driven by logic. A global variable is an absolute point of reference within the world of the application, as opposed to the “relativism”of class members and properties. There are solid reasons for sometimes wanting to have an absolute reference.

An example is the main stage of a Flash application. There is only one stage, and there may be display objects on that stage that need to be referenced from different parts of the application. Flash CS3 and Actionscript 3 have taken the ideological stance and eliminated both the _root and _global objects, so that it is no longer possible to reference instances of MovieClips (for example), using the following code:

var myClip:MovieClip = _root.clip01;

Likewise, using _global in the same way no longer works in AS3.

So , how can we reference “global” objects in AS3? We do still have the DisplayObject.root property, which refers to the root of the display tree that contains a display object. And we do, of course, have static classes, which can hold static variables of any type. Combining these two, we now have a strategy to “simulate” the _root object reference:

1) Create a static Application class to hold globals
2) When instantiating one particular DisplayObject in the application, assign its root property (which will be the main stage) to a static variable _root on the Application class
3) use Application._root from anywhere in the application to refer to the main stage

Here is the stub for the Application class:

public class Application
{
   static public var _root:DisplayObject;
}

and an example of how to assign to the _root property (assume here that Clip is the class for a MovieClip instance that is added at design time to the stage):

public class Clip extends flash.display.MovieClip
{
   public function Clip()
    {
      Application._root = this.root;
   }
}

A similar approach can be taken to store any object in the Application class as a “global” variable. You could even write a generic class wrapper that would allow you to “push” and “pop” global variables easily from an array or other data structure (I am sure this has already been done somewhere).

February 5, 2008

XML parsing in AS3

General, Flash/Flex/Actionscript, XML, Javascript - By Shourov Bhattacharya

Parsing XML documents in Actionscript 2 was never much fun. It had the flash.xml.XMLDocument class which did your standard XML DOM traversing and manipulation, but it was always a tedious task to do any sort of complex parsing that way. What a developer really wants is to convert the XML to objects, which would allow us to deal with the parsed data using native data structures such as arrays. I found an excellent AS2 solution written by a guy called Shane MacCartney called Flash XML Remoting which did exactly that, and I have used it successfully in Flash 8 projects which deal with XML data returned from web services.

However, AS3 has made things much easier. We now have access to ECMAScript for XML (E4X), a programming language that makes manipulating XML much more intuitive and appealing. Essentially, it means that we can refer to nodes and attributes within the XML using standard object dot notation without any extra effort. For example, the following example looks up the title attribute of the second book in the BookXml document:

var title:String = BookXml.book[2].@title;

Yet another example of why AS3 is a quantum improvement over AS2. Incidentally, this makes pulling XML data from the server a far more profitable exercise in AS3 than before. It used to be the case that JSON was the preferred method of passing data structures back to Flash, as it took no extra effort to parse JSON in AS. Now, both the JSON and XML options are very close in terms of programming effort. Of course, there may be other valid reasons for choosing one over the other.

February 3, 2008

Android is here

General - By Shourov Bhattacharya

Android logo

Android is the new open development platform for mobile devices that is being promoted by Google and the Open Handset Alliance. To kickstart development and create acceptance within the developer community, Google has set up the Android Developer Challenge which will reward innovative projects with cash grants totalling $10 million.

We have downloaded the Android SDK and started testing and some non-serious development. So far so good - and it is also good to see native 3D support (although we are yet to try it). It’s also given us a chance to download and use the open-source IDE Eclipse for the first time, and to write Java code for the first time in years :)

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph