Alessandro Zago
Alessandro Zago

Reputation: 803

AS3 class : access of undefined method / property errors

I have this class :

package  {

    import flash.net.SharedObject;
    import flash.display.DisplayObject;

    public class gestioneMultilingua 
    {
        public static function settaLingua(lingua:String):void
        {
            var dati:SharedObject = SharedObject.getLocal("datiApp", "/");
            dati.data.lingua = lingua;
            dati.flush();
        }

        public static function applicaFiltriLingua():void 
        {
            var dati:SharedObject = SharedObject.getLocal("datiApp", "/");
            var lingua : String = dati.data.lingua;

            for(var i:int = 0; i<numChildren; i++){
                var e:Object = getChildAt(i);

                if(e.name.indexOf("$"+lingua) >= 0){
                    e.visible = true;
                }
                else if(e.name.indexOf("$")  >= 0){
                    e.visible = false;
                }
            }
        }
    }
}

but I got these errors :

How can I fix them ? maybe I need to import a package ?

Upvotes: 0

Views: 98

Answers (3)

akmozo
akmozo

Reputation: 9839

You can use a static function without any problem when you use it correctly of course, the problem here is not that the function is static (and not using numChildren or calling getChildAt() in that static function) but it is about how you did all that.

In your case, you want to work with a DisplayObjectContainer's children, so you can simply pass that object to your applicaFiltriLingua() function like this :

public static function applicaFiltriLingua(container:DisplayObjectContainer):void 
{
    var dati:SharedObject = SharedObject.getLocal("datiApp","/");
    var lingua:String = dati.data.lingua;

    for (var i:int = 0; i < container.numChildren; i++) 
    {
        var e:DisplayObject = container.getChildAt(i);
        if (e.name.indexOf("$" + lingua) >= 0) {
            e.visible = true;
        } else if (e.name.indexOf("$")  >= 0) {
            e.visible = false;
        }
    }
}

then you can call your function without any problem, like this, for example :

import gestioneMultilingua;

gestioneMultilingua.applicaFiltriLingua(this);

Hope that can help.

Upvotes: 1

Vasil Gerginski
Vasil Gerginski

Reputation: 587

Since your function is a STATIC you can never instantiate one and non-static fields of all types are not allowed. Instance constructors are also not allowed and the class is automatically sealed.

Make method non-static and instantiate the class "gestioneMultilingua"

var multilingua:gestioneMultilingua = new gestioneMultilingua();

than you can access it like that multilingua.applicaFiltriLingua(//reference to stage) and do

public function applicaFiltriLingua(stage_ref:MovieClip):void
{
    var dati:SharedObject = SharedObject.getLocal("datiApp", "/");
    var lingua : String = dati.data.lingua;

    for(var i:int = 0; i<numChildren; i++){
        var e:Object = stage_ref.getChildAt(i);
        if(e.name.indexOf("$"+lingua) >= 0){
            e.visible = true;
        }
        else if(e.name.indexOf("$")  >= 0){
            e.visible = false;
        }
    }
}

Upvotes: 1

BotMaster
BotMaster

Reputation: 2223

A static method can only make calls to another static method. numChildren and getChildAt are not static methods and calling them from inside a static method can only produce errors. Even if your class was extending a DisplayObjectContainer it will still get those errors since instances of that class will have getChildAt method and numChildren property but at static level those would still not exist and would still produce errors.

Do not make your applicaFiltriLingua method static and extend a valid DisplayObjectContainer

Upvotes: 1

Related Questions