Reputation: 81
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
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
Reputation: 10143
Some global checks
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
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