1 Reply Latest reply on Oct 28, 2007 7:41 PM by peterent

    Setting the width of Sprite doesn't work

    Thiross
      Hi, All:
      I'm trying to set the width of a Sprite. But the result is weired.
      Here follows my test code, please look at the comment:
      package demo
      {
      import flash.display.Graphics;
      import mx.containers.Canvas;
      import mx.core.UIComponent;

      public class MainWidget extends Canvas
      {
      public function MainWidget() {
      var c:UIComponent = new UIComponent();
      c.x=c.y=0;
      c.width=100;
      c.height=100;

      var m:MySprite = new MySprite();
      c.addChild(m);
      m.x=m.y=0;
      m.height=100; // if I comment out these two lines, I'll see the green line on screen.
      m.width=100; // But when I try to set the size, there's no line on screen.
      m.draw(); // use the graphics attribute to draw a green line
      this.addChild(c);
      }
      }
      }
        • 1. Re: Setting the width of Sprite doesn't work
          peterent Level 2
          You've hit upon a very interesting thing in Flash. The "size" of basic elements like Sprite and Shape aren't set with properties. Rather, they take their dimension from their content. You've found that by setting the width and height that it doesn't work. You have to lay down some pixels to get a proper size:

          c.graphics.clear();
          c.graphics.beginFill(0xffffff);
          c.graphics.drawRect(0,0,100,100);
          c.graphics.endFill();

          Now c will have a width of 100 and a height of 100. If you drew that rectangle at (100,100,100,100) then it would have width and height of 200 as measured from 0,0.