Results 1 to 5 of 5
Thread: Adding a Shape inside a Sprite
-
October 23rd, 2009, 01:56 PM #163Registered User
postsAdding a Shape inside a Sprite
Hello!
I am trying to place a rectangle Shape inside a square Sprite, as its child. But, the child rectangle show up outside the sprite, instead.
Also, the click handler event of the sprite reports the error "The supplied DisplayObject must be a child of the caller".
Any suggestion, please!Code:package { import flash.display.Sprite; import flash.display.Shape; import flash.text.TextField; import flash.events.MouseEvent; public class Shapes extends Sprite { var square: Sprite = new Sprite(); var rect: Shape = new Shape(); public function Shapes() { square.graphics.lineStyle(4, 0xFF66CC); square.graphics.beginFill(0x990066); square.graphics.drawRect(250, 35, 175, 175); square.graphics.endFill(); addChild(square); rect.graphics.lineStyle(2, 0xFF6600); rect.graphics.beginFill(0xFFCC00, 1); rect.graphics.drawRect(25, 75, 150, 100); rect.graphics.endFill(); square.addChild(rect); square.addEventListener(MouseEvent.CLICK, clickHandler); } function clickHandler(ev: MouseEvent): void { removeChild(rect); // it reports error. } } }
-
October 23rd, 2009, 02:20 PM #2125Registered User
postsYou added the rect to the square. It should be "square.removeChild(rect);", or you could do "rect.parent.removeChild(rect)". Since the event listener was added to the square, you could also do "evt.currentTarget.removeChild(rect)"
-
October 23rd, 2009, 02:25 PM #363Registered User
postsYes, it was one mistake. But, still I am logically expecting the child rectangle should fall inside the square sprite. Isn't it? Please clearify my this confusion, as well.
Thanks and regards.
-
October 23rd, 2009, 02:32 PM #4125Registered User
postsIt does fall inside the square Sprite, just not the square graphics because you draw the square at (250,35) and you draw the rect at (27,75). The square Sprite is at 0,0 with a graphic drawn at 250,35.
If you change rect.graphics.drawRect(25, 75, 150, 100); to rect.graphics.drawRect(250, 75, 150, 100);, is that closer to what you were hoping to see? Or did I misunderstand completely?
-
October 23rd, 2009, 02:47 PM #563Registered User
postsYour are quite right. It is mid-night and probably, I am too sleepy.
Thank you very much and have a nice time.
Regards

Reply With Quote

Bookmarks