<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hubflanger &#187; stageHeight</title>
	<atom:link href="http://hubflanger.com/tag/stageheight/feed/" rel="self" type="application/rss+xml" />
	<link>http://hubflanger.com</link>
	<description>adventures in code</description>
	<lastBuildDate>Mon, 29 Mar 2010 22:41:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<atom:link rel='hub' href='http://hubflanger.com/?pushpress=hub'/>
		<item>
		<title>Stage RESIZE and the stageWidth and stageHeight Bug</title>
		<link>http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties/</link>
		<comments>http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 16:53:56 +0000</pubDate>
		<dc:creator>Peng</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Flash Player]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[RESIZE]]></category>
		<category><![CDATA[Safari]]></category>
		<category><![CDATA[stage]]></category>
		<category><![CDATA[stageHeight]]></category>
		<category><![CDATA[stageWidth]]></category>

		<guid isPermaLink="false">http://hubflanger.com/using-the-stagewidth-and-stageheight-properties-2/</guid>
		<description><![CDATA[A while ago, I came across a situation where my Flash application would launch but the stage.stageWidth and stage.stageHeight properties would always return 0 within my document class&#8217;s constructor. As I needed to access those properties at runtime and I did not want to hardcode those values to a pair of variables, it drove me [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I came across a situation where my Flash application would launch but the <code>stage.stageWidth</code> and <code>stage.stageHeight</code> properties would always return 0 within my document class&#8217;s constructor. As I needed to access those properties at runtime and I did not want to hardcode those values to a pair of variables, it drove me to seek an answer to this problem.</p>
<p>Turns out that when the <code>stage</code> is initialized, its <code>stageWidth</code> and <code>stageHeight</code> properties are 0 until a <code>Stage RESIZE</code> event is triggered. This event is dispatched when the <code>scaleMode</code> property of the <code>Stage</code> object is set to <code>StageScaleMode.NO_SCALE</code> and the SWF file is resized ie. when the stage is rendered by the Flash Player. So if you need to add a display object onto the <code>stage</code> and its positioning is dependent on the <code>stageWidth</code> or <code>stageHeight</code> properties at runtime, it&#8217;s best handled by a listener that is subscribed to the <code>Stage RESIZE</code> event.<span id="more-102"></span></p>
<p>Interestingly, despite universal claim that the Flash Player behaves exactly the same on all browsers, this problem only appears in a couple of browser/plugin configurations, namely FireFox/OSX and IE/Win. And strangely, on Safari and Firefox/Win, the <code>RESIZE</code> event is not even dispatched on page load, and neither does the Flash Player register the <code>stageWidth</code> and <code>stageHeight</code> properties as being 0. I have not tested the problem on other platforms but you are certainly welcome to do so yourselves and report your observations in the comments section. Eventually, it would be nice if we can get a straight answer from Adobe concerning this bug.</p>
<p>In the following demo code, I&#8217;ve attached a <code>Sprite</code> containing a <code>Rectangle</code> graphic and a <code>TextField</code>, to the <code>stage</code> once before the <code>RESIZE</code> event and once, after. You can see the results <a href="/demos/stage_resize/">here</a>.</p>
<div class="codeblock">
<pre>
package
{
  import flash.text.*;
  import flash.display.*;
  import flash.events.*;

  public class Demo extends Sprite
  {
    private var rect1:Sprite;
    private var rect2:Sprite;

    public function Demo()
    {
      rect1 = createNewRect("Before stage resize event.");
      rect1.x = 0;
      rect1.y = 0;
      addChild(rect1);

      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      stage.addEventListener(Event.RESIZE, resizeHandler);
    }

    private function createNewRect(str:String):Sprite
    {
      var rect:Sprite = new Sprite();
      rect.graphics.beginFill(0x99CCFF);
      rect.graphics.drawRect(0, 0, 200, 100);
      rect.graphics.endFill();

      var fmt:TextFormat = new TextFormat();
      fmt.font = "Arial";
      fmt.size = 12;

      var tf:TextField = new TextField();
      tf.width = 200;
      tf.height = 100;
      tf.multiline = true;
      tf.defaultTextFormat = fmt;
      tf.text = str + "\n" + "stage.stageWidth: " + stage.stageWidth + "\n" +
            "stage.stageHeight: " + stage.stageHeight;

      rect.addChild(tf);
      return rect;
    }

    private function resizeHandler( event:Event ):void
    {
      if (rect2 != null &#038;&#038; contains(rect2)) {
        removeChild(rect2);
      }

      rect2 = createNewRect("After stage resize event.");
      rect2.x = (stage.stageWidth - rect2.width) / 2;
      rect2.y = (stage.stageHeight - rect2.height) / 2;
      addChild(rect2);
    }
  }
}
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
