gregoryb
gregoryb

Reputation: 81

AS3 code with an interface has suddenly started giving "Call to a possibly undefined method"

GameWorld.as, Line 96 1180: Call to a possibly undefined method initialize.

I am adding a controller to my GameWorld that implements IController:

addController(new BackgroundController(this));

public function addController(controller:IController):void
{
    controller.initialize();
    controllers.push(controller);
}

public interface IController 
{
    function initialize():void;         //setup the controller
    function getType():String;          //define the controller by a type string
    function update():void;             //perform update actions
    function destroy():void;            //cleanup the controller
}

initialize is a method from IController but is now undefined suddenly

I am getting no syntax errors and cant seem to revert my code to a working state.

What could be causing this?

Here is the BackgroundController:

package controller 
{
    import Entity;
    import flash.display.Bitmap;
    import flash.display.Sprite;

    public class BackgroundController implements IController
    {
        private var world:GameWorld;
        private var images:Vector.<Bitmap>;
        private var bgImage:Sprite;

        public function BackgroundController(world:GameWorld)
        {
            this.world = world;
        }

        public function initialize():void
        {
            bgImage = new Sprite();
            images = new Vector.<Bitmap>();

            var ypos:int = 0;

            for (var i:int = 0; i < 3; i++ )
            {
                var tempBmp:Bitmap = new Bitmap(new grasstile(0, 0));
                images.push(tempBmp);
                bgImage.addChild(tempBmp);
                tempBmp.y = ypos;
                ypos += 500;
            }

            GameWorld.lowerLayer.addChild(bgImage);
        }
        public function update():void 
        {
            //update the background tiles
            for (var i:int = 0; i < 3; i++ )
            {
                images[i].y -= world.gameSpeed;
                if (images[i].y < -500 )
                {
                    images[i].y += 1500;
                }
            }
        }

        public function getType():String
        {
            return "Background";
        }

        public function destroy():void 
        {

        }

    }

}

Upvotes: 1

Views: 467

Answers (3)

azrafe7
azrafe7

Reputation: 2736

Found myself in the same situation.

Discovered that the problem was due to a name-clashing issue between the package and the defined variable.

So, in your case, just changing

public function addController(controller:IController):void
{
    controller.initialize();
    controllers.push(controller);
}

to

public function addController(controllerImpl:IController):void
{
    controllerImpl.initialize();
    controllers.push(controllerImpl);
}

should have solved it.

Upvotes: 1

Mark Knol
Mark Knol

Reputation: 10143

Some global checks

  • Are you using runtime shared assets, multiple files? Make sure you've build them all.
  • Make sure there are no other runtime/build errors
  • In FDT (which editor do you use?) there is a feature called 'reset the MXML compiler and force full build'. That clears the cache and forces to do a complete new build instead of an incremental build.
  • In Flashdevelop you have to use tools > flash tools > rebuild class path
  • In the Flash IDE you could clear the ASO files(CS5-) / clear publish cache (CS6).
  • Restart/kill the editors + related processes to make sure there are no weird cache conflicts and all syntax checking is up to date.

Code checks

// make sure it has implemented the IController
trace("controller is IController: "  + (controller is IController) );

and..

// detect what kind of class it really is. Goto that class, check the interface.
trace("controller is : "  + getQualifiedClassName(controller) );

Also make sure there are no other IController interfaces, or check all the import statements, so your sure everywhere the right interface is used.

Upvotes: 1

shanethehat
shanethehat

Reputation: 15570

An interface just defines rules for a class, in this case stating that any class implementing IController must contain definitions for those four methods. Do you actually have an initialize method defined in your controller class?

Upvotes: 0

Related Questions