大家好,我是奶綠茶
在 flash 裡,想要透過程式 , 在 runtime 時產生一個新的 swf 檔
可以使用 swfassist
但無法自定類別或是屬性。
今天來介紹另一套類別:as3-commons-bytecode
這套類別可自定一個新的類別, 加入自定的屬性和方法,然後另存一個新的 swf 檔
但目前奶綠我試的結果,無法加入圖片或影格(如果有好心人事試出來的話,也請指教一下)
1. 先下載他的 類別包, 要載四個 swf
as3commons-bytecode-0.9.swc
as3commons-lang-0.3.1.swc
as3commons-logging-1.2.swc
as3commons-reflect-1.3.3.1.swc
都在他的 project 頁面裡。
測試
進階, 使用 interface
先準備一個 interface
測試
轉載請註明出處 http://milkmidi.blogspot.com/2010/12/generate-swf.html
在 flash 裡,想要透過程式 , 在 runtime 時產生一個新的 swf 檔
可以使用 swfassist
但無法自定類別或是屬性。
今天來介紹另一套類別:as3-commons-bytecode
這套類別可自定一個新的類別, 加入自定的屬性和方法,然後另存一個新的 swf 檔
但目前奶綠我試的結果,無法加入圖片或影格(如果有好心人事試出來的話,也請指教一下)
1. 先下載他的 類別包, 要載四個 swf
as3commons-bytecode-0.9.swc
as3commons-lang-0.3.1.swc
as3commons-logging-1.2.swc
as3commons-reflect-1.3.3.1.swc
都在他的 project 頁面裡。
測試
/** * @author milkmidi * @see http://milkmidi.blogspot.com * @version 1.0.1 * @date created 2010/12/27/ */ package { import flash.display.Sprite; import flash.events.Event; import flash.net.FileReference; import flash.system.ApplicationDomain; import flash.utils.ByteArray; import org.as3commons.bytecode.abc.enum.Opcode; import org.as3commons.bytecode.emit.IAbcBuilder; import org.as3commons.bytecode.emit.IClassBuilder; import org.as3commons.bytecode.emit.IMethodBuilder; import org.as3commons.bytecode.emit.impl.AbcBuilder; import org.as3commons.reflect.AccessorAccess; public class MyDemo1 extends Sprite { protected static const PACKAGE :String = "milkmidi.generated"; protected static const CLASS :String = "MyGeneratedClass"; protected static function getPackageAndClassName():String { return PACKAGE + "." + CLASS; } private var _abcBuilder :IAbcBuilder; public function get abcBuilder():IAbcBuilder { return _abcBuilder; } private var _classBuilder :IClassBuilder; public function get classBuilder():IClassBuilder { return _classBuilder; } public function MyDemo1() { init( PACKAGE , CLASS ); generatProperties(); generated(); } private function init(pPackage:String , pClass:String):void { _abcBuilder = new AbcBuilder(); _classBuilder = _abcBuilder.definePackage(pPackage).defineClass(pClass); } protected function generatProperties():void { // generate public property _classBuilder.defineProperty("myName", "String", "milkmidi"); // generate set/get _classBuilder.defineAccessor("myInt", "int", 100).access = AccessorAccess.READ_ONLY; // generate method /* * public function multiplyByHundred(pValue:int):int{ * return pValue * 100; * } * */ var _methodBuilder:IMethodBuilder = _classBuilder.defineMethod("multiplyByHundred"); _methodBuilder.returnType = "int"; _methodBuilder.defineArgument("int"); _methodBuilder.addOpcode(Opcode.getlocal_0) .addOpcode(Opcode.pushscope) .addOpcode(Opcode.getlocal_1) .addOpcode(Opcode.pushint, [100]) .addOpcode(Opcode.multiply) .addOpcode(Opcode.setlocal_1) .addOpcode(Opcode.getlocal_1) .addOpcode(Opcode.returnvalue); } protected function generated():void{ /* 另存成一個實體的 .swf 檔。 var _bytesSWF :ByteArray = abcBuilder.buildAndExport(); new FileReference().save( _bytesSWF ,getPackageAndClassName() +".swf"); return; //*/ _abcBuilder.addEventListener(Event.COMPLETE, _abcBuilderCompleteHandler); //loads the generated class into the current application domain _abcBuilder.buildAndLoad(ApplicationDomain.currentDomain); } protected function _abcBuilderCompleteHandler(e:Event):void { var _clazz :Class = ApplicationDomain.currentDomain.getDefinition(PACKAGE + "." + CLASS) as Class; var _instance :Object = new _clazz(); trace(_instance); trace("myName:"+_instance.myName); trace("myInt:"+_instance.myInt); var _result:int = _instance.multiplyByHundred(5); trace("multiplyByHundred() > 10 :"+_result); } }//__________________________________________________________________________________ End Class }//__________________________________________________________________________________ End Package
進階, 使用 interface
先準備一個 interface
package iface { public interface IVO { function get date():String; function get value():int; } }
測試
/** * @author milkmidi * @see http://milkmidi.blogspot.com * @version 1.0.1 * @date created 2010/12/27/ */ package { import flash.display.BitmapData; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.system.ApplicationDomain; import iface.IVO; import org.as3commons.bytecode.abc.enum.Opcode; import org.as3commons.bytecode.emit.IAbcBuilder; import org.as3commons.bytecode.emit.IClassBuilder; import org.as3commons.bytecode.emit.IMethodBuilder; import org.as3commons.bytecode.emit.impl.AbcBuilder; import org.as3commons.reflect.AccessorAccess; public class MyDemo2 extends MyDemo1 { public function MyDemo2() { } override protected function generatProperties():void { classBuilder.implementInterface("iface.IVO"); classBuilder.defineAccessor("date", "String", "2010.12.14").access = AccessorAccess.READ_ONLY; classBuilder.defineAccessor("value", "int", 999).access = AccessorAccess.READ_ONLY; //classBuilder.defineAccessor("bitmapData", "flash.display.BitmapData", new BitmapData(1,2)).access = AccessorAccess.READ_ONLY; //classBuilder.defineProperty("bitmapData", "flash.display.BitmapData", new BitmapData(1, 2)); //trace(123); } override protected function _abcBuilderCompleteHandler(e:Event):void { var _clazz :Class = ApplicationDomain.currentDomain.getDefinition( getPackageAndClassName() ) as Class; var _instance :IVO = new _clazz() as IVO; trace("date:" + _instance.date); trace("value:" + _instance.value); } }//__________________________________________________________________________________ End Class }//__________________________________________________________________________________ End Package
轉載請註明出處 http://milkmidi.blogspot.com/2010/12/generate-swf.html
留言