轉(zhuǎn) http://www.nshen.net/blog/article.asp?id=495 flex tip
原文地址:http://www.kirupa.com/forum/showthread.php?t=223798
原文作者:senocular
原文目錄:
1. Change the frame rate of your movie
2. Abstract Classes
3. Deep Object Copies with ByteArray
4. Determine Instance Class or Superclass
5. Multiple Arguments in trace()
6. Loading Text and XML with URLLoader
7. Get Sound Spectrum Information
8. Garbage Collection: Reference Counting & Mark and Sweep
9. Weak References
10. MXMLC: SWF Metadata Tag
11. TextField.appendText()
12. Access to stage and root
13. No More Color Class; Use ColorTransform
14. Array.indexOf (Array.lastIndexOf())
15. System.totalMemory
16. Closing Net Connections
17. AVM2 (AS3) to AVM1 (AS2/AS1) Communication via LocalConnection
18. Class member enumeration
19. Key.isDown in AS3
20. Scale and Alpha Ranges
21. Available ActionScript packages
22. Numeric Calculations: Number vs. int Speed
23. Getting Around globally accessible _root and _global
24. ActionScript 2 to ActionScript 3 Converter
25. Accessing FlashVars and HTML Parameters
26. ActionScript Speed Through Typing
27. LivePreview and the LivePreviewParent class
28. Flex Component Kit for Flash CS3
29. Loading URL encoded Variables
30. Interactive Masks
31. No _lockroot
General:
1. 動(dòng)態(tài)改變幀頻:
// change frame rate to 12 fps:
stage.frameRate = 12;
stage.frameRate = 12;
2. Abstract Classes
as3不支持抽象類,但有幾個(gè)內(nèi)置的抽象類
DisplayObject
InteractiveObject
DisplayObjectContainer
Graphics
as3中這意味著不能實(shí)例化這些類,也不能繼承他們 ,以下為錯(cuò)誤操作:
// 錯(cuò)誤1
var myObj:InteractiveObject = new InteractiveObject(); // ERROR
var myObj:InteractiveObject = new InteractiveObject(); // ERROR
// 錯(cuò)誤2
package {
import flash.display.DisplayObject;
public class MyDisplay extends DisplayObject{
public function MyDisplay (){
// ERROR
}
}
}
package {
import flash.display.DisplayObject;
public class MyDisplay extends DisplayObject{
public function MyDisplay (){
// ERROR
}
}
}
3. Deep Object Copies with ByteArray
as3中很容易使用(flash.utils.ByteArray)類來(lái)進(jìn)行對(duì)象的深拷貝,深拷貝不只拷貝引用,而是拷貝整個(gè)相關(guān)對(duì)象,
例如一個(gè)array里包含一個(gè)object引用,這個(gè)object也將被拷貝
注意:這個(gè)方法通常用來(lái)拷貝一般的object
拷貝函數(shù):
function clone(source:Object):* {
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
var copier:ByteArray = new ByteArray();
copier.writeObject(source);
copier.position = 0;
return(copier.readObject());
}
使用方法:
newObjectCopy = clone(originalObject);
4. Determine Instance Class or Superclass
as3中可以很容易的獲取某個(gè)實(shí)例所屬的類和父類的類名,使用這兩個(gè)方法
getQualifiedClassName (flash.utils.getQualifiedClassName).
getQualifiedSuperclassName (flash.utils.getQualifiedSuperclassName)
var sprite:Sprite = new Sprite();
trace(getQualifiedClassName(sprite)); // "flash.display::Sprite
trace(getQualifiedSuperclassName(sprite)); // "flash.display::DisplayObjectContainer"
你甚至可以由字符串獲得類的引用,使用這個(gè)方法
getDefinitionByName (flash.utils.getDefinitionByName).
getDefinitionByName("flash.display::Sprite")); // [class Sprite]
5. Multiple Arguments in trace()
as3
trace(value1, value2, value3);
as1,as2
trace([value1, value2, value3]);
6. Loading Text and XML with URLLoader
var loader:URLLoader;
// ...
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
var request:URLRequest = new URLRequest("file.xml");
loader.load(request);
//...
function xmlLoaded(event:Event):void {
var myXML:XML = new XML(loader.data);
//...
}
// ...
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, xmlLoaded);
var request:URLRequest = new URLRequest("file.xml");
loader.load(request);
//...
function xmlLoaded(event:Event):void {
var myXML:XML = new XML(loader.data);
//...
}
7.Get Sound Spectrum Information
(flash.media.SoundMixer)類的computeSpectrum靜態(tài)方法可以把正在播放的聲譜信息轉(zhuǎn)成ByteArray,由此你可以想到辦法把聲譜可視化顯示出來(lái)
// play sound...
var spectrumInfo:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(spectrumInfo);
// spectrumInfo is now a byte array with sound spectrum info
var spectrumInfo:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(spectrumInfo);
// spectrumInfo is now a byte array with sound spectrum info
具體可視化的方法可以看swfdong那里的教程 :)
8. Garbage Collection: Reference Counting & Mark and Sweep
垃圾回收機(jī)制包含兩個(gè)關(guān)鍵:
(1)Reference Counting (引用數(shù)) :
引用數(shù)就是在內(nèi)存中引用同一個(gè)object的變量多少,每增加一個(gè)變量引用該object,引用數(shù)就+1
var a:Object = new Object(); // new Object in memory given reference count of 1
var b:Object = a; // Object now has reference count of 2
無(wú)論什么時(shí)候,當(dāng)沒(méi)有變量引用該object的時(shí)候,垃圾回收就會(huì)把他們回收了
delete a; // Object has reference count of 1
delete b; // Object has reference count of 0, removed from memory
注意delete只能刪除非成員變量,而且刪除的是引用該object的變量而不是將object從內(nèi)存中刪除,那是垃圾回收器要做的事
看下邊這種情況:
var a:Object = new Object(); // reference(a) count 1
var b:Object = new Object(); // reference(b) count 1
a.b = b; // reference(b) count 2
b.a = a; // reference(a) count 2
delete a; // reference(a) count 1
delete b; // reference(b) count 1
雖然a和b變量被刪除了,而且我們?cè)僖膊荒苡贸绦蛟L問(wèn)到他們了,但他們?nèi)匀淮媪粼趦?nèi)存中~這種情況下垃圾回首器就沒(méi)有辦法了嗎?錯(cuò)!這是記號(hào)清理要做的!
(2)Mark and Sweep (記號(hào)清理):
Mark and Sweep簡(jiǎn)單來(lái)說(shuō)就是一種掃描機(jī)制,如下圖,從root開始,掃到對(duì)象就mark一下,并掃描這個(gè)對(duì)象,等待全掃描完了,沒(méi)有mark的自然是沒(méi)有用的了,就會(huì)被從內(nèi)存中刪掉了.
[root] <- scan...
[objectRef (marked)] <- scan...
[objectRef (marked)] <- scan...
[objectRef (marked)] <- scan...
[objectRef (marked)] <- scan...
[objectRef (marked)] <- scan...
...
[delete all objects not marked]
當(dāng)然這種掃描是昂貴的,需要很長(zhǎng)時(shí)間才能發(fā)生一次,所以不要指望他了,編程的時(shí)候多注意引用數(shù)就可以了
9. Weak References
使用弱引用來(lái)引用對(duì)象,不會(huì)被垃圾回收器視為有效的引用數(shù)。這樣可以幫助垃圾回收器的工作
所以使用弱引用這是一種比較好的編程習(xí)慣,使你不會(huì)在不經(jīng)意間錯(cuò)誤的引用到了某個(gè)object導(dǎo)致這個(gè)object不能被從內(nèi)存中卸載~~~
但不是哪里都可以使用弱引用的,as3中有兩處可以使用:
(1)在Directionary類的構(gòu)造函數(shù)中傳入true
var dict:Dictionary = new Dictionary(true); // use weak references as keys
var obj:Object = new Object();
dict[obj] = true;
delete obj; // 雖然dict中有引用到obj,但并不是一個(gè)有效引用,所以obj仍然會(huì)被垃圾回收
(2)在EventDispatcher的addEventListener 的第5個(gè)參數(shù)指定true
// addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
addEventListener(MouseEvent.CLICK, clickHandler, false, 0 true); // use weak references
10. MXMLC: SWF Metadata Tag
如果你用mxmlc編譯swf的話,可以使用swf metadata tag來(lái)指定swf的一些屬性
支持以下屬性:
width
height
frameRate
backgroundColor
例子:
package {
[SWF(width="500", height="450", frameRate="24", backgroundColor="#FFFFFF")]
public class MyApp extends Sprite {
}
}
[SWF(width="500", height="450", frameRate="24", backgroundColor="#FFFFFF")]
public class MyApp extends Sprite {
}
}
11.TextField.appendText()
(flash.text.TextField)類多了一個(gè)appendText()方法
var my_tf = new TextField();
my_tf.text = "Hello";
my_tf.appendText(" world!"); // my_tf.text == "Hello world!"
相當(dāng)于以前的my_tf.text+=" world!" ,只是效率提高了
12. Access to stage and root
* 在as3中,當(dāng)swf被loaded到player中時(shí) stage object 是最頂端的容器,所有其他東西都在它里邊,包括root在內(nèi)
* 一個(gè)application中只有一個(gè)stage,卻可以有若干個(gè) root ,比如有外部?jī)?nèi)容被Loader類加載近來(lái)的時(shí)候
* 所有的DisplayObject (flash.display.DisplayObject)都有 stage 和 root 屬性,但只有當(dāng)他們被直接或間接的加入到display list 中的時(shí)候才有值,否則他們都為 null 。
* stage 屬性 如果有值,那么他一直指向 stage 對(duì)象 , root 卻不同 ,具體哪不同,下邊懶得翻引用原話 - -b
引用
For the stage, root always references the stage
For the main timeline of the SWF and all display objects within it, root references the main timeline
If an object is added directly to the stage from any timeline, the root property for it and its children references the stage
For display objects in loaded SWF files, root references the main timeline of that SWF file
For loaded bitmap images, root references the Bitmap instance of the image loaded
Loader objects used to load external SWFs and images follow the rules of all other display objects within the SWF it is being used
For the main timeline of the SWF and all display objects within it, root references the main timeline
If an object is added directly to the stage from any timeline, the root property for it and its children references the stage
For display objects in loaded SWF files, root references the main timeline of that SWF file
For loaded bitmap images, root references the Bitmap instance of the image loaded
Loader objects used to load external SWFs and images follow the rules of all other display objects within the SWF it is being used
技巧1: 寫一個(gè)TopLevel類,讓Document類繼承它,通過(guò)TopLevel類訪問(wèn)stage
package {
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
public class TopLevel extends MovieClip {
public static var stage:Stage;
public static var root:DisplayObject;
public function TopLevel() {
TopLevel.stage = this.stage;
TopLevel.root = this;
}
}
}
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Stage;
public class TopLevel extends MovieClip {
public static var stage:Stage;
public static var root:DisplayObject;
public function TopLevel() {
TopLevel.stage = this.stage;
TopLevel.root = this;
}
}
}
package {
public class MyDocumentClass extends TopLevel {
public function MyDocumentClass() {
// code
}
}
}
public class MyDocumentClass extends TopLevel {
public function MyDocumentClass() {
// code
}
}
}
package {
public class RandomClass {
public function RandomClass() {
trace(TopLevel.stage); // [object Stage]
}
}
}
public class RandomClass {
public function RandomClass() {
trace(TopLevel.stage); // [object Stage]
}
}
}
當(dāng)然這很不oo也很不雅觀~~~如果別人的Document類沒(méi)繼承TopLevel類就會(huì)有問(wèn)題出現(xiàn)
技巧2 : 傳遞引用
類似這樣通過(guò)構(gòu)造函數(shù)將stage傳進(jìn)去
package {
import flash.display.Stage;
public class CustomObject {
private var stage:Stage;
public function CustomObject(stageRef:Stage) {
// stage access through
// constructor argument
stage = stageRef;
}
}
}
import flash.display.Stage;
public class CustomObject {
private var stage:Stage;
public function CustomObject(stageRef:Stage) {
// stage access through
// constructor argument
stage = stageRef;
}
}
}
作者給了一個(gè) StageDetection 類
地址:http://www.senocular.com/flash/actionscript.php?file=ActionScript_3.0/com/senocular/events/StageDetection.as
好象是用于檢測(cè)某個(gè)display object 的 stage 或 root 屬性是否存在,然后才能把stage的引用傳給非display object
![[exclaim]](http://www.nshen.net/blog/styles/default/images/smilies/icon_exclaim.gif)
13. No More Color Class; Use ColorTransform
as3 中徹底沒(méi)了flash8就不推薦使用了的 Color類 改用 ColorTransform類
注意ColorTransform 的rgb屬性已經(jīng)改名為color
// creates a red square
var square:Shape = new Shape();
square.graphics.beginFill(0x000000);
square.graphics.drawRect(0, 0, 100, 100);
var colorTransform:ColorTransform = square.transform.colorTransform;
colorTransform.color = 0xFF0000;
square.transform.colorTransform = colorTransform;
addChild(square);
var square:Shape = new Shape();
square.graphics.beginFill(0x000000);
square.graphics.drawRect(0, 0, 100, 100);
var colorTransform:ColorTransform = square.transform.colorTransform;
colorTransform.color = 0xFF0000;
square.transform.colorTransform = colorTransform;
addChild(square);
14. Array.indexOf (Array.lastIndexOf())
Array類多了兩個(gè)方法 indexOf 和 lastIndexOf
AS3 function indexOf(searchElement:*, fromIndex:int = 0):int
AS3 function lastIndexOf(searchElement:*, fromIndex:int = 0x7fffffff):int
AS3 function lastIndexOf(searchElement:*, fromIndex:int = 0x7fffffff):int
跟String類的同名方法差不多 ,返回指定元素位置,如果沒(méi)有返回 -1
var sprite:Sprite = new Sprite();
var object:Object = new Object();
var boolean:Boolean = true;
var number:Number = 10;
var array:Array = new Array(sprite, object, number);
trace(array.indexOf(sprite)); // 0
trace(array.indexOf(number)); // 2
trace(array.indexOf(boolean)); // -1
var object:Object = new Object();
var boolean:Boolean = true;
var number:Number = 10;
var array:Array = new Array(sprite, object, number);
trace(array.indexOf(sprite)); // 0
trace(array.indexOf(number)); // 2
trace(array.indexOf(boolean)); // -1
15. System.totalMemory
System 類 (flash.system.System) 有一個(gè)新屬性叫做 totalMemory,返回當(dāng)前flash player占用的內(nèi)存
var o:Object = new Object();
trace(System.totalMemory); // 4960256
trace(System.totalMemory); // 4960256
var o:MovieClip = new MovieClip();
trace(System.totalMemory); // 4964352
trace(System.totalMemory); // 4964352
16. Closing Net Connections
as1,2時(shí)候,當(dāng)loading開始后,你就不能關(guān)閉這個(gè)連接了,在as3世界里,即使loading開始了,你也可以停止它
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("image.jpg");
loader.load(request);
addChild(loader);
// 如果3秒鐘還沒(méi)loading完,就關(guān)閉連接
var abortID:uint = setTimeout(abortLoader, 3000);
// abort the abort when loaded
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, abortAbort);
function abortLoader(){
try {
loader.close();
}catch(error:Error) {} //注意這里有可能拋出IOError
}
function abortAbort(event:Event){
clearTimeout(abortID);
}
var request:URLRequest = new URLRequest("image.jpg");
loader.load(request);
addChild(loader);
// 如果3秒鐘還沒(méi)loading完,就關(guān)閉連接
var abortID:uint = setTimeout(abortLoader, 3000);
// abort the abort when loaded
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, abortAbort);
function abortLoader(){
try {
loader.close();
}catch(error:Error) {} //注意這里有可能拋出IOError
}
function abortAbort(event:Event){
clearTimeout(abortID);
}
17. AVM2 (AS3) to AVM1 (AS2/AS1) Communication via LocalConnection
由于as3與as1,2虛擬機(jī)的不同導(dǎo)致as1,2 與as3不能直接溝通,如果需要的話必須要間接的使用
LocalConnection AS2
http://livedocs.macromedia.com/flash/8/main/00002338.html
LocalConnection AS3 (flash.net.LocalConnection)
http://livedocs.macromedia.com/flex/2/langref/flash/net/LocalConnection.html
在as2中
// ActionScript 2 file, AS2animation.fla
// one movie clip animation named animation_mc on the timeline
// local connection instance to receive events
var AVM_lc:LocalConnection = new LocalConnection();
// stopAnimation event handler
AVM_lc.stopAnimation = function(){
animation_mc.stop();
}
// listen for events for "AVM2toAVM1"
AVM_lc.connect("AVM2toAVM1");
// one movie clip animation named animation_mc on the timeline
// local connection instance to receive events
var AVM_lc:LocalConnection = new LocalConnection();
// stopAnimation event handler
AVM_lc.stopAnimation = function(){
animation_mc.stop();
}
// listen for events for "AVM2toAVM1"
AVM_lc.connect("AVM2toAVM1");
as3中
// ActionScript 3 file, AS3Loader.fla
// local connection instance to communicate to AVM1 movie
var AVM_lc:LocalConnection = new LocalConnection();
// loader loads AVM1 movie
var loader:Loader = new Loader();
loader.load(new URLRequest("AS2animation.swf"));
addChild(loader);
// when AVM1 movie is clicked, call stopPlayback
loader.addEventListener(MouseEvent.CLICK, stopPlayback);
function stopPlayback(event:MouseEvent):void {
// send stopAnimation event to "AVM2toAVM1" connection
AVM_lc.send("AVM2toAVM1", "stopAnimation");
}
// local connection instance to communicate to AVM1 movie
var AVM_lc:LocalConnection = new LocalConnection();
// loader loads AVM1 movie
var loader:Loader = new Loader();
loader.load(new URLRequest("AS2animation.swf"));
addChild(loader);
// when AVM1 movie is clicked, call stopPlayback
loader.addEventListener(MouseEvent.CLICK, stopPlayback);
function stopPlayback(event:MouseEvent):void {
// send stopAnimation event to "AVM2toAVM1" connection
AVM_lc.send("AVM2toAVM1", "stopAnimation");
}
as3 movie把a(bǔ)s2的movie加載進(jìn)來(lái)然后調(diào)用as2的stopAnimation
18. Class member enumeration
as3中只有dynamic class 中的dynamic definitions才可以被枚舉(就是for in ),例如下邊的class就沒(méi)有可枚舉的成員
package {
public class EnumerateClass {
public var variable:String = "value";
public function method():void {}
}
}
public class EnumerateClass {
public var variable:String = "value";
public function method():void {}
}
}
var example:EnumerateClass = new EnumerateClass();
for (var key:String in example) {
trace(key + ": " + example[key]); // 沒(méi)有輸出
}
for (var key:String in example) {
trace(key + ": " + example[key]); // 沒(méi)有輸出
}
即使上邊的class改成dynamic的,也是沒(méi)有輸出,為什么呢?
因?yàn)槔镞叺淖兞亢头椒ㄊ撬麄冏约旱模⒉皇莿?dòng)態(tài)(dynamic)填加的,只有動(dòng)態(tài)填加的屬性可以被for in
Object類有個(gè) setPropertyIsEnumerable方法,可以控制某個(gè)屬性是否可枚舉,但它只對(duì)dynamic屬性有效
package {
public dynamic class EnumerateClass {
public var variable:String = "value";
public function method():void {}
public function EnumerateClass(){
this.dynamicVar = 1;
this.dynamicVar2 = 2;
this.setPropertyIsEnumerable("dynamicVar2", false);
}
}
}
public dynamic class EnumerateClass {
public var variable:String = "value";
public function method():void {}
public function EnumerateClass(){
this.dynamicVar = 1;
this.dynamicVar2 = 2;
this.setPropertyIsEnumerable("dynamicVar2", false);
}
}
}
上邊的dynamic類有兩個(gè)dynamic屬性, dynamicVar 和 dynamicVar2 ,滿足條件,都應(yīng)該是可枚舉的
但調(diào)用 setPropertyIsEnumerable("dynamicVar2", false);阻止了枚舉 dynamicVar2
var example:EnumerateClass = new EnumerateClass();
for (var key:String in example) {
trace(key + ": " + example[key]); // dynamicVar: 1
}
for (var key:String in example) {
trace(key + ": " + example[key]); // dynamicVar: 1
}
propertyIsEnumerable方法可以測(cè)試某一屬性是否可枚舉
trace(example.propertyIsEnumerable("variable")); // false
trace(example.propertyIsEnumerable("dynamicVar")); // true
trace(example.propertyIsEnumerable("dynamicVar2")); // false
trace(example.propertyIsEnumerable("dynamicVar")); // true
trace(example.propertyIsEnumerable("dynamicVar2")); // false
19. Key.isDown in AS3
在as1,2時(shí)代,尤其是游戲中經(jīng)常會(huì)用到 Key.isDown ,但到了as3,Key類已經(jīng)不存在了,但我們可以通過(guò)些技巧實(shí)現(xiàn)這個(gè)類
package {
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
* The Key class recreates functionality of
* Key.isDown of ActionScript 1 and 2. Before using
* Key.isDown, you first need to initialize the
* Key class with a reference to the stage using
* its Key.initialize() method. For key
* codes use the flash.ui.Keyboard class.
*
* Usage:
* Key.initialize(stage);
* if (Key.isDown(Keyboard.LEFT)) {
* // Left key is being pressed
* }
*/
public class Key {
private static var initialized:Boolean = false; // marks whether or not the class has been initialized
private static var keysDown:Object = new Object(); // stores key codes of all keys pressed
/**
* Initializes the key class creating assigning event
* handlers to capture necessary key events from the stage
*/
public static function initialize(stage:Stage) {
if (!initialized) {
// assign listeners for key presses and deactivation of the player
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
// mark initialization as true so redundant
// calls do not reassign the event handlers
initialized = true;
}
}
/**
* Returns true or false if the key represented by the
* keyCode passed is being pressed
*/
public static function isDown(keyCode:uint):Boolean {
if (!initialized) {
// throw an error if isDown is used
// prior to Key class initialization
throw new Error("Key class has yet been initialized.");
}
return Boolean(keyCode in keysDown);
}
/**
* Event handler for capturing keys being pressed
*/
private static function keyPressed(event:KeyboardEvent):void {
// create a property in keysDown with the name of the keyCode
keysDown[event.keyCode] = true;
}
/**
* Event handler for capturing keys being released
*/
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
// delete the property in keysDown if it exists
delete keysDown[event.keyCode];
}
}
/**
* Event handler for Flash Player deactivation
*/
private static function clearKeys(event:Event):void {
// clear all keys in keysDown since the player cannot
// detect keys being pressed or released when not focused
keysDown = new Object();
}
}
}
20.Scale and Alpha Ranges
值得注意一下的是,as3中好多屬性值的范圍變化了,有些值原來(lái)范圍是0 到 100 ,現(xiàn)在變成了0 到 1 ,例如下邊幾個(gè),要留心一下
ActionScript 2.0 | ActionScript 3.0
_xscale: 0 - 100 | scaleX: 0 - 1
_yscale: 0 - 100 | scaleY: 0 - 1
_alpha: 0 - 100 | alpha: 0 - 1
_xscale: 0 - 100 | scaleX: 0 - 1
_yscale: 0 - 100 | scaleY: 0 - 1
_alpha: 0 - 100 | alpha: 0 - 1
21. Available ActionScript packages
AS3 package 概要
* flash 包 (player的核心類包,永遠(yuǎn)可用)
* mx 包 (flex 專用類包,flex 的組件們需要)
* fl 包 (flash 專用類包,flash組件等)
* adobe 包 (flash 專用類包,繪圖工具擴(kuò)展等)
更多詳細(xì)包內(nèi)容,看文檔
Flex: http://livedocs.adobe.com/flex/201/langref/
Flash: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/
Apollo: http://livedocs.adobe.com/apollo/1.0/aslr/
注意: Apollo相關(guān)的類在 flash和mx包里
22. Numeric Calculations: Number vs. int Speed
比較Number類型和新出現(xiàn)的int類型速度
*只有在用到 bitwise operations (<<, >>, &, ^, and |) 的時(shí)候 int 類型比較快,例如
var valueN:Number = 10;
result = valueN << 2; // not so fast
var valueI:int = 10;
reesult = valueI << 2; // Fast!
result = valueN << 2; // not so fast
var valueI:int = 10;
reesult = valueI << 2; // Fast!
*其他情況下聲明為Number比較快
23. Getting Around globally accessible _root and _global
as1 ,2中任何地方都能訪問(wèn)到_root ,_global ,這樣可以很簡(jiǎn)單的保存變量或函數(shù)到這里,但在as3中已經(jīng)沒(méi)有_global了,root 也只有Disobject在顯示列表里時(shí)才會(huì)得到,如果你非要這么做的話,應(yīng)該自己寫一個(gè)glo類,里邊用一個(gè)靜態(tài)變量bal指向一個(gè)Object,下邊是代碼
package {
public class glo {
public static var bal:Object = new Object();
}
}
public class glo {
public static var bal:Object = new Object();
}
}
現(xiàn)在可以把 gol.bal 當(dāng)成以前的_root或_global用了
trace(glo.bal.foo); // undefined
glo.bal.foo = "bar";
trace(glo.bal.foo); // bar
glo.bal.foo = "bar";
trace(glo.bal.foo); // bar
24. ActionScript 2 to ActionScript 3 Converter
Patrick Mineault(http://www.5etdemi.com/blog/) 用php寫了一個(gè)as2 -->as3 轉(zhuǎn)換器,不是百分之百準(zhǔn)確但也不錯(cuò)
online 版本 :http://www.5etdemi.com/convert
下載 :http://www.5etdemi.com/convert/convert.zip
25. Accessing FlashVars and HTML Parameters
你可以用HTML中的 object/embed 代碼向鑲?cè)氲腟WF傳遞變量。
有兩種方法:
1。在swf的url后邊加變量(query string)
<!-- URL Variables -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="640" height="500" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashMovie.swf?myVar=1" />
<param name="quality" value="high" />
<param name="bgcolor" value="#EFF7F6" />
<embed src="flashMovie.swf?myVar=1" quality="high" bgcolor="#EFF7F6" width="640" height="500" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="640" height="500" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashMovie.swf?myVar=1" />
<param name="quality" value="high" />
<param name="bgcolor" value="#EFF7F6" />
<embed src="flashMovie.swf?myVar=1" quality="high" bgcolor="#EFF7F6" width="640" height="500" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
2.使用FlashVars
<!-- FlashVars -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashMovie.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#FFFFFF" />
<param name="FlashVars" value="myVar=1" />
<embed src="flashMovie.swf" FlashVars="myVar=1" quality="high" bgcolor="#FFFFFF" width="550" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashMovie.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#FFFFFF" />
<param name="FlashVars" value="myVar=1" />
<embed src="flashMovie.swf" FlashVars="myVar=1" quality="high" bgcolor="#FFFFFF" width="550" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
在as2中這些變量會(huì)被簡(jiǎn)單的聲明到_root上,但在as3中有所改變。現(xiàn)在這些變量被放到了root下的parameters對(duì)象下,上邊HTML傳的值可以這樣訪問(wèn):
root.loaderInfo.parameters.myVar;
26. ActionScript Speed Through Typing
考慮到效率,盡量把你的變量聲明類型,如果可能的話避免使用dynamic class
27. LivePreview and the LivePreviewParent class
as2 的組件使用_global下的isLivePreview屬性來(lái)判斷是否是在Flash IDE中預(yù)覽,as3中這個(gè)屬性不見(jiàn)了,取而代之的是你可以檢查這個(gè)組件的parent是否為 LivePreviewParent 類的實(shí)例
var isLivePreview:Boolean = (parent != null && getQualifiedClassName(parent) == "fl.livepreview::LivePreviewParent");
(
![[exclaim]](http://www.nshen.net/blog/styles/default/images/smilies/icon_exclaim.gif)
28. Flex Component Kit for Flash CS3
Flex Component Kit for Flash CS3(在Adobe Labs里) 允許把flash中做的交互內(nèi)容,在Flex中當(dāng)成Flex comonnet來(lái)用
現(xiàn)在可以很容易的使Flash symbol繼承我們的新 UIMovieClip 類
從而使
* flash 組件放在一個(gè)Flex container里,所有l(wèi)ayout會(huì)很好的工作
* flash 的幀標(biāo)簽被完全轉(zhuǎn)換為 Flex 的state和 transition
* 只要簡(jiǎn)單的填加個(gè) SWC 到 library path里Flex Builder就可以有flash組件的語(yǔ)法提示了
* Simple JSFL commands are available to set up new documents as well as publish the Flex-enabled SWC (這句沒(méi)看明白- -b)
29. Loading URL encoded Variables
URLLoader類(flash.net.URLLoader) 在as3中用來(lái)讀外部文本文件.當(dāng)一個(gè)外部文件被load后,它的內(nèi)容會(huì)被存儲(chǔ)在URLLoader實(shí)例的data屬性里,一般情況下無(wú)論外部文件是什么格式,都會(huì)被以raw text形似讀取,如果讀變量可能需要改一下dataFormat屬性
URLLoader類的dataFormat屬性決定被加載數(shù)據(jù)的格式。 dataFormat屬性的可選值在 URLLoaderDataFormat 類中(flash.net.URLLoaderDataFormat)
可選值為:
BINARY - 指定按2禁止數(shù)據(jù)接受
TEXT - 指定當(dāng)成文本接受
VARIABLES - 指定當(dāng)作 URL-encoded變量接受
指定URLLoaderDataFormat.VARIABLES 作為 URLLoader的 dataFormat屬性, 被加載的數(shù)據(jù)將被當(dāng)作一組變量,而不是字符串
data屬性將為包含被加載變量的一個(gè) URLVariables 類 (flash.net.URLVariables)的實(shí)例
ActionScript 代碼:
var loader:URLLoader = new URLLoader();
// specify format as being variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, varsLoaded);
// vars.txt contents: foo=bar&foo2=bar2
loader.load(new URLRequest("vars.txt"));
function varsLoaded (event:Event):void {
trace(loader.data is URLVariables); // true
trace(loader.data.foo); // bar
trace(loader.data.foo2); // bar2
}
// specify format as being variables
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, varsLoaded);
// vars.txt contents: foo=bar&foo2=bar2
loader.load(new URLRequest("vars.txt"));
function varsLoaded (event:Event):void {
trace(loader.data is URLVariables); // true
trace(loader.data.foo); // bar
trace(loader.data.foo2); // bar2
}
30. Interactive Masks
默認(rèn)情況下,當(dāng)一個(gè)對(duì)象 mask 另一個(gè)對(duì)象的時(shí)候,前者(這個(gè)遮照)將失去它的交互性,例如,一個(gè)sprite有一個(gè) click事件,但當(dāng)這個(gè)sprite成為一個(gè)遮照的時(shí)候,這個(gè)click事件就不起作用了
如果你想讓一個(gè)對(duì)象作為遮照的時(shí)候也擁有交互性,就指定它的buttonMode屬性為true
ActionScript Code:
maskInstance.buttonMode = true;
31. No _lockroot
ActionScript 3 沒(méi)有 _lockroot屬性了.
_lockroot 屬性是在Flash Player 7 引入近來(lái)的,_lockroot屬性原本用來(lái)確保被load進(jìn)來(lái)的movie的_root屬性始終引用它自己而不是主swf在 ActionScript 3 中, 所有的root引用永遠(yuǎn)指向文件本身的root 就像_lockroot已經(jīng)在那了一樣。(
![[exclaim]](http://www.nshen.net/blog/styles/default/images/smilies/icon_exclaim.gif)
[最后編輯于 N神, at 2007-05-13 13:09:08]
posted on 2007-05-15 11:22 leoli 閱讀(1561) 評(píng)論(0) 編輯 收藏 所屬分類: Flex