Dynamically add Movieclip to Scrollpane AS3

Step 1: Create Movieclip Title which needs to be added dynamically into

Scrollpane. Make sure you have checked “Export for Actionscript”. This will
automatically create Class name “Title”

Step2 : Inside Title Movie clip , insert texttool and an Image. Convert that
image to symbol and give instance name “Ico”

Instance name for TextTool is TxtValue

Step 3: Insert ScrollPane in Scene1 and give instance name MainScroll

Step 4 : Paste the code below in scene1 actionscript panel.

var S:MovieClip=new MovieClip()  // Create empty movieClipMainScroll.source=S // Assign empty movieclip to the scrollpane. It is mandatory to assign empty movie clicp to scrollpane before you do anything dynamically with scrollpanefor(var i=0;i<5;i++)   // Constructing a loop to create 5 movieclips dynamically
{
var T:Title=new Title() // Create the MovieClip (Title) and assign it to the variable T

T.y=i*30  // Set y axis value for the T
T.Txt.text=”Title” +i //Setting up the Txt value dynamic
T.Ico.id=i // Title movie clip already holds another movieclip named Ico  the movieclips
T.Ico.addEventListener(MouseEvent.CLICK,GetID) // Add eventlistener for that Ico .
MovieClip(MainScroll.content).addChildAt(T,i) //addChild will be used when u add anything to the movieclip. So here the scrollpane content needs to be treated as Movieclip , So MovieClip(MainScroll.Content) will be treated as Movieclip
}

function GetID(e:Event)
{
trace(e.target.id) // e.target points to the Ico movieclip.. Where in id been stored into Ico. So it gives you the respective id numbers.
}

Output

Codes Explanation

MainScroll.source=SAssign empty movieclip to the scrollpane. It is
mandatory to assign empty movie clicp to scrollpane before you do
anything dynamically with scrollpane
MovieClip(MainScroll.content).addChildAt(T,i)
addChild will be used when u add anything
to the movieclip. So here the scrollpane content needs to be treated as
Movieclip , So MovieClip(MainScroll.Content) will be treated as
Movieclip

attachMovie Clip in AS3

attachMovie syntax doesn’t work with Action Script 3.0. Each movieclips are considered as classes in Actionscript 3.0

Step 1: Insert Symbol -> Type MovieClip , Select export for actionscript

Step 2: Make a Note of the Class name

Step 3: Drag texttool and put some static text

Step 4: In Frame 1 paste the below code.

var MC:Symbol1=new Symbol1() //Symbol1 is the classname for the movieclip
addChild(MC)
MC.x=100
MC.y=200

Save XML in flash

This article shows how to create XML and store it into a Local Machine without any dialog box.
In order to avoid the dialog box and save with full security, Adobe Air is the best option. FileStream from Adobe Air has the reliability to save the file without any user interaction

Step 1. Create one Sample XML file in C:\test.xml with <sample></sample>

Step 2: Create two textboxes and one save button with instances name uid , pwd and saveBtn

Step 3. The code below

import flash.filesystem.*;
var LoadXML=new URLLoader(new URLRequest(“c://test.xml”)) // Loading XML file
LoadXML.addEventListener(Event.COMPLETE,LoadXMLFunction) // on load of XML calls LoadXMLFunction
saveBtn.addEventListener(MouseEvent.CLICK,onSave) // on save button click calls onSave
var MainXML:XML;function LoadXMLFunction(e:Event)
{
MainXML=XML(e.target.data) //Load all XML data from test.xml to MainXML
trace(MainXML)
}
function onSave(e:Event):void
{
var item:XML = <citem /> // Create one XML Node
item.userId = uid.text // assign uid textbox value to userId node
item.pwd = pwd.text  // assign pwd textbox value to pwd node
MainXML.appendChild(item); // Append this xml item to MainXML 

var file:File = new File();
file = file.resolvePath(“c:\\test.xml”); // open test.xml 
var fileStream:FileStream = new FileStream(); // Create new FileStream to Perform Read or Write
fileStream.open(file, FileMode.WRITE); // Open file in WRITE Mode
fileStream.writeUTFBytes(MainXML);  //Write MainXML xml content to the file test.xml
fileStream.close(); //Close the File Stream
}

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();
}
 

Save File in AS3

Opens a dialog box that lets the user save a file to the local filesystem. Although Flash Player has no restriction on the size of files you can upload, download, load or save, the player officially supports sizes of up to 100 MB.

The save() method first opens a dialog box that asks the user to enter a filename and select a location on the local computer to save the file. once Selected and confirms the save operation (for example, by clicking Save), the save process begins. Listeners receive events to indicate the progress, success, or failure of the save operation.

FileReference save method has only to be invoked upon user interaction, for example by a mouse click or button press.


<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” >
<mx:Script>
<![CDATA[
private var file:FileReference = new FileReference();
private function onCreate(e:Event):void
{
var Txt:String=”Hello World”
file.save(Txt,”Sample.txt”); // Initiates the save event and opens up a dialog box for user to enter filename and select a location. Once confirmed, it get saved
} ]]>
</mx:Script>
<mx:Button x=”133″ y=”209″ label=”Create File” click=”onCreate(event)”/>
</mx:Application>

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;
}