1

Hail, Stack!

I'm having a little trouble figuring out how to import a SWC file directly in ActionScript, without setting a library path to the file.

To exemplify, I need something like this:

package
{
    [Embed(source = 'Library.swc')] // This line won't work, of course...

    import ClassInsideSWC;

    public class MyClass extends ClassInsideSWC
    {
        public function MyClass()
        {
            super();
        }
    }
}

Besides, I don't want to (I can't, in fact) import the SWC by loading it with Loader class.

Well, someone knows a way to link to the SWC using only ActionScript Code?


Edited

Just to add more info about the problem, I'll showcase my scenario with more details...

I have a class SubClass that wil be independent from the rest. It will extend a class SuperClass that is inside the SWC/SWF...

This SWC/SWF have the whole framework. I can't compile every class inside a single SWF. Every part of my framework is a SWF apart and will be downloaded by Loader class on runtime.

Sadly, @frankhermes answer won't work. That method won't download the classes and won't allow me to extend or use other classes inside the SWC.

If I setup the library path this becomes possible...

NemoStein
  • 2,088
  • 2
  • 22
  • 36

2 Answers2

3

A possible correction on Sean Fujiwara's answer:

[Embed(source="SWFWireDecompiler.swc", mimeType="application/octet-stream")]
public var SWC:Class;

might need to be:

[Embed(source="SWFWireDecompiler.swf", symbol="ClassInSWC")]
public class ClassInSWC
{
   ....
}

EDIT: as a follow-up after the comments, this is another attempt at getting an acceptable answer:

package
{
    import flash.display.Sprite;

    public class SWCTest extends Sprite
    {
        public function SWCTest()
        {
            var extended:ExtendedCrumbs = new ExtendedCrumbs();
        }
    }

}

[Embed(source="../swfs/assets.swf", symbol="assets.Crumbs")]
class CrumbsInSWC
{

}

class ExtendedCrumbs extends CrumbsInSWC
{
    public function ExtendedCrumbs()
    {
        super();
    }

    // override something here
}

Sadly it only works with swf files, not with swc's because you can't use symbol="..." inside Embed statements when using a swc. But the code above does compile. Place the swf file anywhere in your project and adjust the source path inside the Embed statement.

Hope this is still of some use to you! Frank

frankhermes
  • 4,720
  • 1
  • 22
  • 39
  • @Sean and @frank, thank you both, but I don't think that Embed tag will work outside a class definition. I can't put it inside my class because I need to extend a class that is inside the SWC... – NemoStein Mar 14 '11 at 12:27
  • This [Embed ..] and the class beneath it DOES work. And once you have that class you can extend it in another class. The .swc does need to be in your project somewhere though, at compile time. So practically putting it in your library path will be a lot easier. Why can't you do that? – frankhermes Mar 16 '11 at 19:24
  • @frankhermes - Because I'm developing a kind of framework, and the content will be created by people... Maybe stupid people... Maybe, stupid like a door! Hence, I can't teach every door to add SWCs in the library path... ps: If you can make it work, please, create a .as file and answer the question with it. I'll be glad to accept as correct if it works... – NemoStein Mar 16 '11 at 21:09
  • @nemostein: I edited my answer so that it actually works using swf files. Not swc's, I'm sorry. I still think putting a swc in a libs directory is a lot easier (especially for doors) than the solution I came up with. – frankhermes Mar 17 '11 at 19:15
  • Thank you, @frankhermes... +1 because your answer is VERY instructive and informative! Still, that won't solve the problem. I edited my question and updated my scenario. Sorry to bother you even more, I really won't to accept your answer just because it is clean, but I can't, 'cause it will confuse people who look at it. – NemoStein Mar 18 '11 at 21:13
  • I give up. I think your scenario is far more confusing than my solution. You should think of some other way of combining your classes and not stick with inheritance. – frankhermes Mar 20 '11 at 18:56
0

Why do you say "This line won't work, of course..."? You're almost right, just missing a few things.

[Embed(source="SWFWireDecompiler.swc", mimeType="application/octet-stream")]
public var SWC:Class;

Then you can turn get the data as a ByteArray:

var data:ByteArray = new SWC();
Sean Fujiwara
  • 4,506
  • 22
  • 34