Actionscript base class in Flex AIR app

Posted by Alan on Stack Overflow See other posts from Stack Overflow or by Alan
Published on 2010-03-30T03:54:17Z Indexed on 2010/03/30 4:03 UTC
Read the original article Hit count: 275

I'm trying to build a Flex AIR app using Flex Builder 3, which I'm just getting started with.

In Flash CS4, there's a text field in the authoring environment where you can specify a class that will become the "base" class - your class inherits from Sprite and then "becomes" the Stage at runtime. Is there a a way to do the same thing with Flex/AIR? Failing that, can anyone explain how to create and use an external class?

Originally I had this in TestApp.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script source="TestApp.as"/>
</mx:WindowedApplication>

And this in TestApp.as:

package {
    public class TestApp {
        public function TestApp() {
            trace('Hello World');
        }
    }
}

That gives the error "packages cannot be nested", so I tried taking out the package statement:

public class TestApp {
    public function TestApp() {
        trace('Hello World');
    }
}

That gives an error "classes cannot be nested", so I finally gave up and tried to take out the class altogether, figuring I'd try to start with a bunch of functions instead:

function init() {
    trace('Hello World');
}

But that gives the error "A file found in a source-path must have an externally visible definition. If a definition in the file is meant to be externally visible, please put the definition in a package".

I can't win! When I put my class in a package, it says I can't do that because it would be nested. When I don't, it says it needs to be in a package so it can be seen.

Does anyone know how to fix this? If I can't do the custom-class-as-base-class thing, is there a way I could just have it like:

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script source="TestApp.as"/>
    <mx:Script>
        var app = new TestApp();
    </mx:Script>
</mx:WindowedApplication>

At the moment I can't import the class definition at all, so even that won't work.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about actionscript