I/O Reader

RSS Feed

Peter Goodman's blog about PHP, Javascript, Functional Programming, Applications, PINQ,

jQuery Inspired Actionscript Library

posted on Aug 25, 2006 at 3:23pm with two comments and tagged with Flash, Actionscript

So the other week while I was fooling around with flash, I decided to make something. It was nothing really, just a good way for me to learn how to use the Actionscript language. I got generally far when I realized: "some of the things in this language annoy me. I wish there could be a jQuery for actionscript." And so on that wish, I made a jQuery for actionscript.

Admittedly, I don't think it's as good as jQuery. It doesn't deal with multiple movie clips / arrays as sleek as a fashion as jQuery does, but it does a good enough job. Also, as far as extending the library... I just don't know if Actionscript has that kind of flexibility anymore.

You might be thinking: "how could actionscript NOT have that flexibility? Actionscript and javascript both follow the ECMAscript standard!" Well, that is true, but you need to consider how both languages are programmed. With javascript, you are forced to prototype objects because certain browsers understandings of the language's standard (*cough IE*). In actionscript, you don't have this problem. One might say that actionscript is homogeneous because Flash will always understand it in the same way. Also, prototyping in actionscript has been deprecated to more useful class and interface structures. The fact that prototyping is no longer used in actionscript means that you can't quaintly extend the functionality of objects like you can in javascript. Oh well.

Nevetheless, on to an example:

import flash.external.ExternalInterface;
var curr_anchor;
function getAnchor(anchor:String) : Void
{
    if(curr_anchor != anchor && anchor != '')
    {
        $('movie1,movie2,movie3').each(function(){
            $(this).hide();
        });
        $('movie'+anchor).show();
    }
    curr_anchor = anchor;
}
ExternalInterface.addCallback("getAnchor", null, getAnchor);
function toggleAnchor(anchor:String, fn:Function) : Void
{
    if(curr_anchor != anchor)
    {
        ExternalInterface.call("toggleAnchor", anchor); 
        fn.apply(null, [anchor]);
    }
}
var i:Number;
for(i = 1; i < = 3; i++)
{
    $($(_root).movieClip('movie'+i)).set({
        _x: 10,
        _y: 30
    }).hide().text({
        _name: 'movie_text'+i,
        _x: 0,
        _y: 0,
        _width: 100,
        _height: 100,
        text: 'Page '+i,
        size: 30,
        color: 0x000000
    });
    $($(_root).movieClip('button'+i)).set({
        _x: 20 * i,
        _y: 10,
        _i: i
    }).click(function(){
        toggleAnchor(this._i,function(z){
            getAnchor(z);
        });
    }).text({
        _name: 'button_text'+i,
        _x: 0,
        _y: 0,
        _width: 10,
        _height: 13,
        text: i,
        color: 0x000000,
        size: 10
    });
}

You might recognize that code from a previous article about accessible flash. Well, ignore all of the ExternalInterface stuff and concentrate on the $() function and how functions can be chained together just like in jQuery.

Why bother with this library? It simplifies a lot of things:

So, enough for me plugging in, here are the links for you to check out the code and to download the library.

Comments


Comment



write down what you see in the box below (all uppercase)