Posted by: Tushar | June 25, 2007

Action Script 3: Resize SWF on Browser resize

 

Accessing Stage object was pretty simple in previous versions of Action Script. However, it’s little different in Action Script 3. Sprits (movie clip) have property named stage (notice small “s”). One can use this property to access the Stage Object and respond to the Stage object’s Resize event.

Here is the AS3 code to resize SWF depending on browser size:

Class myStage:

package {

import flash.display.Stage;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

public class myStage extends Sprite {

private var _mc:Sprite;
private var mcStage:Stage;

public function myStage(_mc:Sprite){
this._mc = _mc;
mcStage = _mc.stage;
addStageListener();
}

public function addStageListener() {
mcStage.scaleMode = StageScaleMode.NO_SCALE;
mcStage.align = StageAlign.TOP_LEFT;
mcStage.addEventListener(Event.RESIZE, resizeHandler);
}

private function resizeHandler(event:Event):void {
trace("stageWidth: "+mcStage.stageWidth);
trace("stageHeight: "+mcStage.stageHeight);
_mc.x = mcStage.stageWidth/2;
_mc.y = mcStage.stageHeight/2;
}
}
}

Now, create object of myStage and pass movieclip reference (which you want to align at center of stage) as parameter:

var mystage:myStage = new myStage(_mc);

Publish the flash file. Be sure to put 100% as value for Height and Width parameters in Object and param tag in HTML.

All set! Now you can test your file. SWF should now respond as you resize the browser window.


Responses

  1. Hello,

    Nice script, wondering if it was possible to add a action script to a swf file telling it to getURL then re-sizing that opened html to a smaller size. I’m trying to accomplish something similiar to this..

    For example:

    getURL(“http://somepopup.com”, “resizable=1,HEIGHT=200,WIDTH=200″);

    Would you happen to know any that is capable of creating this action? I appreciate any resposces

  2. if you are targetting ActionScript3, simplest way would be:

    var jsCmd:String = “window.open(‘http://somepopup.com’,'win’,'height=200,width=200′);”;
    var url:URLRequest = new URLRequest(“javascript:” + jsCmd + ” void(0);”);
    navigateToURL(url, “_self”);

  3. Thanks for the example, but forgive me I’m having a really difficult time getting it to run at all. I keep getting errors? Do you have any source files? I’d really appreciate it. This jump from AS2 to AS3 has been very difficult. Thanks.

  4. hi – sorry just learning & having trouble getting this to work.. I created myStage.as file and created a new fla where I added “var mystage:myStage = new myStage(_mc:sample.swf);” .

    But I get a syntax error “expecting rightparen before colon”.

    What am I doing wrong?

  5. Thanks, this was super-helpful.

  6. [...] Action Script 3: Resize SWF on Browser resize « Tushar Wadekar Accessing Stage object was pretty simple in previous versions of Action Script. However, it’s little different in Action Script 3. Sprits (movie clip) have property named stage (notice small “s”). One can use this property to access the Stage Object and respond to the Stage object’s Resize event. [...]

  7. Hello. Thank you for providing this resource. I was hoping that you could clarify your conclusive instructions:

    “Now, create object of myStage and pass movieclip reference (which you want to align at center of stage) as parameter:

    var mystage:myStage = new myStage(_mc);”

    I am unclear as to what you mean by ‘object of myStage’ and how to ‘pass movieclip reference.’

    I am new enough to AS that I am missing a developed enough vocabulary to properly understand. I would much appreciate it if you could bridge that gap. Thank you.

  8. This Is So Confusing LOL Well ! Got It To Work

    1. Add Movie To Stage Give it a Instance Ex.”asb”
    2. Give it Class Name(above)
    3. set Property On Stage Ex. “asb.myStage(this)”

    Worked !

    Still don’t Understand ?
    var mystage:myStage = new myStage(_mc);”
    Oh Well !

  9. modified to work better:

    package {

    import flash.display.Stage;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

    public class myStage extends Sprite {

    private var _mc:Sprite;
    private var _stage:Stage;

    public function myStage(mc:Sprite){
    this._mc = mc;
    _stage = _mc.stage;
    init();
    listeners();
    resizeHandler(new Event(Event.RESIZE));
    }

    private function init() {
    _stage.scaleMode = StageScaleMode.NO_SCALE;
    _stage.align = StageAlign.TOP_LEFT;
    }
    private function listeners() {
    _stage.addEventListener(Event.RESIZE, resizeHandler);
    _mc.addEventListener(Event.RESIZE, resizeHandler);
    }
    private function resizeHandler(event:Event):void {
    _mc.x = (_stage.stageWidth/2) – (_mc.width/2);
    _mc.y = (_stage.stageHeight/2) – (_mc.height/2);
    }
    }
    }

  10. Hey guys, I’m completely new at this.
    So, I have a flash file that I want to have resize automatically as the user extends their browser so it fits automatically.

    I saw the code… but holy crp… I’m dumb.

    Anychance on getting a step by step version on how to do this for someone like ‘I’.. that is not savvy at all with this stuff.

    please oh please… thnks guys.

    rje

  11. How come this code brings me so many Syntax errors? I found 7 Syntax errors in the output panel after using this script.. WTF?

  12. I was getting errors , there may be a problem if you copy the code directly into flash as the quotes are incorrect, just remove the two lines that say trace(whatever).

  13. Hi,

    I’m just beginning to learn Actionscript. I’m putting together a gallery. What if I want the image to resize, but not the text description? Any advice or resources to look up? Thanks.

    hsf

  14. Hey there, So the funny thing, is:
    1. the trace statements are causing errors, so I had to /// the trace at the bottome to make this work.

    2. this works for some of the website, but not all of it.

    I have the code placed before my movieclips of:
    content (holding the menu and other content)
    menuMC (holding the menu)
    contentMC (holding the content)

    I am trying to center everything and it isnt working right.

    here it is, this website means nothing right now, so bare with the “crap”

    http://www.hungateagency.com/fonts/site2.swf

    as you can see, the background and the site layout labels are centering, but the menu is not shrinking to the full effect. Very weird.

    I also tried copying SIMON’s version and it completely made the menu dissapear…

  15. exelente, simple y claro como me gusta, muchas gracias (Thanks)

  16. Great code thx!…

    How can i get it to adjust to fit the browser when it loads instead of only after i resize the browser.

    thx,
    A

  17. nevermind… i got it to work

    I added a public function to the myStage class

    public function resizeHandlerInit() {—-resizing—}

    which handled all the resizing. Then I called that function from the same frame as the new stage object in the fla

    mystage.resizeHandlerInit();

  18. I want resize my external swf for included in my xml menu. for exaple:

    ****

    ————————-
    (**** : ı want resize “main.swf” )

    i is rith ?????
    pLZ. HELP ME. tANKS!!!!

  19. how about doing this EXACT thing in as2?????? swf resizes with users browser

  20. I built a re-sizable window with as3 and flash that resizes inside the flash movie. It can be viewed and downloaded at http://www.actionscript-flash-guru.com/blog/16-code-for-a-resizable-window-in-as3 I put a lot of work into this little script and I thought it would be a shame not to make it available to others. Thanks for the rocking post man.

  21. This script gives me an error:

    ArgumentError: Error #1063: Incongruenza nel conteggio degli argomenti su myStage(). Era atteso 1, è stato ricevuto 0.

    ???


Leave a response

Your response:

Categories