Saturday, July 26, 2008

Performance Benchmark on Resetting an Array in ActionScript 3

_localArray.length = 0; // Reset the array to empty

Yesterday I was coding on a rather large application at work and I needed to reset an array I had that was stored as a member variable. My gut instinct was to just take the Array variable and set its length = 0. Although this worked I wasn't sure if it was the best way to reset an array in ActionScript 3. So I asked my friend Guy Wyatt and he sent me this performance benchmark using 100,000 iterations in an Array as a test to see the time it took to clear out this Array using various techniques.

Resetting an empty array over 100,000 iterations in a while(i--) loop
length = 0:  15 ms
splice:  157 ms
new:  145 ms

So it looks like my gut instinct was right to set the length to 0 to reset my Array. I know this isn't the most glamorous blog post but I do think it is important to think about these things while coding a large Flex application. Every little bit of optimization is crucial and can over all help yield a better functioning application.

Follow Up:

It's worth noting that after some digging the Flex Framework's ArrayCollection removes all of the items from an array using this code in the ArrayList.as class:

/**
     *  Remove all items from the list.
     */
    public function removeAll():void
    {
        if (length > 0)
        {
            var len:int = length;
            for (var i:int = 0; i < len; i++)
            {
                stopTrackUpdates(source[i]);
            }

            source.splice(0, length);
            internalDispatchEvent(CollectionEventKind.RESET);
        }   
    }

 

I'm not sure if this is the recommended way of doing things for clearing items from an Array but it appears setting length to 0 works and also takes less time.


Tuesday, July 22, 2008

Weird Alpha Tweening with 9-Sliced MovieClips in Flash CS3

I was helping coworker Ryan Swanson do a timeline based presentation / animation in Flash CS3 today. Doing timeline animations in Flash is something I rarely do anymore and usually try to do it in code, but since the presentation is due tomorrow there was no real need to build some OOP'd out craziness. It just had to move and flow and it had to be done quickly. Anyway, we noticed today that if you make a MovieClip that is 9-Sliced and you are trying to animate the Alpha of that MovieClip you won't see it animate in the IDE at design time. At first we just thought it was some bug or didn't work. But later on this evening I did a quick test and then ran it and it appears that it works but just doesn't display the Alpha tween in the IDE while you are working unless you apply something like a rotation transformation. I'm not sure if anyone else has seen this or even cares. Honestly, I really don't care that much since I don't do timeline animations that often, but I thought I'd mention it just in case someone reads this that does care. So if you happen to run into this situation and you think your animation is jacked. Well it might be hehehe, but if you run your movie, it should compile the proper animation for your 9-Sliced MovieClip in the Flash movie.


Friday, July 18, 2008

Scaling the mx:Image Object Down in Flex Successfully

Flex Image Isn't Perfect But it can be Controlled 

Just the other day I was doing a very basic photo gallery in Flex and ran into weird issues with the Image component when I tried to resize the image width and height properties. I kept getting the bounding box of the original image size but the image itself would scale down. You can see by the image above what I'm talking about. It took a little while to finally realize that by using the scaleX and scaleY properties on the image, it got resized and so did the bounding box of the image container in my Flex application. I'm not sure if this is common but it seemed to work for me in this situation. If you have a better way then please let me know. Also Ely Greenfield's SuperImage is really nice also for using images in Flex as it provides some nice enhancements. Well, here is the code used to make this example illustrating the scaling of the Flex Image properly.

Basic MXML Code Illustrating the Image Resizing in Flex:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" width="635" height="480">
<mx:Script>
<![CDATA[
[Embed(source="images/ipodChunker.swf", symbol="IpodChunker")]

[Bindable]
public var ChunkRock:Class;

]]>
</mx:Script>
<mx:Label text="Using scaleX and scaleY to Scale Your Images Down in Flex" fontSize="15" />
<mx:HBox width="100%" paddingTop="10" paddingLeft="10">
<mx:Spacer width="100%" />
<mx:VBox width="100%" height="100%">
<mx:Box borderColor="yellow" borderStyle="solid" borderThickness="3">
<mx:Image id="chunker1" source="{ChunkRock}" width="40%" height="40%"/>
</mx:Box>
</mx:VBox>
<mx:Box borderColor="yellow" borderStyle="solid" borderThickness="3" horizontalAlign="left">
<mx:Image id="chunker3" source="{ChunkRock}" scaleX="1" scaleY="1"/>
</mx:Box>
<mx:Spacer width="100%" />
</mx:HBox>
<mx:HBox width="530" paddingTop="10" paddingLeft="10" verticalAlign="middle">
<mx:Box borderColor="yellow" borderStyle="solid" borderThickness="3">
<mx:Image id="chunker2" source="{ChunkRock}" scaleX=".4" scaleY=".4"/>
</mx:Box>
<mx:Text text="Animation above uses percentages on image dimensions. The image to the left uses scaleX and scaleY. The animation on the top right is the actual image size. If you have issues scaling your images down you can trying using scaleX and scaleY. Also you can use the SuperImage Component developed by Ely Greenfield." width="100%" height="100%"/>
</mx:HBox>
</mx:Application>


Click to Download the Project Files >>


Monday, July 14, 2008

Get Ready For SOFTIMAGE | XSI 7

 XSI 7

Anyone who is really into 3D or graphics probably knows about this already or has seen the screen shots but for anyone who hasn't heard the news, Softimage has announced SOFTIMAGE|XSI 7, the latest version of its professional 3D animation software. Just watch some of the video embedded below and click the link to the SoftImage website and see the new features for yourself. This is a very exciting time to be alive as the tools we work with are evolving at a rapid pace, allowing us as designers and developers to do more than ever before.

Check out XSI 7: http://www.softimage.com/products/xsi/

 

Watch XSI 7 Videos Below

 

 

 

 

Related XSI 7 Links:

New Features in XSI 7

CG Society XSI 7 Overview

Softimage XSI 7 thread on CGTalk

Digital Tutors - XSI 7


Sunday, July 6, 2008

Simulation, Consciousness, Existence - Hans Moravec, 1998

The Simulation, Consciousness, Existence - Hans Moravec, 1998 was sent to me by co-worker Kelly Harmon, who is a brilliant software programmer and thinker. I found this to be a really interesting read and wanted to share it with anyone who is into reading this kind of stuff, especially topics of Artificial Intelligence and Perception and so on.

Quote - "If our world distinguishes itself from the vast unexamined (and unexaminable) majority of possible worlds through the act of self-perception and self-appreciation, just who is doing all the perceiving and appreciating? The human mind may be up to interpreting its own functioning as conscious, so rescuing itself from meaningless zombiehood, but surely we few humans and other biota---trapped on a tiny, soggy dust speck in an obscure corner, only occasionally and dimly aware of the grossest features of our immediate surroundings and immediate past---are surely insufficient to bring meaning to the whole visible universe, full of unimagined surprises, 10^40 times as massive, 10^70 times as voluminous, and 10^10 times as long-lived as ourselves. Our present appreciative ability seems more a match for the simplicity of Saturday-morning cartoons."

Anyway's just read for yourself, you might like it:

http://www.frc.ri.cmu.edu/~hpm/project.archive/general.articles/1998/SimConEx.98.html

 

More Great Things to Read:

Consciousness and the Different Types of Simulation >>

http://www.frc.ri.cmu.edu/~hpm/hpm.pubs.html

http://www.dmoz.org/Society/Philosophy/Philosophy_of_Mind/