Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 5 of 5

Thread: Adding a Shape inside a Sprite

  1. #1
    63
    posts
    Registered User

    Adding 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".
    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.
      }
     }
    }
    Any suggestion, please!

  2. #2
    125
    posts
    Registered User
    You 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)"

  3. #3
    63
    posts
    Registered User
    Yes, 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.

  4. #4
    125
    posts
    Registered User
    It 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?

  5. #5
    63
    posts
    Registered User
    Your are quite right. It is mid-night and probably, I am too sleepy.

    Thank you very much and have a nice time.

    Regards

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012