Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Archive for the ‘Flash’ 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;
}

Advertisements

Written by Deepanjan Das

November 30, 2011 at 12:52 PM

Posted in ActionScript3.0, Flash, Flex

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

Flash to HTML5

leave a comment »

Adobe’s newly released “Wallaby” application that aims to expand support for Apple’s line of iOS devices, including the iPhone and iPad, by converting Flash files into HTML5.

Read more of it here.

Written by Deepanjan Das

March 9, 2011 at 8:26 AM

Posted in Flash, General, HTML5

Tagged with

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

Build file to generate swf using Adobe SDK

with 9 comments

<?xml version="1.0" encoding="UTF-8"?>
<project name="SWF Compilation using build file" default="CompileSWF" basedir=".">

<property name="FLEX_HOME" value="C:/Program Files/Adobe/Flex Builder 3/sdks/3.2.0" />

<property name="SRC_DIR" value="${basedir}/src" />

<property name="DEPLOY_DIR" value="${basedir}/bin" />

<property name="LIB_PATH" value="${basedir}/lib" />

<taskdef resource="flexTasks.tasks" classpath="${FLEX_HOME}/ant/lib/flexTasks.jar" />

<target name="CompileSWF">

<!-- Template -->
<echo>Compiles SWF</echo>
<mxmlc
file="${SRC_DIR}/BaseTemplate.as"
output="${DEPLOY_DIR}/BaseTemplate.swf"
actionscript-file-encoding="UTF-8"
incremental="true"
debug="false"
default-frame-rate="24"
default-background-color="0x25262E"
>

<load-config filename="${FLEX_HOME}/frameworks/flex-config.xml" />
<source-path path-element="${FLEX_HOME}/frameworks"/>

<library-path dir="${LIB_PATH}" append="true">
<include name="fl.swc"/>
<!--
Mention your own swc library here
<include name="library.swc"/>
-->
<include name="framework.swc"/>
</library-path>
</mxmlc>

<!-- Clears out the build directory -->
<echo message="Removing build xtras..."/>
<delete>
<fileset dir="${DEPLOY_DIR}" includes="**/*.cache"/>
</delete>

</target>

</project>

Note: You should have ant, adobe open source SDK installed in your machine.

Written by Deepanjan Das

January 5, 2011 at 8:09 AM

Apple’s revised policy will now allow apps to be written using a Flash-based tool

leave a comment »

We Flash/Flex developer, its time to enjoy! Flash is there to stay in Apple. Watch out here.

Written by Deepanjan Das

September 10, 2010 at 8:29 AM