I’m having a problem with using addChild in my program, the document class goes as such:
public function gridtest()
{
turn++;
var plusX = 1;
var plusY = 0;
trace ("game started");
var i;
var j;
for (i = 0; i < 5; i++)
{
trace ("first for loop");
for (j = 0; j < 8; j++)
{
gridbutton = new GridButton();
gridbutton.x += plusX;
gridbutton.y += plusY;
gridbutton.GBID = (i*10)+ j;
trace ("created button at " + plusX + "," + plusY);
addChild(gridbutton);
GBList.push(gridbutton);
trace("grid button number " + i + j + " added");
plusX += 92.5;
}
plusY += 61.5;
plusX = 1;
}
}
I have one addChild in the main function gridtest(), which works fine, but the other one in the second function here (still the same document):
static function NewUnit(locX, locY)
{
unit = new Unit(locX, locY);
unit.Set_ID(UnitList.length);
addChild(unit);
UnitList.push(Unit);
}
gives me the error 1180:Call to a possibly undefined method addChild, does it have something to do with the function being static? Or is it something else entirely.
addChild is a non-static function. Interaction between static and non-static functions is unavailable. you should refer to an instance of Object first and call the function from that instance. Please check
trace(this);in static function.If you want object addChild. should add an additional parameter for the parent you want to add to.