Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-23-2009, 03:23 PM   #1
Raabi
Member
 
Join Date: Sep 2009
Posts: 57
Default Adding a Shape inside a Rectangle

Hello!

Experimenting with adding a square shape inside a rectangle, in the following code, the compiler bounced the error:
"Call to a possibly undefined method addChild through a reference with static type flash.display:Shape".

Code:
package
{
      import flash.display.Sprite; 
      import flash.display.Shape; 
	
      public class Shapes extends Sprite
     {
          public function Shapes()
         { 
             var rect: Shape = new Shape(); 
             rect.graphics.lineStyle(2, 0xFF6600);
             rect.graphics.beginFill(0xFFCC00, 1);
             rect.graphics.drawRect(25, 75, 175, 75);
             rect.graphics.endFill();
             addChild(rect); 
			
             var square: Shape = new Shape();
             square.graphics.lineStyle(4, 0xFF66CC);
             square.graphics.beginFill(0x990066);
             square.graphics.drawRect(250, 35, 75, 75);
             square.graphics.endFill();
          //the following statement cause the error.
            rect.addChild(square); 
        }
   }
}
It simply smashed my faith that addChild() method applies to every type of shape. Would anybody kindly rectify my mistake, please!
Raabi is offline   Reply With Quote
Old 10-23-2009, 03:25 PM   #2
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

ActionScript Code:
var rect: Sprite = new Sprite();
Shape are not capable of having nested DisplayObjects.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 10-23-2009, 03:59 PM   #3
Raabi
Member
 
Join Date: Sep 2009
Posts: 57
Default

Thanks ASWC for your attention and the working tip. However, I want to avail this opportunity to clear some of my confustions:
  • Is Shape Not a Container? If it is, then why is this restriction?
  • Why many AS3 features seem challenging the common sense. Do such restrictions have to memorized; or there are some rules clearly defined for such situations.

I hope, my confusions will be addressed; because I feel quite frustrated when for every different situation, I have to revert to the forum.
Raabi is offline   Reply With Quote
Old 10-23-2009, 04:10 PM   #4
Raabi
Member
 
Join Date: Sep 2009
Posts: 57
Default

Further to my previous query, Sprite also produces the same error, as with Shape.
Raabi is offline   Reply With Quote
Old 10-23-2009, 04:50 PM   #5
ASWC
Super Moderator
 
ASWC's Avatar
 
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528
Default

Shape is a very basic DisplayObject and cannot contain other DisplayObjects. It has also a very limited interactive capability. It is meant to provide a simple canvas to draw into while saving resources.
Sprite can contains other DisplayObjects and is fully interactive.
Loader can contain other DisplayObjects and is fully interactive and you can also load external pictures/swfs into it.
Finally MovieClip can contain other DisplayObject, is fully interactive and has also a timeline.
You shouldn't get the same error when using Sprite so if you still have the error then something's wrong with your code.
__________________
aswebcreations
Super Duper!
ASWC is offline   Reply With Quote
Old 10-23-2009, 04:57 PM   #6
Raabi
Member
 
Join Date: Sep 2009
Posts: 57
Default

Thanks again for the explanation. Let me look into the code to find the bug.

Have a good time.
Raabi is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:36 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.