View Full Version : Shared Library question...
CyanBlue
11-11-2008, 03:56 AM
Hm... I don't seem to be able to get this Shared Library in AS3/CS3... :(
I have SharedLibrary.fla file where I have a movieClip linked as Square for the class, flash.display.MovieClip, and Export for ActionScript and Export in first frame are checked...
I have SharedLibraryMain.fla file where I have the following script...
//
var loader:Loader = new Loader();
var request:URLRequest = new URLRequest("SharedLibrary.swf");
loader.contentLoaderInfo.addEventListener('complet e', completeHandler);
loader.load(request);
function completeHandler(e):void
{
var square:Class = getDefinitionByName("Square") as Class;
addChild(square);
}
But that gives me this '1067: Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject.' error message... I can use that method to use the image or font just fine, but I am not able to use a movieClip... What am I missing here???
Thanks... :)
aamenabar
11-11-2008, 04:11 AM
not sure, but this might help
var ClassReference:Class = getDefinitionByName("Square") as Class;
var myContent:MovieClip = (new ClassReference() as MovieClip);
addChild(myContent);
hope it helps
Mazoonist
11-11-2008, 04:23 AM
Yeah, looks as though you just forgot to use the "new" operator to make an instance. If var square is of type Class, you have to make an instance of it. What aamenabar said ^^.
CyanBlue
11-11-2008, 04:32 AM
Thanks for the reply... ;)
Ah... I see... Yeah... That does make sense... :)
However, I now get this error message with that code... The getDefinitionByName() function takes the class name as a string, but it is not recognizing what was defined in the SharedLibrary.swf... Any idea what I am missing???
ReferenceError: Error #1065: Variable Square is not defined.
at global/flash.utils::getDefinitionByName()
at SharedLibraryMain_fla::MainTimeline/completeHandler()
aamenabar
11-11-2008, 04:40 AM
this error sometimes shows when you haven't declared the class as public... or is it just an MC?
CyanBlue
11-11-2008, 04:43 AM
No, that's just one of the item in the library that has class name of Square... Maybe a sample file would be easier to explain... ;)
Mazoonist
11-11-2008, 04:59 AM
Actually, I haven't tried this before, and don't have experience with shared libraries. I just recently resolved an issue with getDefinitionByName, but in that situation, I was using it to access the current fla's library, not a loaded one. Someone else will have to help solve this, and I'll be interested in the answer, too.
CyanBlue
11-11-2008, 05:08 AM
This is somehow very different that what it used to be done in AS2... Or, maybe it is just a case where I am not knowing the new syntax... Hm...
Is there a better way to do this Shared Library stuff in AS3??? I have spent good amount of time googling for this with no luck...
Man... I am too old to stay up late... I'll check back when I get up in the morning... I'd really appreciate it if you guys can help me on this... Thanks... ;)
aamenabar
11-11-2008, 05:26 AM
OK, at this time of the night I couldn't get my head around this, but it works, check out this link:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000217.html
you'll probably have to download the Flash AS3 examples files to see it working.
there's just some class declarations that I don't get...
hope this leads to something... I'm just starting with this classes thing... I only know how to use "private" and "public", and never made a subclass that I'm aware of.
I'll keep looking into this.
Mazoonist
11-11-2008, 05:28 AM
I googled and found this page: http://forums.devshed.com/flash-help-38/problems-with-export-for-runtime-sharing-542510.html, and it seems to give the recipe for building a successful shared library. I didn't follow through and make one (yet) as it's getting late. I'll probably try it tomorrow.
CyanBlue
11-11-2008, 02:07 PM
I actually have played with the sample from the Adobe site, which works great, but I don't really understand why so many unnecessary classes declared there... I am planning to dissect it abit further later today...
I like his comment in the DevShed thread... "you have to do a bit of voodoo to get this to work" :)
Once again, his explanation sounds like it's got to work, but it requires creating multiple empty clip holders which kinda is the similar thing from the Adobe example done in timeline...
So, it "sounds" like you create 10 shared movieClips in the SharedLibrary.swf file, you have to create the exact replica shell in your main file to get this to work... I thought it was whole lot easier in AS2... Dang...
I'll fiddle this more later today and see if I can get this to work and get away with that idea... Thanks, guys... ;)
wvxvw
11-11-2008, 02:43 PM
RSLs aren't loaded when you load the SWF that refes them. I can think of this sort of workaround:
check in enter-frame / timer if the loaderInfo of the SWF containing references to RSL hasDefinition() of the class you want to use, once it's true, remove the timer / enter-frame handler.
CyanBlue
11-11-2008, 02:53 PM
Hm... That's interesting... So, you are saying that the SWF is fully loaded, yet the classes defined in it are not usable by that point???
wvxvw
11-11-2008, 03:39 PM
If those classes are defined only in the RSL, then they are not yet there when complete event is fired, you may look into the mx.core.RSLListLoader and mx.core.RSLItem classes to see more complicated, but, probably more proper way to handle RSL loading :)
CyanBlue
11-11-2008, 03:42 PM
Is that RSLListLoader class for Flex or Flash??? I don't see that if I search that inside the Flash IDE's help section...
wvxvw
11-11-2008, 03:47 PM
No, that's for Flex (mx), I don't think fl framework has any special utilities for loading RSLs :)
CyanBlue
11-11-2008, 03:50 PM
So, you were just teasing me, eh??? :p
CyanBlue
11-11-2008, 04:19 PM
Well, well, well... Flash manual to the rescue...
I was searching for the hasDefinition() function and found this example from the Flash manual, and it works great...
//
package
{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.*;
public class ApplicationDomainExample extends Sprite
{
private var loader:ClassLoader;
private var tf:TextField = new TextField();
public function ApplicationDomainExample()
{
addChild(tf);
loader = new ClassLoader();
loader.addEventListener(ClassLoader.LOAD_ERROR,loa dErrorHandler);
loader.addEventListener(ClassLoader.CLASS_LOADED,c lassLoadedHandler);
loader.load("SharedLibrary.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;
var imageClassRef:Class;
var myContent:MovieClip;
runtimeClassRef = loader.getClass("Square");
myContent = (new runtimeClassRef() as MovieClip);
myContent.x = 100;
myContent.y = 100;
addChild(myContent);
runtimeClassRef = loader.getClass("Circle");
myContent = (new runtimeClassRef() as MovieClip);
myContent.x = 200;
myContent.y = 100;
addChild(myContent);
imageClassRef = getDefinitionByName("KatieHolmes") as Class;
var imageData:BitmapData = new imageClassRef(0, 0);
var bmp:Bitmap = new Bitmap(imageData);
bmp.x = 300;
bmp.y = 100;
addChild(bmp);
}
}
}
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.CO MPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorE vent.IO_ERROR,ioErrorHandler);
loader.contentLoaderInfo.addEventListener(Security ErrorEvent.SECURITY_ERROR,securityErrorHandler);
}
public function load(lib:String):void
{
swfLib = lib;
request = new URLRequest(swfLib);
var context:LoaderContext = new LoaderContext();
context.applicationDomain=ApplicationDomain.curren tDomain;
loader.load(request,context);
}
public function getClass(className:String):Class
{
try
{
return loader.contentLoaderInfo.applicationDomain.getDefi nition(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));
}
}
Now, I need to find a way to call that in the initialization part of the main file and use those RSL items in the main file... Hm...
wvxvw
11-11-2008, 04:57 PM
I meant you could see what those classes do if you look into the source code :)
Actually, the loading process starts in SystemManager.mx_internal::initialize() and then uses an array of RSLItem's to load whatever it needs from RSLs.
CyanBlue
11-11-2008, 05:02 PM
Hm... I kinda get what you saying on that, but I'll check more on it... Thanks... ;)
lkchandler
12-30-2008, 07:52 PM
Jason (hi!)
You've probably already figured out this issue, but I noticed in the SharedLibrary.fla, that the shared items didn't have the middle box selected: export for runtime sharing - would all 3 boxes need to be selected for it to work?
I'm having an issue using a shared library myself. The shared item comes up fine the first time to the 'page' (an e-learning project), but if I replay the page or go forward a page, then back a page, I get that 1065 Variable not found message... still looking into that...
lkc
CyanBlue
12-30-2008, 08:08 PM
Holy cow!!! Where do I hide now??? What's up... :)
No, you don't need the second checkbox selected for the Shared Library use...
1065 error usually means that Flash cannot find the defined variable/class name, and the only two things I can think of is when the SharedLibrary gets unloaded for some reason when you come back to the page or the variable that references the class defined in the SharedLibrary gets deleted for some reason... Check to see if any of those happen... ;)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.