Groups | Blog | Home
all groups > flash actionscript > july 2007 >

flash actionscript : Class exclusion in AS3 in Flash IDE


Patrick B
7/24/2007 2:42:49 PM
Hi Steve,

Maybe I'm missing something but don't you simply have to remove the
"import" statement? The only time a class will be imported and included
is is:
a) You import it
and
b) You specify a variable of that type (eg. var myType:SomeClass;)

Patrick

[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Patrick B
7/24/2007 4:56:07 PM
Hi again,

I'm not sure what it is that you're trying to do here. Presumably, if
you're importing a class it's because you're using it, no? If not then
why was the import added in the first place? Imports aren't
automatically added to classes so for them to have been added, someone
decided that they needed to use the methods/properties in those classes.
The only time this wouldn't be true would be if someone didn't use
common sense and simply imported all packages in every single class.
Basically, if the class isn't being used, it shouldn't/wouldn't have
been imported. Simply removing the class import from classes that use
the methods will cause all sorts of compile-time errors and for good reason.

Maybe you could provide me with an example of what the actual problem
is because I'm having trouble imagining what it is. I may also be able
to offer a creative solution...I spent a good chunk of time developing
an API that was flexible fro the top down so that changes in one file
simply trickle out to the whole application without having to change
every single file. But again....I don't know what your specific
situation is so let's start there.

Patrick


[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
swrb1977
7/24/2007 6:19:38 PM
Hi,

I've been looking high and low for a way to exclude class file from compiling
into a swf on publish, similar to the functionality available when publishing
AS2 apps (outlined here:
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?c
ontext=LiveDocs_Parts&file=00000817.html).

I've found references on how to do this when compiling with the command-line
compiler (detailed here: http://www.bit-101.com/blog/?p=941), but so far I've
haven't been able to find anything that details how to do this in the IDE.

I'm hoping someone here might have an answer. I'm going to change my approach
to a number of applications if I can't find an answer to this.

Thanks,

--Steve
swrb1977
7/24/2007 7:36:54 PM
I hope that's not official solution. I'm pretty sure that removing the imports
would result in compile errors and prevent successful publishing. In your
example the compiler would have no idea how to type check "SomeClass".

From a workflow perspective, it would be awful to have to hunt down and remove
every import for a given class before publishing. The as2 solution is simple
and easy to implement. I'm hoping the CS3 solution is at least as elegant.

Thanks for the response though, Patrick, I haven't seen this topic discussed
much elsewhere. Any other thoughts?
swrb1977
7/24/2007 11:51:21 PM
I think the preamble from the Flash Documentation I linked to above
(http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?
context=LiveDocs_Parts&file=00000817.html) sums it up pretty well:

"To reduce the size of a SWF file, you might want to exclude classes from
compilation but still be able to access and use them for type checking. For
example, you might want to do this if you are developing an application that
uses multiple SWF files or shared libraries, especially those that access many
of the same classes. Excluding classes helps you avoid duplicating classes in
those files."

So, for example, I have a parent swf that loads in a child swf. The child
contains references to and instances of a number of classes that happen to also
be in the parent swf. Knowing this, including those classes when I publish the
child swf would needlessly bloat the child swf. I would need to keep the
imports inplace, though, for type-checking etc.

Does that makes sense? I've done this a number of times with AS2 apps using
the instructions in the above link. Doing this in Flash CS3 for AS3 remains a
mystery.
kglad
7/25/2007 3:23:05 AM
SymTsb
7/25/2007 3:37:40 AM
Patrick B
7/25/2007 12:54:55 PM
Hi again,

If I understand correctly, you're using the child to check the parent,
is that right? In this scenario, your child isn't necessarily including
any of the parent types but since you're checking the parent for those
types you need to define them in the child. You may be able to do
something dynamically at runtime that can get around this problem but
I'm doing this off the top of my head so use with caution :)

First, import "flash.utils.*". This allows you to grab class definitions
by name at runtime. This is done like this:

try {
var classRef:*=getDefinitionByName('flash.display.MovieClip'); //for
example...can be any class
} catch (err:ReferenceError) {
trace ('MovieClip isn\'t defined');
}

(this will throw an error if you run it if it's not in the parent and
you don't do the import). You can still keep your code running though,
just make sure you do it in a try..catch..finally block otherwise the
whole execution chain stops.

Now for comparison, you simply do:

if (parentItem is classRef) {
trace ('Yup...it is a movie clip');
}

Here, classRef becomes an alias for MovieClip (getDefinitionByName does
this) so you can run a successful comparison. If it's not a MovieClip,
this will return false as expected.

Is this what you meant?

Patrick


[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Patrick B
7/25/2007 6:02:39 PM
Hi Steve,

I've been a professional developer now for six years, since Flash 4, I
can honestly say I've never heard of this. Maybe I still don't
understand what it is that you're trying to do but the way I'm picturing
it in my head, it can't possibly work. I can't think of a single feature
or export setting that would do what you describe because, if I
understand correctly, you're making hard comparisons against something
that doesn't exist. Am I getting this right? You're trying to specify
types in your child SWF without loading any of the classes to specify
those types? I mean, that's a moebius strip of failed references, do you
know what I mean. If types aren't defined at compile time, they're
evaluated dynamically at runtime. If you're not importing them, you're
using something like getDefinitionByName (not necessarily this but
something similar). The compiler has to prepare the application right
there and then...it can't compiler part of the SWF and just say that the
other half will exist somewhere. If stuff is added it's evaluated
dynamically at runtime. If it's evaluated against a type, then the type
must exist. How can Flash say thisVar==String if it has no idea what a
String is? Maybe String exists in another SWF but how can your current
SWF know that?

You know, maybe we're running in circles because we're both failing to
properly define the problem. So...What exactly is the feature you're
describing? I mean, what is it called? How would you trigger it for
older AS2/1 style exports? In the Flash documentation, how is it
referred to? What is the official term for it? If it existed at some
point, it must have a name. Without a name, it's like trying to define a
type without importing the class ;)

Patrick

[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
Patrick B
7/25/2007 6:26:50 PM
Is this what you're talking about?

https://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=rsl_124_2.html

Sorry if I'm presumptuous here...if this is not what you're talking
about then just ignore the rest of this comment. If this is it, however,
this is called an external, shared, or runtime-shared library. This
links assets between SWFs and these assets can also have code. You can
use the code in the external SWFs unless you need to define types that
are only found in those SWFs, then you need to import the associated
class in the main SWF as well. Again, this is because the pieces are
separated and each SWF must be dealt with separately.

If you're asking if this functionality is still available, the answer is
yes :)

[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
swrb1977
7/25/2007 9:30:11 PM
Hmmm... not really what I meant at all. This is a very specific feature that
exists for compiling AVM1 applications. The getDefinitionByName method is very
useful in other situations, but not really feasible for this.

This blog post (http://www.bit-101.com/blog/?p=941) describes how to do what I
need using Flex Builder 2.01.

Macromedia support folk used to lurk in these forums. Any Adobe folk out there
who want to add their 2 cents? I'd be content I could get an official word that
this feature no longer exists. At least then I could stop beating my head
against the wall.

Thanks,

--Steve
kglad
7/25/2007 10:34:23 PM
it's pretty much known as class exclusion just as swrb explained in his first post. here's some elaboration:

swrb1977
7/25/2007 10:57:59 PM
Hi Patrick,

AFAIK, it's just called class exclusion. I'm not surprised if you're not
familiar with this feature, there is no check box in the IDE to click or
anything and it's only mentioned once in the documentation. This is how it
works:

Let's say you have an FLA called foo.fla and let's say that foo.fla uses a
class called "com.foo.ColorTween". You can tell Flash to publish this FLA
without the ColorTween class by creating an XML file called "foo_exclude.xml"
and placing it in the same directory as the FLA. The contents of
foo_exclude.xml looks like this:

<excludeAssets>
<asset name="com.foo.ColorTween" />
</excludeAssets>

When you publish the FLA, Flash will look for this XML file. If it find it, it
excludes the classes listed ("com.foo.ColorTween").

Now, if you try to run this SWF, the player will freak out when it tries to
use this class. However, if you loaded this SWF into a parent SWF that already
contained that class, it would be able to run fine.

This is described pretty well in this link
(http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?
context=LiveDocs_Parts&file=00000817.html). I think Colin Moock also discusses
the topic in Essential ActionScript 2.

In terms of what I am trying to do specifically, I need to export some library
items as a swc, but not include the base classes. I won't go too much further
into why... I don't want to further confuse the issue.

Hope this clarifies things a bit.
swrb1977
7/25/2007 11:28:21 PM
Patrick B
7/26/2007 11:49:54 AM
No, I have never heard of this. I've only ever used dynamic techniques
to evaluate types. If I need to use functions in classes, I include the
classes. If they're included in a separate SWF, I call them there. If
it's a matter of suppressing warnings, I go back to step one. If I need
local instance, I go dynamic.

In all honesty, I can't say I can see why this would ever be necessary.
I mean, I completely understand excluding code out of a SWF when you're
not using it or if it's supplied by a parent SWF. I use this technique
every day to slim down code. But:

1. If I need to use ColorTween, I dynamically evaluate it at runtime so
that I don't need to include it in my child SWF (this is actually better
since I can do something else if the parent doesn't have it for some
reason).

2. Use the generic type *. Whatever's in the parent, use it. Depends on
how important the declaration is.

3. Dynamically determine the type at runtime so that if the parent has
it, I can check to see if it is a ColorTransform and, if so, use it.

The first step allows me to make my own instances, regardless of which
SWF I need to use them in. The second step allows me to reference
existing instances, regardless of which SWF they're in. The third step
allows me to check if, either in step 1 or step 2, the type is correct.
I don't think even think there's a fourth option in this list, is there?

That being said, the only time I could ever see myself needing a class
exclusion is if I simply said something to the effect of:

public var myVar:ColorTransform;

....in foo.fla. But that, to me, seems very dependent and kind of
pointless. If you happen to run this SWF in a parent that doesn't have
ColorTransform...well, that's the end of that, no?. I mean, whatever is
depending on ColorTransform is basically out of luck and you get a
run-time error, is that right?. Using dynamic evaluation, the parent may
or may not have it and your code can continue to run and not toss it's
cookies every time it needs to use a ColorTransform instance. Also,
ColorTransform can be used from another SWF, and not be hard-bound to a
parent instance (pre-existing condition). The only thing is, you simply
don't specify that myVar has to be a ColorTransform, you determine that
at runtime.

If I understand this correctly, you may as well include the
ColorTransorm class in foo.fla because if you're code is depending on it
(using the above declaration), and you need to load the parent to use
it...what's the difference from including it? If it's not required, why
declare it or declare a strong type?

Maybe you can provide me with another example of how this is intended to
work. Like I said, I understand the concept but I fail to see the
practical use. I guess this may explain why I've never heard of
this...the situation where this would be required has simply never come up.

Patrick

[quoted text, click to view]
Patrick B
7/26/2007 2:42:44 PM
That's too bad. Well, if you need a workaround in the meantime, I'll be
happy to help :)

Patrick

[quoted text, click to view]

--
http://www.baynewmedia.com
Faster, easier, better...ActionScript development taken to new heights.
Download the BNMAPI today. You'll wonder how you ever did without it!
swrb1977
7/26/2007 6:40:03 PM
Hey Patrick,

Thanks for your interest.

I've just finished speaking with an Adobe support engineer regarding this
issue and I just thought I'd update this thread. It seems, at present, the
exclusion functionality available when publishing AS2 does not exist when
publishing AS3. The engineer suggested that additional functionality might be
added in an update to the software, and would be reflected in the support
documentation.

This doesn't surprise me too much as this functionality did not work in the
original Flex 2 SDK or FlexBuilder 2.0, but became available in the 2.01 update
to FlexBuilder. I will keep my fingers crossed for the next point update to
Flash.

I think this is as official as an answer as can be expected.
AddThis Social Bookmark Button