Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Archive for the ‘ActionScript3.0’ Category

Extract Bitmap from a MovieClip

with one comment

A code snippet to extract Bitmap from a MovieClip object:

/**
* This returns a Bitmap representation of an area
* of a DIsplayObject from the application.
*
* @param source the DisplayObject whose Bitmap is to be generated
* @param area the rectangular part of the DisplayObject to be considered
* for image creation. The default value is <code>null</code> which denotes

the whole image.
*
* @return A bitmap representation of the DisplayObject maked by a

Rectangular area
*/
static public 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

November 30, 2011 at 12:52 PM

Posted in ActionScript3.0, Flash, Flex

Proportionate scaling of MovieClips

leave a comment »

There have been few instances where I had to resize a MovieClip maintaining the ascpect ratio. And every now and then I had to make temporary codes as was unable to locate where I wrote the last time. So guys I have now shared it here so that I atleast do not get lost:

static public function scaleMaintainAspectRatio( object:DisplayObject, targetWidth:Number, targetHeight:Number ):void
{
var scale:Number = 0;
var objectWidth:Number = object.width;
var objectHeight:Number = object.height;

if( targetWidth == 1 || targetHeight == 1 ){
if( targetWidth == 1 && targetHeight == 1) {
//targetWidth = 1; targetHeight = 1
//do nothing

} else if( targetWidth == 1 ){
//targetWidth = 1; targetHeight > 1
object.height = targetHeight;
scale = objectWidth / objectHeight;
object.width = targetHeight * scale;

} else if( targetHeight == 1 ) {
//targetWidth > 1; targetHeight = 1
object.width = targetWidth;
scale = objectHeight / objectWidth;
object.height = targetWidth * scale;
}

} else {

if( objectHeight > targetHeight || objectWidth > targetWidth){
if( objectWidth > objectHeight ){
scale = targetWidth / objectWidth;
object.width = targetWidth;
object.height = objectHeight * scale;

if( object.height > targetHeight ){
scale = targetHeight / objectHeight;
object.width = objectWidth * scale;
object.height = targetHeight;
}
} else {
scale = targetHeight / objectHeight;
object.width = objectWidth * scale;
object.height = targetHeight;

if( object.width > targetWidth ){
scale = targetWidth / objectWidth;
object.width = targetWidth;
object.height = objectHeight * scale;
}
}
}
}

Written by Deepanjan Das

November 30, 2011 at 12:18 PM

Posted in ActionScript3.0

Global TImer in ActionScript

leave a comment »

With my experience with RIA using actionscript, I have come most applications where multiple timers are required.
As the application grows complex, the more and more timers added does slow up the application performance to lot.

That is where I thought of developing a utility class called GlobalTimer. It is a singleton class and runs only a single timer. So where ever in your application a timer is necessary, just add a timer name to this global timer with other parameters and you are done without hitting the application performance.

This class is a pure actionscript class and hence can be used both for flash, flex and AIR projects.

GlobalTimer.as

package com.te.utilities
{
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Dictionary;
import flash.utils.Timer;

public class GlobalTimer extends EventDispatcher
{
public static const EQUALS : String = "equals";
public static const DIFFERENCE_FROM_CURRENT_TIME : String = "differs";
public static const CALL_IN_INTERVALS : String = "intervals";

public static var TIME_INTERVAL : int = 100;
public static var instance : GlobalTimer;

private var timer:Timer;
private var currentDate:Date;
private var currentTime:int;//in milliseconds
private var _running:Boolean = false;

private var timersForCheck:Dictionary;

public function GlobalTimer( enf:SingletonEnforcer )
{
timersForCheck = new Dictionary();

super();
}

public static function getInstance() : GlobalTimer
{
if( instance == null ) instance = new GlobalTimer( new SingletonEnforcer() );
return instance;
}

public function start() : void
{
if( _running )    return;

timer = new Timer( TIME_INTERVAL );
timer.addEventListener(TimerEvent.TIMER, onTimerHandler, false, 0, true );
timer.start();

_running = true;
}

private function onTimerHandler( event:TimerEvent ) : void
{
currentDate = new Date();
currentTime = ( currentDate.getHours() * 60 * 60 ) + ( currentDate.getMinutes() * 60 ) + ( currentDate.getSeconds() );

for each( var prop:* in timersForCheck ){
if( prop != null ){
switch( prop[2] ){
case EQUALS:
if( currentTime == ( prop[0] + prop[1] ) ){
//AUTO UPDATE CHECK
prop[3][ String( prop[4] ) ]();
}
break;

case DIFFERENCE_FROM_CURRENT_TIME:
//IDLE TIME CHECK
if( prop[5] != null ){
prop[3][ String( prop[5] ) ]();
}
if( ( currentTime - prop[0] ) == prop[1] && prop[4] != null ){
prop[3][ String( prop[4] ) ]();
}
break;

default:
//CALL_IN_INTERVALS:
if( ( timer.currentCount - prop[0] ) == prop[1] && prop[5] != null ){
prop[0] = timer.currentCount;
prop[3][ String( prop[5] ) ]();
}
break;
}
}
}
}

public function addTimer( name:String, time:Number, check:String, object:*, functionOnComplete:String=null, functionOnTimer:String=null ) : void
{
//timersForCheck[name] = [   0,      1,    2,     3,            4,                 5        ];
timersForCheck[name] = [ timer.currentCount, time, check, object, functionOnComplete, functionOnTimer ];
}

public function removeTimer( name:String ) : void
{
timersForCheck[name] = null;
}

public function isTimerRunning( name:String ) : Boolean
{
var prop:* = timersForCheck[ name ];
if( prop == null ){
return false;
}
return true;
}

public function getTimerData( name:String ) : Object
{
var prop:* = timersForCheck[ name ];
if( prop == null ){
return null;
}
var data:Object = {};
data.currentTime = ( currentTime - prop[0] );
data.remainingTime = ( prop[1] - data.currentTime );
return data;
}

public function stop() : void
{
if( timer != null ){
timer.stop();
timer.reset();
timer.removeEventListener( TimerEvent.TIMER, onTimerHandler, false );
timer = null;

_running = false;

for each( var prop:* in timersForCheck ){
if( prop != null ){
prop = null;
}
}
timersForCheck = null;
timersForCheck = new Dictionary();
}
}

public function get running() : Boolean
{
return _running;
}

protected function reset():void
{
stop();
}
}
}
internal class SingletonEnforcer{}

In the application use the following code:

//Code to be used in your application
private var globalTimer:GlobalTimer;

globalTimer = GlobalTimer.getInstance();
if( !globalTimer.running ){
globalTimer.start();//starts timer
}

//checks if the timer is already running
if ( !globalTimer.isTimerRunning( "game_timer" ) ) {
globalTimer.addTimer( "game_timer", _asuraThrowWeaponInterval, GlobalTimer.CALL_IN_INTERVALS, this, null, "onIntervalEventHandler" );
}

globalTimer.removeTimer( "game_timer" );//removes/clears the particular timer
globalTimer.stop();//stops all the timers

Written by Deepanjan Das

November 22, 2011 at 1:22 PM

Layout mirroring in Flex4

with 3 comments

Today I came across a strange problem (mostly due to lack of knowledge in LayoutDirection.as).
I had a mx.controls.VideoDisplay component in a class which extends Sprite in Flex4.
Now when I add the videoDisplay to the container, I found that the video did not show up, but the sound was playing in behind, which suggests that the visual elements are not added.
After a long search I found that I was just trying to add a UIComponent to a Sprite which is not at all possible. And I am sure some of the developers like me also faces the same problem for the first time (though a silly mistake).
So I added spark.core.SpriteVisualElement and added the video to it and it started showing.

Now the actual stuff, to my surprise the video was showing flipped. After much pondering over net and studying the UIComponent class I came to know about the LayoutDirection class, by which any UIComponent and IVisualElement can be flipped. A detailed explanation of the same can be found here.

Here is the code snippet I used.

import mx.controls.VideoDisplay;
import mx.events.VideoEvent;
import spark.core.SpriteVisualElement;
import mx.core.LayoutDirection;

var container:SpriteVisualElement = new SpriteVisualElement();
var fPlayer:VideoDisplay = new VideoDisplay();
fPlayer.maintainAspectRatio = true;
fPlayer.width = 600;
fPlayer.height = 400;
fPlayer.layoutDirection = LayoutDirection.LTR;
container.addChild( fPlayer);

Written by Deepanjan Das

March 17, 2011 at 10:30 AM

Posted in ActionScript3.0

StageVideo – A new way to render video

leave a comment »

Until now, Flash Player used to decode H.264 video data back from the GPU to the CPU and use the CPU to render the video. With the out of Flash Player 10.2, comes the Stage Video feature. It enables the GPU to render the video directly on the display without the overhead incurred as previously. So to get the best performance, use H.264 encoding for the video and Stage Video for displaying the video. Read more

Written by Deepanjan Das

February 23, 2011 at 9:51 PM

Private variables accessible in ActionScript3.0

leave a comment »

While browsing through some blogs I came over this fantastic blog on ActionScript. I bet, you all will like it too.
A new thing I learned today is that a variable declared private can still be accessed from a class by another instance of the same parent class. I am sure you also did not believe me like I did until I tested it myself as well. Check this out.

Written by Deepanjan Das

February 19, 2011 at 10:17 PM

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