Simple Drag / Drop in flash

Multi Level Dropdowns in NewForm

Step 1: Create one MovieClip

Step 2: Paste some random image in to the movieclip

Step 3: Go back to scene1 and drag Hand Movie from library and give instance
name to this movie clip (Best practice, in AS3 instance name should be different
from movieclip name)


 –

Step4: Actionscript code (When user mouse downs on the movieclip , startDrag
function needs to be initiated. Once mouse up, stop drag function needs to be
called)

AS3
HandIns.addEventListener(MouseEvent.MOUSE_DOWN,startDragFunction)
HandIns.addEventListener(MouseEvent.MOUSE_UP,stopDragFunction)

function startDragFunction(e:Event):void
{
e.target.startDrag();
}
function stopDragFunction(e:Event):void
{
e.target.stopDrag();
}

AS2
HandIns.onPress=startDragFunction  
//On Press is equivalent to mouse down
HandIns.onRelease=stopDragFunction // on
Release is equivalent to mouse up
function startDragFunction()
{
this.startDrag();
}
function stopDragFunction()
{
this.stopDrag();
}
 

Store Object in an Array

Storing Object into an Array

First Method

var Ar:Array=new Array() //Ar is an Array and obj is an object which have two identifiers fname and lname.
var Obj:Object={fname:”Steve”,lname:”Chan”} //Define obj with two identifiers(variables) fname and lname
Ar[0]=Obj // Store obj in Ar at 0 index
trace(Ar[0].fname) //Returns Steve

Second Method
var Ar:Array=new Array({fname:”Steve”,lname:”Chan”}) //Store object in Ar at 0 index
trace(Ar[0].fname) //Returns Steve

Third Method

var Ar:Array=new Array() //Ar is an Array
var Obj:Object={fname:”Steve”,lname:”Chan”} //Define obj with two identifiers(variables) fname and lname
Ar.push(obj) // Pushing (Storing Obj in ar) starts from 0 index
trace(Ar[0].fname) //Returns Steve

Fourth Method

var Ar:Array=new Array() //Ar is an Array
Ar[0].fname=”Steve” //creating an identifier named “fname” in an Array at 0 index.
Ar[0].lname=”Chan” //creating an identifier named “lname” in an array at 0 index
trace(Ar[0].fname) // return Steve

Object Declaration

Different Way of Declaring and storing Objects

First Method

var obj:Object=new Object()
obj={fname:”Steve”, lname:”chan”}
trace(obj.fname) //Returns Steve

Second Method

var obj:Object={fname:”Steve”, lname:”chan”}
trace(obj.fname) //Returns Steve

Third Method

var obj:Object=new Object()
obj.fname=”Steve”
obj.lname=”chan”
trace(obj.fname) //Returns Steve

Fourth Method

var obj:Object=new Object()
obj[“fname”]=”Steve”
obj[“lname”]=”chan”
trace(obj[“fname”]) //Returns Steve
trace(obj.fname) //Returns Steve

Array Declaration

The Array class lets you access and manipulate arrays. Array indices are zero-based, which means that the first element in the array is [0], the second element is [1], and so on.

Different Way of Declaring and storing Arrays

First Method

var Ar:Array=new Array()
Ar[0]=”Orange”
Ar[1]=”Apple”
trace(Ar) // Returns Orange, Apple
trace(Ar[0]) //Returns Orange

Second Method

var Ar:Array=new Array(“Orange”,”Apple”)
trace(Ar) // Returns Orange, Apple
trace(Ar[0]) //Returns Orange

Third Method

var Ar:Array=new Array()
Ar.push(“Orange”)
Ar.push(“Apple”)
trace(Ar) //Orange, Apple

Fourth Method

var Ar:Array=new Array()
Ar.push(“Orange”,”Apple”)
trace(Ar) // Orange, Apple

Fifth Method

var Ar=[]
Ar.push(“Orange”,”Apple”)
trace(Ar) // Orange, Apple

Get Unique values from Array

This can be achieved (in AS2 and AS3) in a much simpler manner, using the following function or prototype:

Array.prototype.countValues = function ()
{
var z, x = this.length, c = false, a = [], d = [];
while (x–) { z = 0;  while (z < x){ if (this[x] == this[z]){ c = true; d.push (this[x]); break; }
z++;} if (!c) a.push ([this[x], 1]); else c = false; } y = a.length;while (y–) { q = 0; while (q < d.length) { if (a[y][0] == d[q]) a[y][1]++; q++; }}
return a;
};

For an example i have an array [“Orange”,”Apple”,”Apple”,”Orange”,”Pappaya”],
Would like to get the count of each values [Orange => 2, Apple => 2, Pappaya
=>1]

Once the following function is invoked for this array, it removes the all the
duplicates and generate a new Array with Unique values and it counts.

Usage

var SourceArray=[“Orange”,”Apple”,”Apple”,”Orange”,”Pappaya”]
var NewArray=SourceArray.countValues()
trace(NewArray)//Returns Pappaya,1,Apple,2,Orange,2

Find and replace in ActionScript

Here i have given two functions

  • Find – Checks whether the string holder contains the word and returns true or false
  • FindAndReplace – Checks for the string and replace with the new word provided.

var Str=“i love blogging”
var
out=Find(Str,“love”)
trace
(“result —>” +out)
var
out1=FindAndReplace(Str,“love”,“hate”)
trace
(“result1 —->”+out1)

function Find(SourceStr, FindString) :Boolean
{
var NArray=SourceStr.split(FindString)
if(NArray.length==2)
return true
else

return false
}

function FindAndReplace(SourceString, FindString,ReplaceString) :String
{
var SplitArray=SourceString.split(FindString)
var Result=SplitArray.join(ReplaceString)
return
Result;
}