2013年5月23日 星期四

Action Script Basic

is: check if an object is subclass of a class
as: convert subclass instance to super class

package: group of classes (as path)
e.g. : package aaa.bbb{
public class ClassName{
}
}

scope: private, protected, public, internal(classes within the same package can access)

override a function:
e.g. override public function funtionName:returnType{
}

scale movie: action script 3.0 3.6
change alignment 3.7
hide flash player menu items 3.8

allow .swf from other server: 3.12
use .xml to be a policy file:
e.g.
<?xml version="1.0"?>
<!-- http://www.mydomain.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="www.otherdomain.com" />
<allow-access-from domain="*.adobe.com" />
<allow-access-from domain="123.45.67.89" />
</cross-domain-policy>
call the policy file:flash.system.Security.loadPolicyFile( )

trace parseInt("103",2); use binary to show 103

String compare
var str:String="1";
var num:uint=1;
trace(str==num); //compile error (if strict mode)
trace(this["str"]==this["num"]);//true
trace(this["str"]===this["num"]);//false

use math function :Math.XX e.g. Math.PI, Math.round();
also check NumberUtilities

Number Format: 4.4,4.5,4.6

addChild(instance name) to show class instance
be sure to release resource of objects (aaa.destroy(); aaa=null;)

Array:
Integer-indexed
Associative array : use keys
var array:Array=new Array();
var array:Array=["a", 2, true, new Object()];
items[4] = "apples";
trace(items[4]); // Displays: apples


var array:Array = new Array();
array.push("val 1", "val 2"); //add at the end
can use Array.pop();


var letters:Array = ["a", "b", "c"];
letters[5] = "f"; =>["a", "b", "c", undefined, undefined, "f"]//(==undefined) is ok

letters.unshift("z"); //add at the begining

length is supported


find elements in array

var letters:Array = ["a", "b", "c", "d", "a", "b", "c", "d"];
trace(ArrayUtilities.findMatchIndices(letters, "b"));
// Displays: 1,5



var words:Array = ["bicycle", "baseball", "mat", "board"];
trace(ArrayUtilities.findMatchIndices(words, "b", true));
// Displays: 0,1,3

findLastMatchIndex( )

Remove elements:
splice()//remove element in middle splice(start pos, num elements to delete)
pop()//remove last
shift()//remove first

var deleted:Array = letters.splice(0, 2);

insert element:

var letters:Array = ["a", "b", "c", "d"];
// Insert three string values ("one", "two", and "three")
// starting at index 1.
letters.splice(1, 0, "r", "s", "t");
// letters now contains seven elements:
// "a", "r", "s", "t", "b", "c", and "d".

//delete and insert together

var letters:Array = ["a", "b", "c", "d"];
// Remove two elements and insert three more
// into letters starting at index 1.
letters.splice(1, 2, "r", "s", "t");
// myArray now contains five elements:
// "a", "r", "s", "t", and "d".

String to Array

var list:String = "Peter Piper picked a peck of pickled peppers";
// Split the string using the space as the delimiter. This puts
// each word into an element of the new array, words.
var words:Array = list.split(" ");

Array to String:

var letters:Array = ["a", "b", "c"];
trace(letters.join("|")); // Displays: a|b|c

copy array:

var letters:Array = ["a", "b", "c"];
// Create an independent copy of letters using concat( ),
// which returns a new array.
var newLetters:Array = letters.concat( );
// Both arrays contain the same values, as expected.
trace(letters); // Displays: "a,b,c"
trace(newLetters); // Displays: "a,b,c"

letters = ["d", "e", "f"];
// Unlike preceding examples, the arrays are independent.
trace(letters); // Displays: "d,e,f"
trace(newLetters); // Displays: "a,b,c"
//NOT REFERENCE

if multi dimention array: concat is REFERENCED, use duplicated:

var coordinates:Array = new Array( );
for(var i:int = 0; i < 4; i++) {
coordinates[i] = new Array( );
for(var j:int = 0; j < 4; j++) {
coordinates[i].push(String(i) + "," + String(j));
}
}
// Duplicate coordinates. Cast the result as an array.
var newCoordinates:Array = ArrayUtilities.duplicate(coordinates, true) as Array;

sort()/reverse() is supported
ArrayUtilities.min( ) and ArrayUtilities.max( )
ArrayUtilities.equals(letters1, letters2)

Associate Array (with Keys)

var memebers:Object = {scribe: "Franklin",
chairperson: "Gina",
treasurer: "Sindhu"};


var members:Object = new Object( );
members.scribe = "Franklin";
members.chairperson = "Gina";
members.treasurer = "Sindhu";

trace(members.scribe); // Displays: Franklin
trace(members["scribe"]); // Displays: Franklin


var members:Object = new Object();
members.councilperson1 = "Beatrice";
members.councilperson2 = "Danny";
members.councilperson3 = "Vladamir";
for (var i:int = 1; i <= 3; i++) {
trace(members["councilperson" + i];
}


for (var sRole:String in members) {
// Displays:
// treasurer: Sindhu
// chairperson: Gina
// scribe: Franklin
trace(sRole + ": " + members[sRole]);
}












沒有留言:

張貼留言