this is not the first blog that comments about this bug found in IE for windows having swf being non-scale, and being embedded using swfobject. In this site: http://hubflanger.com/stage-resize-and-the-stagewidth-and-stageheight-properties. They mentioned that the stage dimensions can be returned as 0. they recommend to target instead when the stage resizes. That is true, that works better and someone else show how they implemented this approach already in a piece of work. You would like to have reusable code to handle this issue. I have created the class that will handle the stge resize, and trigger when it’s ready… The nice thing is that this instance can be passed to other different listeners…
First we use an event class…
package com.juandevelops.flesh.stage
{
import flash.events.Event;
public class StageEvent extends Event
{
public static const classPath:String = “com.juandevelops.flesh.stage.StageEvent”;
public static const ADDED:String = classPath + “.added”;
public static const READY:String = classPath + “.ready”;
public static const RESIZE:String = classPath + “.resize”;
public static const REMOVED:String = classPath + “.removed”;
public function StageEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}
/*
we now use the StageListener class. This is the order in which this class dispatches events
1. StageEvent.ADDED … the display object has been aded to stage..
2. StageEvent.READY… the display object has dimensions available
3. StageEvent.RESIZE.. the display object’s stage is resizing.. ( we skip StageEvent.RESIZE the first time if the dimensions of stage are 0, but instead we dispatch StageEvent.READY )
4. StageEvent.REMOVED.. the display object left the stage..
*/
package com.juandevelops.flesh.stage
{
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
public class StageListener extends EventDispatcher
{
private var di:DisplayObject;
private var isReady:Boolean = false;
//Make sure you pass a reference of the display object to watch.
public function StageListener( di:DisplayObject )
{
super(this);
this.di = di;
this.di.addEventListener(Event.ADDED_TO_STAGE, this.onStageAdded );
this.di.addEventListener(Event.REMOVED_FROM_STAGE, this.onStageRemoved );
}
private function onStageAdded( event:Event ):void
{
this.di.stage.addEventListener(Event.RESIZE, this.onStageResize );
this.dispatchEvent( new StageEvent( StageEvent.ADDED ) );
if( !this.isReady )
this.testIfReady();
}
private function onStageRemoved( event:Event ):void
{
this.isReady = false;
this.dispatchEvent( new StageEvent( StageEvent.REMOVED ) );
}
private function onStageResize( event:Event ):void
{
if( !this.isReady )
{
this.testIfReady();
}
else
this.dispatchEvent( new StageEvent( StageEvent.RESIZE ) );
}
private function testIfReady():void
{
if( !this.isReady )
{
if( this.di.stage.stageHeight > 0 && this.di.stage.stageWidth > 0 )
{
this.isReady = true;
this.dispatchEvent( new StageEvent( StageEvent.READY ) );
}
}
}
public function isStageReady():Boolean
{
return this.isReady;
}
public function displayObject():DisplayObject
{
return this.di;
}
public function stage():Stage
{
return this.di.stage;
}
}
}
//here is a demo of using StageListener
package
{
import com.juandevelops.flesh.stage.StageEvent;
import com.juandevelops.flesh.stage.StageListener;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.external.ExternalInterface;
public class StageTest extends MovieClip
{
private var stageListener:StageListener;
public function StageTest()
{
this.stageListener = new StageListener( this );
this.stageListener.addEventListener( StageEvent.ADDED, this.onStageAdd ); // you wouldn’t use this handler.. but just to show you we know the item is added into the stage…
this.stageListener.addEventListener( StageEvent.READY, this.onStageReady ); // you will rely on this one though..
}
private function onStageAdd( event:StageEvent ):void
{
ExternalInterface.call( “alert”, “stage just added,, but ready is not confirmed” );
}
private function onStageReady( event:StageEvent ):void
{
//now we have a reference of stage.. we can access it and change certain things..
//those same lines at the constructor could have thrown an error if stage is null.
this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
ExternalInterface.call( “alert”, “stage as ready can be confirmed” );
}
}
}
i wrote all the code and tested. i could see in IE using swfobject, when StageEvent.ADDED happened but the dimensions were still 0, and then when StageEvent.READY is dispatched by StageListener class confirming the dimensions are found now.. As i said before having an instance of StageListener, you can pass it as a reference so another class knows when this event occurs, and in this way avoid having to rewrite the code every time.. like this
var di:MyClip = MyClip();
var listener:StageListener = new StageListener( di );
var myObject:SomeClass = new SomeClass( listener );
from there on in SomeClass you can write the code to listen for the stage-ready event, and StageListener will also let you know the display object we are triggering… This is just to give you a hint how you can do more with this class…
Thank You.
Comment by juandevelops — March 16, 2009 @ 8:26 pm |
please write a coment if this posting was helpful
Comment by juandevelops — March 26, 2009 @ 6:36 pm |
For Flex.. i needed to get the parameters passed to the swf.. this worked really great…
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” onPreinitialize=”this.onPreinitialize(event);” …….
…………..
private var listener:StageListener;
private function onPreinitialize( event:FlexEvent ):void
{
this.listener = new StageListener( this as DisplayObject );
this.listener.addEventListener( StageEvent.READY, this.onStageReady );
}
private function onStageReady( event:StageEvent ):void
{
//this class requires to read from stage the parameters passed into the swf.
var stageResizer:StageResizer = new StageResizer( this.stage );
}
Comment by juandevelops — May 14, 2009 @ 5:35 pm |