forum > External Library in haXe
-
Win john Aug 24 at 08:39
I'm starting with Loader.But I have a problem.I want to call MovieClip from external swf.My code works in flash cs3.I get some error in haXe.
My code:package { import flash.display.MovieClip; import flash.display.Loader; import flash.display.DisplayObject; import flash.net.URLRequest; import flash.events.Event; import flash.utils.getQualifiedClassName; public class Main extends MovieClip { var loader:Loader=new Loader(); public function Main() { loader.load(new URLRequest("ExSwf.swf")); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,comp); } var clsObj:Object; function onframe(ev:Event){ clsObj.rotation++; } function comp(event:Event):void { //error.How to change 'as Class' to haXe? var cls:Class=event.target.content.loaderInfo.applicationDomain.getDefinition("box")as Class clsObj=new cls(); clsObj.x=100; clsObj.y=100; //error in DisplayObject addChild(DisplayObject(clsObj)); processCompleted(); stage.addEventListener(Event.ENTER_FRAME,onframe); function processCompleted():void { trace(getQualifiedClassName(cls)); } } } }
thanks -
Haxe is stricter than AS3 so it is hard to be sure without me running the code, it looks roughly fine. I have enclosed some old haxe flash code that maybe useful? I tend to use signals now but maybe it's useful. In haxe we tend to use Type and Reflect for some aspects of what your doing see http://haxe.org/doc/cross/reflect. The code below may have bugs in some of the preloader info, it's just the first non signal loading stuff I could dig up, used on a very small project, but largely it all works so it maybe useful.
Cheers ;Jpackage net.justinfront.utils; import flash.display.Sprite; import flash.display.MovieClip; import flash.display.Loader; import flash.display.Bitmap; import flash.display.BitmapData; import flash.system.LoaderContext; import flash.system.ApplicationDomain; import flash.net.URLRequest; import flash.events.EventDispatcher; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; import flash.events.MouseEvent; import net.justinfront.events.SimpleLoadEvent; enum Domain { Same; New; } class SimpleLoading extends EventDispatcher { /** The loader instance */ private var _loader: Loader; /** Loader context not really used for the carousel */ private var _loaderContext: LoaderContext; /** Image or swf file location */ private var _location: String; /** Internal URLrequest used in loading */ private var _url: URLRequest; /** A reference for the image that can be stored in an arroy for instance so image can relate to content */ private var _ref: Dynamic; /** Total bytes loaded ( can be used for progress bars) */ private var _total: Int; /** holds loaded content */ private var _container: MovieClip; private var _domain: Domain; private var _loadedApplicationDomain: ApplicationDomain; //only applies to new public var isButton: Bool; /** * @Constructor */ public function new( ref_, location_: String, domain_: Domain, ?checkPolicy: Null<Bool> = false, ?isButton_: Null<Bool> = true ):Void { _loader = new Loader(); _ref = ref_; _location = location_; _loaderContext = new LoaderContext( checkPolicy ); _domain = domain_; isButton = isButton_; switch( _domain ) { case New: _loaderContext.applicationDomain = new ApplicationDomain(); _loader.contentLoaderInfo.addEventListener( Event.INIT, addMovie ); case Same: _loaderContext.applicationDomain = ApplicationDomain.currentDomain; _loader.contentLoaderInfo.addEventListener( Event.INIT, libloaded ); } _loadedApplicationDomain = _loaderContext.applicationDomain; _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, downloadProgressFirst ); _loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, loadError ); super(); } /** * Starts loading, separate to allow setting of listeners. */ public function load():Void { var url: URLRequest = new URLRequest( _location ); _loader.load( url, _loaderContext ); } /** * * Lets you know when a library has loaded. * * @pram e Event that the movie has loaded * * */ private function libloaded( e: Event ):Void { dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.LIBRARY_LOADED, true, false, _ref, e.target.content ) ); dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.COMPLETED, true, false, _ref, e.target.content ) ); } /** * * call back when image/swf loaded * wraps the loaded as a movie button (if button) * dispatches that it has loaded * * @pram e Event that the movie has loaded * */ private function addMovie( e: Event ): Void { var mc: MovieClip = wrapLoaded( e );// as MovieClip; dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.VISUALLOADED, true, false, _ref, mc ) ); dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.COMPLETED, true, false, _ref, mc ) ); } /** * wraps the loaded movie or image and creates a hitarea * maps the Mouse down to the class * @param e Event * @return wrapped movie */ private function wrapLoaded( e: Event ): MovieClip { var mc: MovieClip = new MovieClip(); _container = new MovieClip(); e.target.content.smoothing = true; _container.addChild( e.target.content ); var w: Int = cast( _container.width ); var h: Int = cast( _container.height ); var rollover: Bitmap = new Bitmap( new BitmapData( w, h, true, 0x00000000 )); if( isButton ) { rollover.smoothing = true; _container.addEventListener( MouseEvent.MOUSE_DOWN, pressed ); _container.buttonMode = true; _container.mouseChildren = false; } _container.addChild( rollover ); mc.addChild( _container ); //mc._container = _container; //mc.x = -1000; //mc.y = -1000; return mc; } /** * Used in providing progress information * @param e */ private function downloadProgressFirst( e: ProgressEvent ):Void { _total = e.bytesTotal; _loader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, downloadProgressFirst ); downloadProgress( e ); _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, downloadProgress ); } /** * Allows progress of download to be tracked * @param e Standard progress event */ private function downloadProgress( e: ProgressEvent ):Void { dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.PROGRESS, true, false, _ref, Math.floor( e.bytesLoaded*100/e.bytesTotal ) ) ); } // so can be overridden public function loadError( e: IOErrorEvent = null ):Void { dispatchEvent( e ); } private function pressed( e: Event ):Void { dispatchEvent( new SimpleLoadEvent( SimpleLoadEvent.PRESSED, true, false, _ref, _container ) ); dispatchEvent( new MouseEvent( MouseEvent.MOUSE_OUT ) ); } //////////////GETTERS & SETTERS/////////////// /** * getter for bytes loaded */ public var totalBytes( gettotalBytes, null ): Int; private function gettotalBytes(): Int { return Math.round( _total ); } } package net.justinfront.events; import flash.events.Event; class SimpleLoadEvent extends Event { public static inline var PROGRESS: String = "simpleloading_progress"; public static inline var PRESSED: String = "simpleloading_pressed"; public static inline var VISUALLOADED: String = "simpleloading_visuallyloaded"; public static inline var LIBRARY_LOADED: String = "simpleloading_library_loaded"; public static inline var COMPLETED: String = "simpleloading_complete"; public var ref( default, null ): Dynamic; public var data( default, null ): Dynamic; public function new( type: String, ?bubbles: Null <Bool>, ?cancelable: Null <Bool>, ?eventRef: Null <Dynamic>, ?eventData: Null <Dynamic> ) { if ( bubbles == null ) bubbles = false; if ( cancelable == null ) cancelable = false; super( type, bubbles, cancelable ); ref = eventRef; data = eventData; } override public function clone():Event { return new SimpleLoadEvent( type, bubbles, cancelable, ref, data ); } }
package net.justinfront.utils; import flash.display.Stage; import flash.display.Sprite; import flash.display.DisplayObject; import flash.display.MovieClip; import flash.text.TextField; import flash.geom.Rectangle; import flash.Lib; import flash.media.SoundTransform; import flash.events.MouseEvent; import flash.events.Event; import flash.system.ApplicationDomain; typedef SymbolInfo = { classType: Class<Dynamic>, classNom: String, nom: String, layout: String, pos: Rectangle } enum Symbol{ movie ( desciption: String, nom: String, mc: MovieClip, frames: Int ); button ( desciption: String, nom: String, mc: MovieClip, _up: Int, _over: Int, _down: Int, _hit: Int ); graphic ( desciption: String, nom: String, mc: MovieClip ); } class LoadedLib { private var _stage: MovieClip;//Stage; // A list of instances and positions public var _symbolsList: List<SymbolInfo>; // A hash of avalible symbols public var _symbolsHash: Hash<Class<Dynamic>>; public var _symbolInfoHash: Hash<SymbolInfo>; // TODO: implement usePosition public function new( ?layout: String, ?usePosition: Bool ):Void { _symbolsList = new List(); _symbolsHash = new Hash(); _symbolInfoHash = new Hash(); // Is stage correct?? Is Lib.current Ok?? _stage = Lib.current;//.stage; if( layout != null ) { addLib( layout ); } } // Add swf to stage and get content // TODO: implement usePosition public function addLib( layout: String, ?usePosition: Bool, ?offPosSetRectangle: Rectangle ):Void { //Need a catch in here or something if not Same domain var layoutMC: MovieClip = createLibraryMovieCalled( layout ); var child: Dynamic; var clas: Class<Dynamic>; var clasNom: String; var mc; var iter = new IntIter( 0, layoutMC.numChildren ); var symbolInfo: SymbolInfo; for ( i in iter ) { child = layoutMC.getChildAt( i );//trace( child.getChildIndex() ); clas = Type.getClass( child ); clasNom = Type.getClassName( clas ); //trace('classNom' + clasNom ); symbolInfo = { classType: clas, classNom: clasNom, nom: child.name, layout: layout, pos: new Rectangle( child.x, child.y, child.width, child.height ) } _symbolsList.add( symbolInfo ); //check name good if( !_symbolInfoHash.exists( child.name ) ){_symbolInfoHash.set( child.name, symbolInfo );} if( !_symbolsHash.exists( clasNom ) ){_symbolsHash.set( clasNom, clas );} } Lib.current.removeChild( layoutMC ); layoutMC = null; } public static function createMovie( clasNom: String ): MovieClip { var mc: MovieClip = cast( Type.createInstance( Type.resolveClass( clasNom ), [] ), MovieClip); return mc; } public function createMovieByInstance( instanceNom: String ):MovieClip { var info: SymbolInfo = _symbolInfoHash.get( instanceNom ); var mc: MovieClip = cast( Type.createInstance( info.classType, new Array() )); mc.name = info.nom; var rec: Rectangle = info.pos; mc.x = rec.x; mc.y = rec.y; return mc; } public static function createTextField( mcLinkage: String, txtFieldInstance: String ): TextField { var mc: MovieClip = cast( Type.createInstance( Type.resolveClass( mcLinkage ), [] ), MovieClip); var txt: TextField = cast( mc.getChildByName( txtFieldInstance ), TextField ); mc = null; return txt; } // used when iterating the _symbolsList public function createMovieById( clasNom: String ): MovieClip { var mc: MovieClip; try { mc = cast( Type.createInstance( _symbolsHash.get( clasNom ), new Array() )); } catch ( err: Dynamic ) { mc = cast( Type.createInstance( ApplicationDomain.currentDomain.getDefinition( clasNom ), new Array() )); } return mc; } // used when iterating the _symbolsList public function getTitleFromInfo( _symbolInfo: SymbolInfo ): String { return _symbolInfo.nom; } public static function getClassNameFromInfo( _symbolInfo: SymbolInfo ):String { return _symbolInfo.classNom; } // used when iterating the _symbolsList public function createSymbolFromInfo( _symbolInfo: SymbolInfo ): MovieClip { var mc: MovieClip = cast( Type.createInstance( _symbolInfo.classType, new Array() )); //mc.x = _symbolInfo.pos.x; //mc.y = _symbolInfo.pos.y; return mc; } // public function createLibraryMovieCalled( nom: String ): MovieClip { var mc: MovieClip = cast( Type.createInstance( Type.resolveClass( nom ), new Array() ), MovieClip); mc.stop(); var soundTransform = new SoundTransform(); soundTransform.volume = 0; mc.soundTransform = soundTransform; mc.visible = false; Lib.current.addChild( mc ); return mc; } // returns information on the symbol frames public static function checkSymbol( mc: MovieClip, nom: String ):Symbol { // this could be use full for button view var overFrame: Int = 0; var downFrame: Int = 0; var hitFrame: Int = 0; // var totFrames: Int = mc.totalFrames; if( mc.totalFrames == 1 ) { return graphic( 'graphic', nom, mc ); } else { mc.gotoAndStop('_over'); if( mc.currentFrame != 1 ) { overFrame = mc.currentFrame; } mc.gotoAndStop(1); mc.gotoAndStop('_down'); if( mc.currentFrame != 1 ) { downFrame = mc.currentFrame; } mc.gotoAndStop(1); mc.gotoAndStop('_hit'); if( mc.currentFrame != 1 ) { hitFrame = mc.currentFrame; } if( hitFrame + overFrame + hitFrame != 0 ) { mc.addEventListener( MouseEvent.MOUSE_DOWN, function( e: Event ){}); mc.buttonMode = true; mc.mouseChildren = false; return button( 'button',nom, mc, 1, overFrame, downFrame, hitFrame ); } else { return movie( 'movie',nom, mc, totFrames ); } } } public static function getSymbolDescription( symbol: Symbol ):String { switch symbol { case movie(description,a,b,frames): return description; case button(description,a,b,c,d,e,f): return description; case graphic(description,a,b): return description; } } }
-
I think the third class i posted assumes a certain timeline structure so you may need to adjust it to your needs but should atleast give ideas :)
-
Win john Aug 25 at 11:05
Very detailed.It is quite hard.Thanks again.Now,I know two ways: the way of your and loader a resource.swf contains movieclip at server
-
Win
I am not expert on code theories, but normally I find away to make stuff, what are you trying to create, maybe i can help. People often ask one thing, but... if they say what they want to do, or more rightly and what they actually want to create - it's easier than understanding their code arrangement, even if I can only offer a part of the solution.
Cheers
;j