View unanswered posts | View active topics


Reply to topic  [ 5 posts ] 
Loading SWC file at run time 
Author Message
Member

Joined: Tue May 19, 2009 4:18 am
Posts: 35
Post Loading SWC file at run time
Hi

I want to load swc at run time for that i had zipped the swc using winrar and after ziping it it contains one catalog.xml file with the class name information.I tried to load that swf with the same name as the swc obtained after zipping the swc into the current Application Domain code is listed below

package {

import flash.system.*;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Loader;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.text.TextField;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
public class ApplicationDomainExample extends Sprite
{

private var loader:ClassLoader;
private var tf:TextField = new TextField();
public function ApplicationDomainExample()
{
addChild(tf);
trace("Inside Application Domain");
loader = new ClassLoader();
loader.addEventListener(ClassLoader.LOAD_ERROR,loadErrorHandler);
loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
loader.load("HoloPicker.swf");
}
private function loadErrorHandler(e:Event):void
{
tf.text = "Load failed";
throw new IllegalOperationError("Cannot load the specified file.");
}

private function classLoadedHandler(e:Event):void
{
var runtimeClassRef:Class = loader.getClass("HoloPicker");
var greeter:Object = new runtimeClassRef();
trace("Greeeeeeeeeeter"+greeter);
// tf.text = greeter.greet();

}
}
}
import flash.display.Loader;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
class ClassLoader extends EventDispatcher {
public static var CLASS_LOADED:String = "classLoaded";
public static var LOAD_ERROR:String = "loadError";
private var loader:Loader;
private var swfLib:String;
private var request:URLRequest;
private var loadedClass:Class;
public function ClassLoader() {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
}
public function load(lib:String):void {
swfLib = lib;
request = new URLRequest(swfLib);
var context:LoaderContext = new LoaderContext();

context.applicationDomain=ApplicationDomain.currentDomain;

loader.load(request,context);
}
public function getClass(className:String):Class {
try {

return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
} catch (e:Error) {
throw new IllegalOperationError(className + " definition not found in " + swfLib);
}
return null;
}
private function completeHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
}
private function ioErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
private function securityErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
}



after executing this code i had thefollowing error message

Error: HoloPicker definition not found in HoloPicker.swf
at ::ClassLoader/getClass()
at ApplicationDomainExample/::classLoadedHandler()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at ::ClassLoader/::completeHandler()


The HolloPicker.swf used in the code sample is obtained by extracting the HolloPicker.swc and the class name tried to access is HolloPicker.I got this name as the class name from the catalog.xml

Is that possible to load the class and call the functions inside it with the Application DomainClass
Pls Helppppppppppp


Tue May 19, 2009 4:33 am
Profile E-mail
Member

Joined: Wed Aug 01, 2007 3:37 pm
Posts: 1130
Location: Roseville, CA
Post Re: Loading SWC file at run time
To add an SWC file to an AS3 project in FlashDevelop:

  • Move the SWC so that it is in the project directory somewhere
  • Find the SWC in the Project panel, right-click and select "Add To Library"

To create your own SWC file from FlashDevelop, try looking into the "Export SWC" plugin here:

viewtopic.php?f=4&t=4099

If you want to use the SWC at runtime, I would first recommend that you make sure the SWC works correctly when included at compile time. That would probably be the easiest way to tackle problems if you are experiencing errors. Once you are able to use the SWC without errors, right-click the SWC in the Project panel again and select "Options". Set the mode to "External library", so that FlashDevelop uses the file for code completion but does not compile it with your SWF. I don't use runtime libraries (other than the Flex framework when appropriate), but I'm sure that you could then add the reference as a compiler option under your project's properties.


Tue May 19, 2009 5:32 am
Profile WWW
Member

Joined: Tue May 19, 2009 4:18 am
Posts: 35
Post Re: Loading SWC file at run time
elyon wrote:
To add an SWC file to an AS3 project in FlashDevelop:

  • Move the SWC so that it is in the project directory somewhere
  • Find the SWC in the Project panel, right-click and select "Add To Library"

To create your own SWC file from FlashDevelop, try looking into the "Export SWC" plugin here:

viewtopic.php?f=4&t=4099

If you want to use the SWC at runtime, I would first recommend that you make sure the SWC works correctly when included at compile time. That would probably be the easiest way to tackle problems if you are experiencing errors. Once you are able to use the SWC without errors, right-click the SWC in the Project panel again and select "Options". Set the mode to "External library", so that FlashDevelop uses the file for code completion but does not compile it with your SWF. I don't use runtime libraries (other than the Flex framework when appropriate), but I'm sure that you could then add the reference as a compiler option under your project's properties.


Iam using Flash cs3. Is the above mentioned steps to be used in cs3?


Tue May 19, 2009 7:25 am
Profile E-mail
Member

Joined: Sun May 11, 2008 3:01 pm
Posts: 860
Post Re: Loading SWC file at run time
You can compile SWC from Flash (check Export SWC in Publish Settings > Flash). But, I don't know if you can output SWZ (this is the runtime shared library file extension) using Flash, I'm inclined to think you cannot... though, you should be able to use a plain SWF as the shared library...

_________________
http://www.couchsurfing.com/people/wvxvw


Wed May 20, 2009 1:23 am
Profile
Member

Joined: Thu Dec 10, 2009 11:46 am
Posts: 1
Post Re: Loading SWC file at run time
the example below is same as the one given in livedocs.
I tried the same and it works, you have to create both the projects as ActionScript Projects especially the one of RuntimeClasses
When i load the swf of flex project and tried to get the class out of it, it doesn't work but the same works when my RuntimeClasses project is an actionscript project.


Thu Dec 10, 2009 11:48 am
Profile E-mail
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.