Nobien A nerd blog about nerdy things by … nerdy guys?

26May/100

Snippet: Convert milliseconds to a time code string (00:00:00)

5Feb/095

AS3: Stopping All Timeline Animations

I haven't posted in months for good reason. I've been involved in a pretty large project at Almighty that led to many late nights. All in all though, the end product turned out to be pretty awesome. I'll post something more on it later. Until then, I hope to roll out some snippets of code that I found useful during the project.

The following snippet of code is a recursive loop that halts all timeline-based animations (assuming they're on a gotoAndStop() loop. This came in handy when working with animation designers.

function haltAllAnimations( mc:MovieClip = null ):void
{
    if( !mc ) mc = this as MovieClip;
 
    for(var i:int = 0; i < mc.numChildren ; i++)
    {
         if( (mc.getChildAt( i ) is MovieClip) == false ) continue;
 
        MovieClip( mc.getChildAt( i ) ).stop();
        if( mc.numChildren > 0 )
            haltAllAnimations( MovieClip( mc.getChildAt( i ) ) );
    }
}
14May/086

Duplicate a DisplayObject as a BitMap

In working on a project, I created a simple utility class that has this public static method. This basically takes a DisplayObject, copies it, and adds it to a new Sprite which is then returned to the caller. Simple enough. I'm sure this can be refined or done a different way, so have at it.

public static function duplicateImageAsSprite( original:DisplayObject ):Sprite
{
    var bitmapData:BitmapData = new BitmapData( original.width , original.height ,
        true , 0x000000 );
    bitmapData.draw( original as IBitmapDrawable );

    var bitmap:Bitmap = new Bitmap( bitmapData );

    var returnSprite:Sprite = new Sprite();
    returnSprite.addChild( bitmap as DisplayObject );

    return returnSprite;
}
3Dec/070

The Obligatory Snippets Post

There are a bunch of code snippets that I always forget. In an attempt to organize my life, I'm going to throw them up here. Hopefully Matt will too.