Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  Advanced Search
  HOME    TRACKER    Flash




ApplicationDomain



Hi,I am having some conceptual and practical problems with RSL assets and classes.I have the following code in an SWF document class:
Code:
var applicationDomain:ApplicationDomain = ApplicationDomain.currentDomain;var context:LoaderContext = new LoaderContext(false, applicationDomain);var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);loader.load(new URLRequest("../../RSL/bin/SLAssets.swf"), context);

Code:
private function completeHandler(event:Event):void{var NextButton:Class = event.target.content.loaderInfo.applicationDomain.getDefinition("NextButton");var nextButton:* = new NextButton();nextButtonHolder.addChild(nextButton);}
"NextButton" is the parameter in the "Class" field of the MovieClip that is in the library of "SLAssets.swf"This works fine, and i can see a button on the display list of the Document class that loaded the other SWF.But, i want the "NextButton" instance, to be of type "MyButton" (a custom class that extends MovieClip, in my code library). I want to be able to call e.g.
Code:
var nextButton.myCustMethod();
I am using the SLAssets.swf as a Shared Library container, and do not, therefore, want custom class linkages attached to them (in the BaseClass field).I am unable to cast anything to MyButton, and am unsure as to how to go about adding a "MyButton" instance to the display list.Regards, snapple :]



ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0
Posted on: 07-03-2008, 03:39 PM


View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread

ApplicationDomain And Fonts
Hi,

I am trying to do some dynamic font loading, by loading a .swf file containing the fonts I need to embed. It is partly working, I just need to be able to get the fontName of an embedded font name by reference.


Code:
trace(ApplicationDomain.currentDomain.getDefinition("Font_Navigation"));
That returns [class Font_Navigation] which is expected, and I can use the fon't name directly e.g. "Chiller", but I want to get the font name based on the "Font_Navigation" reference if that makes sense.

I can't appear to cast that class as a Font or anything, is there a way to achieve this?

Thanks for your time

ApplicationDomain With Loaded SWF
Consider this scenario:

Anyone has some ideas on how to solve this problem? I DO want the loader.swf and the module.swf to be in the same ApplicationDomain if possible.









Attach Code

// 1. Create a common class
package {
import flash.display.Sprite;
public class CommonClass extends Sprite {
public function CommonClass () {
};
}
}
// 2. Create a loader.swf by extending the CommonClass
package {
import flash.net.*;
import flash.display.*;
import flash.system.*;
public class LoaderClass extends CommonClass {
private var ldr:Loader;
public function LoaderClass () {
ldr = new Loader();
ldr.load(new URLRequest("module.swf"),new LoaderContext(false,new ApplicationDomain(ApplicationDomain.currentDomain)));
};
}
}
// 3. Create a module.swf by extending the CommonClass
package {
public class ModuleClass extends CommonClass {
public function ModuleClass () {
};
}
}
// Run the loader.swf and you will get the following error message:
// ReferenceError: Error #1074: Illegal write to read-only property CommonClass on global. at global$init()

Getting My Head Around ApplicationDomain
Hi,

Ok, so I have been messing around with applicationDomains.

Getting communication between a parent swf and a child swf and its various embedded classes is straight forward. I found you can either use applicationDomain.getDefinition or simply cast the the event.target.content property of the loaded swf to an object and access its methods that way. Creating new instances of classes etc still requires getDefinition however.

Getting the communication going the other direction is more difficult, well the docs seem to state that it should be easy using the applicationDomain approach, but it seems to not work like they state, or I am missing something.

Anyway, I found I had two options.

Simply call the parents methods using parent.method(). Which is fine, if you are dealing with display objects etc.

The other is to create a setter method in your child class, and pass this method a reference (this) to its instantiating class. Cast that reference as an Object, and you are good to go. Something I had to do to get this to work was declare the Object that I wanted to act as the reference to the parent as public and static.

The docs do state that if you use the correct application domain, ie
Quote:




Child of loader's ApplicationDomain. The default. You can explicitly represent this choice with the syntax new ApplicationDomain(ApplicationDomain.currentDomain) . This allows the loaded SWF file to use the parent's classes directly, for example by writing new MyClassDefinedInParent().




However, using that approach does not seem to let me use my parents classes using the syntax quoted. Its easy to create new instances of parent classes, but more difficult to access the document class of the parent without passing a reference. Is there a better way?

So I'm obviously missing something. Anyone know what?

I can some example files I have where I have a working parent/child and child/parent relationship working.

