AddChild relative positioning

5 posts

Flag Post


I have 2 classes the first of which is my document class that creates a stage sprite that I add an instance of my second class to. My second class is a sprite that contains a shape and a textfield. I am trying to add the textfield as a child of the shape and I can get it to display, but its position is set relative to my stage sprite, which it is a grandchild of, not my menuPanel instance, which it is the child of. According to my searching and the tutorial I am working off of, positioning of addChild is supposed to be relative to the parent. Is this not correct?

Code below:

package  {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	public class Game extends MovieClip {

		private var iPanel:ExpositionPanel = new ExpositionPanel();
		private var iStage:Sprite = new Sprite();
		public function Game(){
			trace("Program started");
		public function initialize():void{
			trace("Program initializing...");
			trace("Creating iStage Sprite...");;, 0, 700,700);
			iStage.buttonMode = true;
			trace("Adding Stage sprite...");
			trace("adding Exposition Panel...");
			trace("Adding mouse event listener...");
		private function clicked(evt:Event):void{
			trace("Click registered");


package  {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	public class ExpositionPanel extends Sprite {
		private var panelText:TextField = new TextField();
		private var menuPanel:Sprite = new Sprite();

		public function ExpositionPanel() {
			panelText.text = "Initialized";
			panelText.x = 20;
			panelText.y = 20;


Flag Post
Originally posted by Pixel_Chaos:


positioning of addChild is supposed to be relative to the parent. Is this not correct?

addChild is not positioning anything, it’s just adding a child to a parent.

The position is determined by the x and y coordinates of a DisplayObject, which exist independently from being displayed or not.
If it has a parent (got addChild’ed) these coordinates will be interpreted to be in the coordinate system of the parent, yes.

x and y are initially 0

You stack multiple DisplayObject(-Containers): panelText → menuPanel → iPanel → iStage

Non of them is transformed in any way (rotated, translated, scaled), therefore in addition to the assumptions above, all coordinate systems of those containers (menuPanel, iPanel, iStage) are identical.

You do not need to create a Sprite to use the graphics API (menuPanel).
Your class ExpositionPanel extends Sprite and thus has its own Graphics object that you can use.

Flag Post

iStage appears to have coordinates of 0,0
menuPanelappears to have coordinates of 0,0

So the position will be the same regardless of which is panelTexts’ parent.

Am I missing something?

And yes, you are correct, children position from parent. But parent positions from its’ parent, and so on, so things can stack and get complex.

Flag Post

Thank you to all that responded. The problem was just as listed, I didn’t give the parent any coords so the child spawned at 0,0. My problem was a misunderstanding of the way drawRect() works. I had thought that since the arguments define it’s location that they were its actual position coordinates. I will be reading the Reference docs before my next question. Thanks again!

Flag Post
Originally posted by Pixel_Chaos:

I will be reading the Reference docs before my next question.

Halleluiah! A new programmer who knows how to RTFM. :)