Flex: ObjectUtil.copy() throws errors for classes with custom namespace properties.

February 26th, 2008

The easiest way to clone an instance of a class in Flex is to use the inbuilt ObjectUtil.copy() . This function serialises the entire object into an array of bytes and then deserialises it back into a completely new instance of that class. All that needs to be added to a class to ensure that it is correctly deserialised is to include the [RemoteClass] metadata tag above the class definition.

Rather than explaining it all here, I suggest you have a read of Darron Schall’s explanation of it here.

I’ve used this method for cloning custom objects in several projects without issue, but when I implemented this functionality in a current project, everytime I called ObjectUtil.copy() I received the following error:

ArgumentError: Error #2004: One of the parameters is invalid.
    at flash.utils::ByteArray/writeObject()

There is only one parameter for ObjectUtil.copy() - the object to be copied, so obviously there was something in one of my classes that didn’t like being deserialised. Unfortunately by this time there was already a reasonable inheritance chain going in my classes, and eventually I had to start stripping bits out of my classes (and superclasses) until the error went away.

It turns out that the culprit was a custom namespace property in my superclass. Whenever the namespace was defined, ByteArray.writeObject() fails to deserialise the byte array, and the copy fails.

package
{
    [RemoteClass]
    public class Example
    {
        public var ns:Namespace = new Namespace(”http://www.misprintt.net”);

        public function Example()
        {

        }
    }
}

Cloning an instance of this class will always throw an error:

var example:Example();= new Example();
var o:Object = ObjectUtil.copy(example);

Luckily the solution is also describe in Darron’s post. While he doesn’t indentify what problems he was having, he explains how he used the [Transient] metadata tag to avoid issues with ObjectUtil.copy().

So now that the culprit has been identified, it is just a matter of adding the [Transient] metadata tag to avoid the namespace being serialised in the first place.

package
{
    [RemoteClass]
    public class Example
    {
	[Transient]
        public var ns:Namespace = new Namespace(”http://www.misprintt.net”);

        public function Example()
        {

        }
    }
}

Anyway, hopefully this saves some time for those trying to decipher yet another one of those vague flash player errors :)

Limitations of Drag And Drop animations in Flex

November 11th, 2007

One of the key benefits of using Flex for the end user is a consistent experience when interacting with on screen elements. While the look/skin can vary greatly, the underlying behaviours remain consistent, making the site or application more usable. While Flex provides a default implementation to each step in an interaction, it generally allows customization of the visual state through the various events associated with each component type. Hence we have custom animations and transitions between states to implement the most appropriate behaviour in an individual project.

A good example of this is Flex’s inbuilt Drag and Drop. Generally on the web, drag and drop functionality varies greatly from implementation to implementation. Many cases don’t provide enough visual feedback to the user to fully utilise the benefits of a drag and drop system – easy, direct manipulation of information on screen.

Drag and Drop functionality within Flex is pretty solid. It works out of the box with inbuilt components, and adding it to custom components is relatively straight forward (once you understand the order of all the events in a drag-drop interaction by reading this PDF- http://blogs.adobe.com/flexdoc/pdf/dragdrop.pdf). It also allows for a reasonable amount of customization to the visual states during a drag-drop process, with the exception below.

Flex provides two possible behaviours when an object is dropped:

  1. The drop is rejected and the dragged item animates back to it’s original position
  2. The drop is accepted and the dragged item animates into the new position.

These behaviours are correct, however there is no means to override the actual animations that occur as a result of these behaviours, so we are stuck with the following “zoom to mouse position” transition when a drop is accepted (from mx.managers.dragClasses.DragProxy) :

 

 

// Zoom into mouse location to show drag was accepted.
var e:Zoom = new Zoom(this);
e.zoomWidthFrom = e.zoomHeightFrom = 1.0;

e.zoomWidthTo = e.zoomHeightTo = 0;

e.duration = 200;

e.play();

var m:Move = new Move(this);
m.addEventListener(EffectEvent.EFFECT_END, effectEndHandler);

m.xFrom = x;

m.yFrom =
this.y;
m.xTo = parent.mouseX;

m.yTo = parent.mouseY;

m.duration = 200;

m.play();

This transition may work well when dragging data from one list to another (which is the most common use case when using the inbuilt components), but can look a bit much when dragging a large container around the screen.

You drag an object from one area of the screen to another, and when you release the mouse it scales down into obscurity before appearing in its new position. There should be a means to override this default effect.

In this example try dropping the item in the bottom right corner of one of the (white) drop areas. This transition looks wrong as the mouse coordinates are not always the intended final destination of the dropped object.

Basically there should be some way to define or override the animation that occurs on a successful drop event. It seems surprising that this animation is hardcoded within the framework, while every other visual state during the drag/drop operation is customisable.

I know it is hardly a show stopper, but I’ve submitted an enhancement request to the Flex Bug and Issue Management System (https://bugs.adobe.com/jira/browse/SDK-13472) as it is an UI issue that has already arisen in one of our commercial projects.

- D

As predicted - Banner ads take over the internet

October 30th, 2007

Banner ads take over the internet (small)

Click on the image for the full sized version.

And if you don’t believe me - here’s the evidence: http://coldfusion.sys-con.com/read/450703.htm

The International War Crimes tribunal is probably not the right place to report atrocities such as this, but I’m sure it breaks some part of the Geneva Convention.

And why do so many people blame Flash when they see a page like this? Flash is just the tool, not the actual tool who thought it was a good idea to combine a floating ad, an expanding ad, two video ads, and three banner ads on the top of a single page (and there is more if you scroll down).

There are so many guilty parties involved - including the webmaster, the online advertisers, and especially the person who invented the eyeblaster.

Rant over and out,

Dom

Nasty FDT bug - renaming a project with “refactoring” selected

April 29th, 2007

The FDT plugin for Eclipse is a great piece of software (way way better than other flash plugins for Eclipse such as ASDT and FlexBuilder). I use it for all my AS2 flash development. However it has always had some issues with the location of the intrinsic core ActionScript classes in Flash 8 because they reside in a FP7 or FP8 directory (see forum post here and solution here).

While this solution solves the problem of FDT not recognising intrinsic classes such as Object and String, It doesn’t resolve a very nasty potential bug.

DON’T rename a project if you have selected “Refactoring” - “Flash Explorer File Operations” in the FDT preferences.

FDT preferences

What happens is it refactors all the intrinsic classes in the core library and renames all the classes in the FP7 and FP8 folders as if the folders were package names. E.g. Object becomes FP8.Object, String becomes FP8.String.

This causes FDT all kinds of problems, and Eclipse hangs while trying to refresh the workspace (while consuming 100% CPU).

This has happened to me on several occasions and it took me hours to work out the issue - i tried cleaning out all the cache and project metadata from eclipse, I tried reinstalling the FDT plugin, all with no success. I eventually set up a new workspace and imported a single project and eventually discovered what had happened.

The only solution is to copy over the core classes (C:\Program Files\Macromedia\Flash 8\en\First Run\Classes) with a fresh copy, or go through and re-edit each intrinsic class.

I’ve posted the bug here.

to Boolean || not to Boolean

April 23rd, 2007

I was looking for an simpler way of setting default values for function parameters in ActionScript2. The way I usually do this is something along the lines of:

function set value(s:String):Void
{
if(s == undefined) _value = s;
else _value = "default";
}

One option is using a logical OR operator to set default values. For example:

function set value(s:String):Void
{
_value = s || "default";
}

According to the flash LiveDocs a non-Boolean logical OR operation first converts the first operand (i.e. the expression on the left of the ||) to Boolean and if true returns the resolved value of the first operand. Otherwise it will return the resolved value of the second operand (expression on the right of the ||)

In the use case above it works fine, but after exploring the non-boolean implementation of the OR operator we found the following behaviour. In both the string literal (”hey”) and the string variable (s), the Boolean value equals true, but when placed in a logical OR operation they return different results. See below:

var s:String;
s = "hey";
trace(Boolean(s));//true
trace(Boolean("hey") //true
trace("hey" || "dude");//"dude"
trace(s || "dude");//"hey"

Can anyone explain why the String literal is treated differently than a variable containing a String literal?

Oh and word up to Ian on this one for his input :)

[UPDATE] I know I could have gone with “toBoolean() || !toBoolean()” for the title but I think that is taking it a little too far.

[UPDATE 2] The only gotcha with this approach is with numbers as a value of zero converts to false.

Taming the code library

March 29th, 2007

I attended WEBDU the other week and had some thoughts after listening to one of the sessions - Geoff Bowers’ “taming the code”.

Amazingly over half the people in the audience weren’t using any form of version control. While we’ve been using version control for a few years now on our flash projects, It made me think about the development methodologies and processes that we have developed in the flash team at Massive over the last few years, specifically in relation to maintaining a common centralised code base.

The way i see it, there are two types of common code libraries - constant and evolving.

Constant code base
This comprises of functionality that rarely changes. Good examples of this are utility classes (e.g. trimming white space on a String or a generic XML parser). These classes are added to over time (in the case of utility classes as new conversions/formats are required), but the usage of existing methods remains the same across all projects - the returned format of a NumberUtil.toDollarsAndCents() method is always going to need to return a string with 2 decimal places.

Evolving code base
This is the common code base that evolves over time. At massive this is primarily the frameworks we use for our applications. While there is little change individually from project to project, over time our processes evolve and all the subtle improvements add up to a distinctly different framework. A project that is only 6-12 months old looks and behaves quite differently to a project started today.

Despite this, it is still necessary to version control the code base for our frameworks - because the alternative is to copy and paste core classes by hand every time we start a new project - yuck! At the same time we don’t want to have to maintain backwards compatibility in our common code base just in order to be able to recompile old projects.

Solution

Our solution to this problem has been to not version control the actual classes for our frameworks, but instead version control scripts that can automatically generate them. This way each project has its own version of the core classes that are fixed within that project, and every new project contains the most recent set of classes and structure.

There are heaps of benefits to this approach - not only do we not have to worry about breaking old projects - but we gain an extra level of standardisation across our projects no matter who initially creates them. Setting up a new project by hand can take hours and is often the most tedious part of the process. Automating this process saves time and heaps of monkey work.

Because we primarily use Eclipse for our actionscript development, we’ve been using Ant for all our build scripts (Eclipse has inbuilt support for it). Initially our scripts were responsible for creating a few folders, and some stub core classes. Over time we have extended this further to auto-generate project specific build scripts for both compiling a project and also creating specific class types on demand.

jacksonpollock.org

July 4th, 2006

Create your own jackson pollock masterpiece (click the mouse to change colour).

It’s an interesting variation on the various flash drawing tools that are out there.
http://jacksonpollock.org/

jackson.jpg

html web page visualiser

May 28th, 2006

Interesting little applet that visualises an html page as an organic tree. Each colour represents some type of element in the html (purple is an image, blue is a hyperlink, red is a table, yellow is a form, etc)

http://www.aharef.info/2006/05/websites_as_graphs.htm

The complex the web page the more complex the image that is created. Check out the difference between google and ninemsn. The ninemsn site nearly crashed my browser!

visualiser_google.jpg

visualiser_ninemsn.jpg

interact 10 ways - space

May 23rd, 2006

Space is one of the projects for getty images’ interact10ways.com.

It threads various images in a three dimensional canvas where the user chooses which “path” to travel. While it is actually pretty linear, the animation and style works nicely.
It is created by thebarbariangroup.

Screenshots [1][2][3]

space.jpg

Still The One

May 22nd, 2006

It may be an Australian first, but the new channel 9 “catch-up tv” download service definately lives up to it’s name - it REALLY needs to “catch up”.

It looks terrible, almost as if Eddie knocked it up himself in his spare time…

Take a look at a couple of choice shots [1[2][3]
Follow the “download now” link here.

channel9_catchup_tv.jpg

an_australian_first1.jpg