Skip to main content
Known Participant
May 20, 2008
Question

Nested Shapes vs. Nested Sprites in AS 3.0

  • May 20, 2008
  • 2 replies
  • 504 views
Nested Shapes vs. Nested Sprites in AS 3.0

Hello,
I'm trying to learn Actionscript 3.0 from the ground up. I am trying to figure out why I can't create nested shape objects in the manner that's demonstrated below w/ Sprite objects.

var container:Shape = new Shape();


When I change the below from a Sprite to a Shape I get this error:

"Call to a possibly undefined method addChild through a reference with static type flash.display:Shape."


var container:Sprite = new Sprite();
container.graphics.lineStyle(0,0xFF6600);
container.graphics.beginFill(0xFF3300);
container.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
container.graphics.lineTo(100, 0); // move to left 65 x and 0 y
container.graphics.lineTo(100, 100); // from 65 x to 20 y
container.graphics.lineTo(0,100); // 0 x to 20 y

this.addChild(container);
container.x = 140;
container.y = 140;


var box_1:Sprite = new Sprite();
box_1.graphics.lineStyle(0,0xFFFFCC);
box_1.graphics.beginFill(0xFFFF00);
box_1.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
box_1.graphics.lineTo(80, 0); // move to left 65 x and 0 y
box_1.graphics.lineTo(80, 80); // from 65 x to 20 y
box_1.graphics.lineTo(0,80); // 0 x to 20 y

container.addChild(box_1);
box_1.x = 10;
box_1.y = 10;

var box_2:Sprite = new Sprite();
box_2.graphics.lineStyle(0,0x92E9FE);
box_2.graphics.beginFill(0x92E9FE);
box_2.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
box_2.graphics.lineTo(60, 0); // move to left 65 x and 0 y
box_2.graphics.lineTo(60, 60); // from 65 x to 20 y
box_2.graphics.lineTo(0,60); // 0 x to 20 y

box_1.addChild(box_2);
box_2.x = 10;
box_2.y = 10;

Thanks,
Eric
This topic has been closed for replies.

2 replies

Ned Murphy
Legend
May 20, 2008
The preceding response is correct and useful info to absorb. Shapes do not have the addChild method associated with them. So you could create nested shapes, but they would have to be nested within the Sprite as shown in the following variation of the code:

var container:Sprite = new Sprite();
this.addChild(container);
container.x = 140;
container.y = 140;

var box_0:Shape = new Shape();
box_0.graphics.lineStyle(0,0xFF6600);
box_0.graphics.beginFill(0xFF3300);
box_0.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
box_0.graphics.lineTo(100, 0); // move to left 65 x and 0 y
box_0.graphics.lineTo(100, 100); // from 65 x to 20 y
box_0.graphics.lineTo(0,100); // 0 x to 20 y

container.addChild(box_0);

var box_1:Shape = new Shape();
box_1.graphics.lineStyle(0,0xFFFFCC);
box_1.graphics.beginFill(0xFFFF00);
box_1.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
box_1.graphics.lineTo(80, 0); // move to left 65 x and 0 y
box_1.graphics.lineTo(80, 80); // from 65 x to 20 y
box_1.graphics.lineTo(0,80); // 0 x to 20 y

container.addChild(box_1);
box_1.x = 10;
box_1.y = 10;

var box_2:Shape = new Shape();
box_2.graphics.lineStyle(0,0x92E9FE);
box_2.graphics.beginFill(0x92E9FE);
box_2.graphics.moveTo(0, 0); // start line @ 10._x and 0 Y
box_2.graphics.lineTo(60, 0); // move to left 65 x and 0 y
box_2.graphics.lineTo(60, 60); // from 65 x to 20 y
box_2.graphics.lineTo(0,60); // 0 x to 20 y

container.addChild(box_2);
box_2.x = 20;
box_2.y = 20;
May 20, 2008
A sprite is an "interactive" object - which means it can have children and be interactive (handle mouse events/text events, etc...). A shape is just a drawn display-object... which means its just displayable, no interactivity is included in a shape.

The shape extends the object DisplayObject and Sprite extends the object InteractiveObject. The good news is they share a lot of the same attributes, so if you made a shape now - and 1 month down the line, you realize it needs to be interactive, you could probably just change its datatype to a Sprite :)