views:

39

answers:

3

Hi....

How do I call a private function from an external ActionScript3 document? I'm working in Flash Builder 4, and I need to call a private function from an external AS3 document. I think I've imported it correctly....

import myapp.utils.WebcamFaceDetector;
import myapp.utils.FaceDetector;

But I want to call a function from "FaceDetector". Here's the part of the code in FaceDetector...

public class FaceDetector
{
    private var detector    :ObjectDetector;
    private var options     :ObjectDetectorOptions;
    private var faceImage   :Loader;
    private var bmpTarget   :Bitmap;

    private var view :Sprite;
    private var faceRectContainer :Sprite;
    private var tf :TextField;

    private function FaceDetector() {
        initDetector();
    }
    //...
}

I want to call "private function FaceDetector()" to initiate at a certain point in another AS3 file. How do I properly declare it and get it to run?

Thanks, Alex

A: 

First, you cannot call a private class. The private keyword purpose is to stop external classes, including subclasses from calling the function.

Second, FaceDetector has the same name as the class. This means it is the constructor and is automatically called when you create a new instance of the class.

PS. Constructors in ActionScript 3.0 must be public

Allan
+1  A: 

The only way to access a private function it is to declare it as public or introduce an extra function and declare that as public.

The private attribute is meant to restrict access to that Class alone.

What you can do is create a protected function that subclasses your FaceDetector class and that gives you access but maybe not in the way that you want to use it.

On closer inspection you are using a private constructor (unless this is not your package) which prevents instantiation from other classes so I am not sure what you are really trying to accomplish.

If is was a normal private function (not a constructor) you could also register it to listen for events and and dispatch the event from where-ever you need it from.

The only proper way I know of to use private constructors other than utility classes are Singletons and that cannot even be done in ActionScript 3 (private constructors)

phwd
+1  A: 

From your example code, the FaceDetector function is the contructor of the FaceDetector class. This means it is called when you construct a new instance of FaceDetector e.g.

var faceDetectorInstance:FaceDetector = new FaceDetector();

your constructor should be public not private. AS3 does not support private constructors.

you should make your initDetector method public, so you can call that directly e.g.

public function initDetector():void 
{
    //Do Stuff Here...
}
TandemAdam