ApplicationDomain Problems
I have some fonts stored in SWF's that load at startup and then are registered with registerFont. It works perfectly well, unless the application that is doing the loading is itself loaded by another application using SWFLoader into a seperate ApplicationDomain. Then registerFont stops working. Has anybody encountered this and knows what the problem is.

Here's the code (actually part of a test program to isolate the problem):


Code:

private function InitApp():void {
var ldr:Loader= new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, regfont);
ldr.load(new URLRequest("Bogusflow.swf"));
}

private function regfont(event:Event):void {
var Bogusflow:Class = Class(LoaderInfo(event.target).applicationDomain.getDefinition
("Bogusflow"));
Font.registerFont(Bogusflow.wrFont);
}
(Works fine unless the SWF containing the above is itself loaded via SWFLoader into a seperate ApplicationDomain.)

Here is the font file:


Code:

package {
import flash.display.Sprite;
public class Bogusflow extends Sprite {
[Embed(systemFont='Bogusflow', fontName='Bogusflow', fontStyle='normal',
fontWeight='normal',
mimeType='application/x-font')]
public static var wrFont:Class;
}
}
And the code that loads the application:


Code:

private function InitApp():void {

var swf:SWFLoader = new SWFLoader();

swf.scaleContent = false;


//comment this out and the problem goes away:
//(it needs to stay in though)
swf.autoLoad = false;
swf.loaderContext =
new LoaderContext(false,new ApplicationDomain(null));
swf.trustContent = true;
////

Application.application.addChild(swf);

swf.load("test.swf");
}

ApplicationDomain.currentDomain Equality
Hi all,
I'm having a problem comparing an ApplicationDomain to another. Here's a very simple example:

var test1:ApplicationDomain = ApplicationDomain.currentDomain;
trace(String(test1 === ApplicationDomain.currentDomain));

This returns false. So does ==. ObjectUtil.compare returns true, however it also returns true when comparing different applicationDomains pointing at the same root.

Any help would be greatly appreciated! Thanks,
Josh

[AS3] How To Solve This ApplicationDomain Problem?
Hi,

when you load a .swf into another .swf, and the loaded .swf uses a own Class-Type that`s also used in the loader .swf, the Compiler throws a RefernceError:


Quote:




ReferenceError: Error #1074: Illegal write to read-only property com.MyPackage::TestClass on global.
at global$init()




That`s because the loader .swf already imported this Class, now when you load a .swf into where also uses this Class, it will try to overwrite the Class-Definition. And this is forbidden.

Please look at this example, it will explaind it better.

