Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Archive for the ‘Code Snippets’ Category

Screen capture with ActionScript

with 63 comments

While working over numerous projects I often had requirements for doing screen capture in AS3.0. Unfortunately I found none in google search, but got some resources which helped me to develop a component for screen capture.

With a hope that this will someone out there, I have posted it here Read the rest of this entry »

Written by Deepanjan Das

February 5, 2011 at 1:31 PM

Duplicate MovieClip in AS3.0

with 4 comments

Like duplicateMovieClip in AS2.0, here is an API to do the same in AS3.0, So just chill 🙂

/**
* usage: var dupMC:MovieClip = duplicateDisplayObject( MC1 );
* @params -  target:DisplayObject - The movieclip to be duplicated.
*            autoAdd:Boolean - default is false, setting it to true will add the new
*            duplicate movieclip to the parent of the to be duplicated movieclip
*/

public function duplicateDisplayObject( target:DisplayObject, autoAdd:Boolean = false ):DisplayObject
{
     // create duplicate
     var targetClass:Class = Object(target).constructor;
     var duplicate:Sprite = new targetClass();

     // duplicate properties
     duplicate.transform = target.transform;
     duplicate.filters = target.filters;
     duplicate.cacheAsBitmap = target.cacheAsBitmap;
     duplicate.opaqueBackground = target.opaqueBackground;
     if ( target.scale9Grid ) {
          var rect:Rectangle = target.scale9Grid;
          duplicate.scale9Grid = rect;
     }

     // add to target parent's display list
     // if autoAdd was provided as true
     if ( autoAdd && target.parent ) {
          target.parent.addChild( duplicate );
     }
     return duplicate;
}

Written by Deepanjan Das

February 1, 2011 at 8:58 AM

Clone Sprite in AS3.0

with one comment

A simple way to clone a sprite:

/**
*  @usage : var newSprite:Sprite= cloneSprite( existingSprite );
*  @params- target:Sprite- existing Sprite which needs to be cloned
*/
public function cloneSprite( target:Sprite ):Sprite
{
     // create clone
     var targetClass:Class = Object( target ).constructor;
     var clone:Sprite = new targetClass();
     return clone;
}

Written by Deepanjan Das

February 1, 2011 at 8:56 AM

Convert DisplayObject to Bitmap

with one comment

This API will return a Bitmap Image of any DisplayObject in Flash AS3.0. This was developed by Avijit Dutta, one of my friends. Hope this helps someone out there:

/**
*  @usage : var bitmap:Bitmap = BitmapUtil.getImage( source, new Rectangle( 0, 0, 100, 100 ) );
*  @params: source - DisplayObject whose Bitmap is required
*                   area - the part of the Object to be converted to an image.
*                    If the whole object is required to be converted then set null.
*/
public static function getImage( source:DisplayObject, area:Rectangle = null ):Bitmap
{
     var bitmapData:BitmapData;
     var matrix:Matrix;

     if ( area != null ) {
           matrix = new Matrix( 1, 0, 0, 1 , Math.abs( area.x ) , Math.abs( area.y ) );

           if ( area.width > 0 && area.height > 0 )
                bitmapData = new BitmapData( area.width, area.height, true, 0x0 );
           else
                bitmapData = new BitmapData( source.width, source.height, true, 0x0 );
     } else {

           bitmapData = new BitmapData( source.width , source.height , true, 0x0 );
     }

     bitmapData.draw( source, matrix, null, null, area, true );

     var bitmap:Bitmap = new Bitmap( bitmapData );
     return bitmap;
}

Written by Deepanjan Das

February 1, 2011 at 8:40 AM

Tween issues in loading AVMovie1 in AVMovie2

leave a comment »

Today I was working on an ActionScript project while I came across an interesting fact which I would like to share.
I developed a flash site in AS3.0 but due to some constraints I had to use the home page of another flash movie developed in AS2.0; so basically loading AVMovie1 in AVMovie2.

I had a Tween created in my home.swf which works well on first load within AVMovie2. But the problem started when I navigated to another section and again returned to Home. This time surprisingly none of the Tweens in home.swf worked. I checked multiple times to be sure that when I navigated, I cleared my home loader which I added to the base flash. Read the rest of this entry »

Remove all punctuations from a String

leave a comment »

var output:String = "";
var input:String = "There's a bright light seen around the corner of Cafe-coffee-day (C.C.D)!";

for (var i:int = 0; i < input.length; i++) {
    if ((input.charCodeAt(i) >= 65 && input.charCodeAt(i) <= 90) || (input.charCodeAt(i) >= 97 && input.charCodeAt(i) <= 122) || input.charCodeAt(i) == 32 ) {
        output= output.concat(input.charAt(i));
    }
}

trace(output); //Theres a bright light seen around the corner of Cafecoffeeday CCD
output = output.split( " " ).toString();
trace(output); //Theres,a,bright,light,seen,around,the,corner,of,Cafecoffeeday,CCD

Written by Deepanjan Das

October 7, 2010 at 9:04 AM

Simple YOYO for text in a textfield without tween and mask

leave a comment »

This script will allow a text to do Yoyo effect without using any Tween/mask

private var textData:String = "This is sample text to have YOYO effect";
private var textField:TextField;
private var textCount:int = 0;
private var direction:String = "LeftToRight";
private var textYoyoTimer:Timer;

public function startTextYoyo():void
{
    textField.text = textData;
    if( textField.textWidth > textField.width ){
        textCount = 0;
        textYoyoTimer = new Timer( 90 );
        textYoyoTimer.addEventListener( TimerEvent.TIMER, moveTextHandler );
        textYoyoTimer.start();
    }
}

public function stopTextYoyo():void
{
    textField.text = textData;

    direction = "LeftToRight";
}

private function moveTextHandler( event:TimerEvent ):void
{
    switch( direction ){
        case "LeftToRight":
            moveLeftToRight();
            break;

        case  "RightToLeft":
            moveRightToLeft();
            break;
    }
}

private function moveLeftToRight():void
{
    textCount++;
    textField.text = ( textData ).substring( textCount );

    if( ( textField.textWidth + 30 ) < textField.width ){
        direction = "RightToLeft";
    }
}

private function moveRightToLeft():void
{
    textCount--;
    if( textCount < 0 ){
        direction = "LeftToRight";
        return;
    }

    textField.text = ( textData ).substring( textCount );
}

Written by Deepanjan Das

September 20, 2010 at 4:49 PM