Saturday, February 20, 2010

Comparing two objects in jQuery

Had a strange problem in comparing two objects in js. I am dynamically creating a number of divs, adding them to the DOM and also placing them into an array in order to keep tack of what divs are hidden or shown at a given time. Later, I tried to loop through all of the divs and compare them using the === operator but they never returned true -- even though both objects were divs and even showed the same innerHTML.

Anway, a Google search turned up a suggestion to add this function to the jQuery prototype:

$.fn.equals = function(compareTo) {
if (!compareTo || !compareTo.length || this.length!=compareTo.length)
{
return false;
}
for (var i=0; i
if (this[i]!==compareTo[i]) {
return false;
}
}
return true;
}

Problem fixed!

Tuesday, February 16, 2010

More problems with the AS3 Loader Class

I've built an AS3 preloader so a Flash app can preload assets before starting. The app uses a number of Loaders to load about 15 swfs and when the LoaderEvent.INIT is fired it increments the index. When all of the assets have loaded, it starts the app and adds the contents of the Loaders to the display list:

myclip.addChild(MovieClip(myLoader.content);

However, when the app is run from the web occasionally it throws an error that a myLoader.content is null -- so it can't be added to the display list.

I assume that this means that a false INIT event is being fired -- or the player is not always seeing that the content is available to add.

In any event, I wound up setting a Timer that waits 2 seconds before adding the content to the display list. So far this seems to solve the problem for me -- but not the bug....

Saturday, February 6, 2010

Great javascript editor - Aptana Studio

I have been using Dreamweaver for over 10 years and never thought I would need another html code editor -- and ponied up the price for updates every few years. But now that I'm spending increasing time doing javascript development I found that it just wasn't that good for code completion/hinting/documentation etc. After a bit of searching I came across Aptana Studio and absolutely love it. It's built on Eclipse and has everything I could hope for js development -- including being able to install plugins for various frameworks like JQuery, DoJo and more. Plus you can even add a plugin for AIR development.

To install the JQuery plugin just go to:

Help>Install Aptana features>Ajax>JQuery Support

Did I mention that its free?

Thursday, January 28, 2010

IE7 javascript error in for each loops

One of the reasons I like Flash is the fact that one piece of code will work on every browser. But not so with javascript. Case in point:

If you have an array (langArray) in javascript and want to loop through it the common way is this:

for each(var item in langArray){
...
}

but this causes IE 7 to choke. Instead you have to use the more verbose:

for(var item=0;item var item = langArray[i];
...
}

Thursday, January 21, 2010

Flash elements disappear in full screen mode

I was surprised to find that a button and a dropdown list disappeared when I went full screen on a Flash app. Turns out that if you use an FLVPlayerback component it forces everything else on screen to disappear if you go fullscreen. Nice feature unless you don't want that to happen.

To fix it:

myFlvPlayback.fullScreenTakeOver = false;


Thanks Adobe forums :-)

Thursday, January 7, 2010

Working with AS3 Video cuepoints

I have been working with cuePoints in flash video and so far have been unable to add actionscript cuePoints and then seek to them. (Update: see solution below)
  • Encode a video: "myVideo.flv" without embedded cuePoints
  • Attach the video to a FLV playback component "my_FLVPlybk"
  • Add cuepoints like this: my_FLVPlybk.addASCuePoint(15, "Cue1");
  • Set up an event Listener to see if the cuepoints are there:
my_FLVPlybk.addEventListener(MetadataEvent.CUE_POINT, cp_listener);

function cp_listener(eventObject:MetadataEvent):void {
trace("Elapsed time in seconds: " + my_FLVPlybk.playheadTime);
trace("Cue point name is: " + eventObject.info.name);
trace("Cue point type is: " + eventObject.info.type);
}

When you run the app, it all works expected. Once the cuepoints are reached it traces out each point. However when you try to go to the cuepoint using this method:

my_FLVPlybk.seekToNavCuePoint("Cue1");

It fails with this error:

VideoError: 1003: Invalid seek
at fl.video::FLVPlayback/seekToNavCuePoint()

Now, this last bit of code works fine if you embed navigation cuepoints in the video itself -- using Adobe Media Encoder (FLV only -- F4V seems not to support cuepoints--at least not well), but according to Adobe it should be possible to add the cuepoints and reference them through actionscript only. In this particular case its not a problem for me since its easier to add the cuepoints to the encoded video, but if anyone has come up with a solution, please post it!


***************************

Update: I found the solution on another forum (http://bit.ly/8EdgdN):

The answer is to locate the cuePoint using findCuePoint() -- which returns an associative array of the point -- and then get the value of "time". At this point you can just do a seek. The final code would be like this:

my_FLVPlybk.seek(my_FLVPlybk.findCuePoint("Cue4", CuePointType.ACTIONSCRIPT)["time"]);

Wednesday, December 16, 2009

Making sense out of MVC in Flex

Now that the video project is nearly out of the way, I'm anxious to get back more to Flex and to do this MVC is a top priority. Looking around the web there are a lot of tutorials but most focus on using frameworks like Cairngorm and PureMVC and very light info on the concepts of MVC and how they are implemented in AS3. Fortunately I stumbled upon this one--from the writers of an OReilly book on the subject -- and it's so good I'm thinking about buying the book:

http://www.as3dp.com/category/design-patterns/mvc/