It`s curios in AS2 it makes no problem but in AS3.

Is there a solution for this?

ApplicationDomain Class Question
Im looking for a way to divide all my application code (classes) into separate swf files. Im looking for a good design pattern to use, to manage the application. I came accross the ApplicationDomain class while looking into the Flash CS3 documentation. It looks quite helpful. But does anybody know any other class (or design pattern) that I can use to manage this?
My main issue with ApplicationDomain (correct me if I'm wrong), is say I have 4 classes, class A,B,C, and D. In my main.swf I have classes A, and B. And in module1.swf I have classes C, and D. However, in the definition of class C, I am importing class A. You see where this is becoming a problem? Is it very important for me to find a way where class C does NOT import class A?

Runtime Sharing VS ApplicationDomain
Does anyone know how runtime sharing (through linkage properties) and sharing classes through applicationDomain class are related?

I feel that they're the same, but runtime sharing through linkage is made mainly for non-programmers, do provide a GUI of sharing classes. Is this true or not?

Im interested in using the linkage properties but the only prob facing me is that the URL of the shared file must be hard-typed (and can't be changed dynamically). I want it to change dynamically so that my main application file can choose from within different shared asset files (URLs), same concept as a CSS file, where each shared asset will give a different view/appearance/style for the application

Thanks!

Abusing ApplicationDomain To Skin Components
I've been trying to skin the v3 List component via code for over a week now, and I cannot find a way to do so. So I decided to try another approach, which I'm also having problems with. Hopefully someone here can help me.

Since the only information I can ever find about skinning components instructs me to double click on them (which is totally unhelpful when manipulating components in actionscript), I figured that if I could mimic that with code, perhaps I could achieve the same goal.

So I started playing with loading in SWF files with libraries of Sprites intended to act as replacements for the default component skins.

I'm doing this by manipulating the applicationDomain. The theory being that, if a needed asset cannot be found in the parent SWF, it will look in the child SWF. But thus far I haven't found a way to do this. I keep getting runtime errors because the component is looking for library assets that don't exist in the main movie.

Can this be done? Looking over ApplicationDomain, it really looks like it should work, but I can't get it to.

Exception On RegisterFont With Seperate ApplicationDomain
I have a "Font Library" containing one font:


Code:

package {
import flash.display.Sprite;
public class Bogusflow extends Sprite {
[Embed(systemFont='Bogusflow', fontName='Bogusflow', fontStyle='normal',
fontWeight='normal',mimeType='application/x-font')]
public static var wrFont:Class;
}
}
I can load it from application "X" (an SWF file) as follows, with no errors or exceptions:

X:


Code:

private function InitApp():void {
var ldr:Loader= new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, regfont);
ldr.load(new URLRequest("Bogusflow.swf"));
}

private function regfont(event:Event):void {
var Bogusflow:Class = Class(LoaderInfo(event.target).applicationDomain.
getDefinition("Bogusflow"));
Font.registerFont(Bogusflow.wrFont);
}
However, if I load application X from another application "Z" via SWFLoader, and I also specify a new LoaderContext, I get an exception in X when it tries to register the font (although the rest of X still loads).

Z:


Code:

private function InitApp():void {

var img:SWFLoader = new SWFLoader();

img.scaleContent = false;

img.autoLoad = false;
img.loaderContext =
new LoaderContext(false,new ApplicationDomain(null));
img.trustContent = true;


Application.application.addChild(img);

img.load("X.swf");

The exception currently is, "Cannot access a property or method of a null object reference", but at other times the error msg has referenced the font parameter of RegisterFont. The exception goes away if I comment out the new LoaderContext as defined (with the new ApplicationDomain), but I need to keep it in as I get other errors I don't understand when the ApplicationDomain is shared between X and Z.

I guess it will be a miracle if someone actually reads all this and knows what the solution is.

Class From ApplicationDomain Causing Hard Crash
In the complete handler for a loader I'm calling this:


PHP Code:



var _class:Class = (event.target.applicationDomain.getDefinition('LoadeeClass')) as Class;




And that seems to work ok...it traces correctly. Later I try and instantiate that class like so:


PHP Code:



var mc:MovieClip = new _class();




And my IDE locks up and eventually crashes (no traces get out and I can't catch any exceptions)...I have to admit I've never used appDomain before but I should at least get an exception or something...any thoughts out there?

Accessing Classes In Loaded Swf (ApplicationDomain) Question
I'm having problems getting my head around what I think should be fairly easy.

I am making a simple charting application that is comprised of a main.swf that contains the main GUI interface and loads in an external line.swf that is its own class (Line.as as the document class for the line.fla). Within this line.fla library there is a movieClip of a bezier point graphic that gets added to the display within the line.swf as the user clicks to draw. The point mc's Class has been set to AdjustPoint with a base class of flash.display.MovieClip in the symbol properties panel of the line.fla so that it can be added dynamically to the display list.

I am loading the line.swf in and setting its ApplictionDomain to the currentDomain (the main.swf) so that all the class references of the line.swf are resident in the main app domain (or so I thought):


ActionScript Code:
otherAssetsLoader = new Loader();
var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
otherAssetsLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,otherAssetsLoaded);
var _request:URLRequest=new URLRequest();
_request = new URLRequest("line.swf");
otherAssetsLoaderArray.load(_request,ldrContext);

Then later on when the user clicks the button in the main.swf that allows the user to draw lines on the screen I simply add a new instance of the Line class to the screen:


ActionScript Code:
private function buttonClick(event:MouseEvent):void {
    var newLine:Line = new Line();
    lineArray.push(newLine);
    container_mc.addChild(newLine);
    newLine.startLine();
}

the startLine() function call creates some initial event listeners, one of which is a onClick event that will add the instance of the AdjustPoint class in the line.fla library to the display list.

However, when I compile the main.swf Flash gives me the error:

1180: Call to a possibly undefined method AdjustPoint.

So obviously the AdjustPoint class in the line.fla isn't being found. I assumed that by adding:


ActionScript Code:
var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);

to the loader this would make the class readily available. And, in fact, if I trace:


ActionScript Code:
trace(ApplicationDomain.currentDomain.getDefinition("AdjustPoint"))

after the loader is finished I can indeed see the "AdjustPoint" class. It just doesn't seem to be recognized by the line class anymore once loaded into the main.swf.

I can solve this by just copying the adjust point mc from the line.fla to the main.fla library. When I compile then everything works as expected... no errors. But obviously, I'd rather not do this.

What am I misunderstanding here? I'm lost as to how to make the AdjustPoint class available to the line.swf class once its loaded into the main.swf.

Any advice is greatly appreciated.

Copyright © 2005-08 www.BigResource.com, All rights reserved