Ok, so I’ve got the following code structure:
var environment:AvEnvironment = new AvEnvironment(stage.stageWidth, stage.stageHeight);
addChild(environment);
environment.addChild(new Player());
environment.addChild(new Terrain());
the player class passes information on its x and y to the environment class so that the environment class will center on the player object. As terrain is also a child of the parent, the terrain class will also be centered. However, I do not want this to be the case: I want the terrain class to be at the top left of the parent (environment) when the game is created.
This is the code I am trying to implement in the terrain class:
if(parent)
{
var ev:AvEnvironment = AvEnvironment(parent);
this.x = -ev.x
this.y = -ev.y
}
However, this does not work in the constructor method as parent returns null and I’m not sure why.
I’m thinking your Terrain gets initialized, calling your code above, before it gets a parent (addChild()). You can try the following in your Terrain class: