views:

113

answers:

2

I'm trying to get my main class to resize to the browser window size. I'm listening on the stage to Event.RESIZE, updating my width/height to match the stageWidth/stageHeight, and drawing a rectangle to show me how big it is.

When I resize, it flashes between a small and big size every other time the event fires. The width and height are correct in both cases, but in the "small" case, everything is in a small box.

Big Size

Small Size

Here's my code

package
{
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.text.TextField;

 public class main extends Sprite
 {
  public function main()
  {
   stage.align = StageAlign.TOP_LEFT;
   stage.scaleMode = StageScaleMode.NO_SCALE;

   stage.addEventListener(Event.RESIZE, onResize);

   var text:TextField = new TextField();
    text.text = "Some Text";

   addChild(text);
  }

  private function onResize(event:Event):void
  {
   this.width = stage.stageWidth;
   this.height = stage.stageHeight;

   trace("RESIZE " + width + " " + height);

   this.graphics.clear();
   this.graphics.beginFill(0x000000, 0.5);
   this.graphics.drawRect(0, 0, this.width, this.height);
  }
 }
}

What's the right way to do this? What am I doing wrong? Thanks!

+1  A: 

It looks like your Sprite is doing the right thing, but the TextField is the thing that's causing the problems. So, if you update the width and height on that as well it works:

package
{
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import flash.text.TextField;

 public class asProj extends Sprite
 {
  private var text:TextField;

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

   stage.addEventListener(Event.RESIZE, onResize);
   text = new TextField();
   text.text = "Some Text";

   addChild(text);
  }

  private function onResize(event:Event):void
  {
   this.width = stage.stageWidth;
   this.height = stage.stageHeight;

   text.width = this.width;
   text.height = this.height;

   trace("RESIZE " + this.width + " " + this.width);

   this.graphics.clear();
   this.graphics.beginFill(0x000000, 0.5);
   this.graphics.drawRect(0, 0, this.width, this.height);
  }
 }
}
99miles
Awesome... but it doesn't make any sense. Setting the width and height of the text field keeps it SMALL (which is what I wanted, but why does it work?). Also, it's clearly doing something strange. If I make the window smaller quickly, the textfield seems to shrink for a moment and then get big again.
Sean Clark Hess
Well, the size of the TextField, and the font size are two different things. Using my code, the TextField is changing size, but the font size is staying the same. You can see what I mean if you give the TextField a background color:text.background = true;text.backgroundColor = 0xff6666; And i think what you are seeing when you quickly resize the window is simply a delay between the resize and when the textfield gets updated.back2dos's solution is a better idea.
99miles
+2  A: 

the problem is, that default implementation for setters of width and height is just a mere alias to scaleX and scaleY ... when you have a Sprite of width 100, and you set its width to 200, than it is simply horizontally stretched by factor 2 ... at the same time, the default getters simply return the effective width and height on screen, so if you draw to the sprite, it's width and height are updated accordingly ...

this should be perfectly working:

            private function onResize(event:Event):void
            {
                    this.graphics.clear();
                    this.graphics.beginFill(0x000000, 0.5);
                    this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
            }
back2dos
That makes sense. It's been a long time since I've worked in flash (flex guy here). Width and Height are really pretty useless without some extra help. Thanks!
Sean Clark Hess
I ended up adding a variable to store explicitWidth and explicitHeight. I then update that variable when width and height are changed, and have the graphics draw themselves based on the variable, not the "actual" width/height.
Sean Clark Hess