momijigari
momijigari

Reputation: 1598

Error: Call to a possibly undefined method

I have the simplest code ever. Main class:

package 
{
import field.Field;
import flash.display.Sprite;
import flash.events.Event;

public class Main extends Sprite 
{

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        var field:Field = new Field();
        addChild(field);
        field.test();
    }
}
}

and a Field class:

package field 
{
import flash.display.Sprite;

public class Field extends Sprite 
{

    public function Field() 
    {
        super();

    }

    public function test():void
    {

    }

}
}

test method is presented. But when I try to compile I get this:
Main.as(26): col: 10 Error: Call to a possibly undefined method test. field.test();

How could this be happening?

Upvotes: 1

Views: 990

Answers (1)

akmozo
akmozo

Reputation: 9839

field is your package, that's why you can not do field.test(). So you have to choose another name of your Field instance. You can do like this :

var _field:Field = new Field();
addChild(_field);
_field.test();

Hope that can help.

Upvotes: 2

Related Questions