4 Replies Latest reply on Dec 14, 2006 1:56 AM by RIA_Flasher

    Basic Actionscript 3.0 OOP Confusion

      I'm having a difficult time getting basic Actionscript OOP principles to work in my code. I have an application I'm building that extends the Sprite object. I try to instantiate another Sprite object, and specify the with/height/x/y values to size this new object, but it never properly sets these values. If I trace the width/height, etc. they're always equal to 0. I'm not exactly sure what I'm doing wrong.

      The 3 different sizing syntax I've tried are:
      graphics.drawRect(0,0,width,height); // DOES NOT WORK
      graphics.drawRect(0,0,parent.width,parent.height); // WORKS
      graphics.drawRect(0,0,600,400); // WORKS

      Code is posted below:

      package {
      import flash.display.Sprite;
      import flash.display.Stage;
      import flash.display.StageScaleMode;
      import flash.display.StageAlign;

      import ChartPanel;

      public class Chart extends Sprite
      private var chartType:int;
      private var chartPanel:ChartPanel;

      public function Chart()
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;

      graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight); // Fill BG as White

      chartPanel = new ChartPanel();
      chartPanel.setDimensions(600, 400, 0, 0);

      import flash.display.Sprite;

      public class ChartPanel extends Sprite
      public function ChartPanel() {}
      public function setDimensions(w:int, h:int, off_l:int, off_t:int):void
      this.width = stage.width;
      this.height = stage.height;
      this.x = off_l;
      this.y = off_t;
      public function draw():void
      trace("DRAWING: this.width=" + this.width + " this.height=" + this.height + " this.x=" + this.x + " this.y=" + this.y);
      // DOES NOT WORK graphics.drawRect(0,0,width,height);
        • 1. Re: Basic Actionscript 3.0 OOP Confusion
          buabco Level 1
          Remember that the width and height parameters of a movieclip are measures of its content, when you set this parameters, the content is sized to fit the values you've set, this normally should modify the transformation matrix of the clip itself.

          Given this logic, you can't set the with and height of an empty Sprite, since it has no content and it always be of 0 width, no matter witch transformation matrix you use.

          Once you've added some content it's a different story though.

          I've been able to change this behavior by overriding the setter and getter method of the width and height parameters. But keep in mind that playing with this parameters affect the way flash will measure your clip.
          • 2. Re: Basic Actionscript 3.0 OOP Confusion
            buabco i would be interested in the code you used to override the height and width methods.

            I would like to fix the width and height of a sprite even though there is no content in it.

            When content is added, any content that falls outside the fixed width and height should not be visible.

            I sort of got round the problem of setting the height and width of a sprite with no content by creating an invisible rectange but this is more of a workaround than a solution, and my sprite size will grow if any content breaches the boundaries of the sprite, which is not what I want.

            • 3. Re: Basic Actionscript 3.0 OOP Confusion
              buabco Level 1
              well... you still don't grab the hole width and height concept, as I told you they are MEASURES not fixed variables, so by setting a width and a height you are not defining a biding rectangle with content in ti, the Sprite class simple doesn't have the code for that, the Sprite is simply an elements with things in it, now actual border or frame.

              For what you want you'll still need your invisible rectangle, but it will have to be a MASK of the sprite class.

              As for the code (NOTE I'M JUST WRITING HERE SO IT CAN HAVE SOME BUGS) it should be something like this:

              public class Box extends Sprite {
              private var border:Shape;
              private var _width:Number = 100;
              private var _height:Number = 100;

              public function Box() {
              border = new Shape();
              mask = border;

              private function drawDispArea():void {
              border.drawRect(0, 0, width, height);

              override public function set width(v:Number):void {
              _width = v;

              override public function get width():Number {
              return _width;

              override public function set height(v:Number):void {
              _height = v;

              override public function get height():Number {
              return _height;
              • 4. Re: Basic Actionscript 3.0 OOP Confusion
                RIA_Flasher Level 1
                Thanks buabco

                I now understand the sprite size concept fully.

                I will give the code a try, thanks for taking the time to include it.