Binary Types in ActionScript
A read-only stream, like a URLStream, would only implement IDataInput. A read-write stream, like Socket, implements both.
Read or write an ActionScript object using
AMF encoding. Specify AMF0 or AMF3 using the objectEncoding parameter.
In Flash Player 10 and later, you can clear out a ByteArray with the clear() method.
position determines where objects will be read starting from, and read operations will automatically advance the position to the end of the last read object.
length reports, in bytes, the total size of the ByteArray.
have an exact duplicate of the original object, not merely another reference
import flash.utils.ByteArray;
var arrayA:Array = new Array("a", "b", "c", "d";);
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(arrayA);
byteArray.position = 0;
var arrayB:Array = byteArray.readObject();
arrayB.push("e", "", "g", "h");
trace(arrayA.join(" ")); //a b c d
trace(arrayB.join(" ")); //a b c d e f g h
}There is one important consideration when you arecopying objects of custom types. Byte arrays write data using AMF, which natively supports standard ActionScript data types,
butnot custom classes. (AMF is the Action Message Format, a binary protocol) That means that, by default, if you write an object of a custom type, it is written to the byte array correctly, but it isstored
as a generic object. Therefore,when you retrieve it from a byte array you cannot cast it back to the correct type. You can fix this by using the registerClassAlias() function.
Note
The AIR runtime can have multiple stages because every top-level window has its own display list.
交换两个显示对象的深度
stage.setChildIndex(apple, 0);
stage.setChildIndex(carrot, 1);
//now carrot is definitively on top of apple
stage.swapChildren(apple, carrot); //now apple is on top
stage.swapChildren(apple, carrot); //now carrot is back on top
//note that swapping again puts things the way they were
stage.swapChildrenAt(0, 1); You can investigate the contents and organization of a display list. You can test whether one display object contains another:
carrot.addChild(sprouts);
trace(carrot.contains(sprouts)); //true wmodeGPU— In Flash Player 10.0.32 and later, this read-only Boolean flag indicates whether the GPU is performing compositing. This property will be true only if GPU compositing is actively in use: if you’ve indicated that the GPU wmode should be used in your embed parameters or at SWF publish time, and if it is supported by the hardware Flash Player is running on. When this is true, you may be able to crank up the graphics without dropping frame rate. If you have this graphics-intensive of an application, you would do well to keep track of other performance metrics while you adjust.
Using Assets from a SWF
package {
import com.actionscriptbible.Example;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
public class ch16ex1 extends Example {
protected var loader:Loader;
public function ch16ex1() {
trace("Loading assets...");
loader = new Loader();
loader.load(new URLRequest(
"http://actionscriptbible.com/files/ch16assets.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
}
protected function onLoad(event:Event):void {
try {
var assetsDomain:ApplicationDomain =
loader.contentLoaderInfo.applicationDomain;
var FishingCatClass:Class =
Class(assetsDomain.getDefinition("assets.FishingCat"));
var cat:DisplayObject = new FishingCatClass();
cat.y = 100;
addChild(cat);
trace("added fishing cat!");
} catch (error:Error) {
trace(error);
}
}
}
} double clicking
double-clicks is off by default.
doubleClickEnabled = true;
addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
本文详细介绍了ActionScript中的二进制类型及其使用方法,包括如何读写ActionScript对象,利用AMF0和AMF3编码进行序列化与反序列化,并探讨了自定义类型的对象复制与类型转换。此外,还介绍了如何在Flash Player中管理和操作ByteArray。
728

被折叠的 条评论
为什么被折叠?



