Ultrashock Forums > Flash > ActionScript
Full Flash Sites 101 - Resizing

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rating: Thread Rating: 2 votes, 5.00 average. Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Full Flash Sites 101 - Resizing
Old 2009-02-13



Despite Ultrashock having one of the most comprehensive threads regarding full-screen Flash sites, I still see a lot of people asking how to handle things when the browser is resized. Seeing as the existing full-screen thread is MASSIVE and people no doubt get bored looking through all of the pages to find the info they need I thought it would be a good idea to get the essential bits 'n' pieces together here, and include demo files.

ActionScript 2.0 and ActionScript 3.0 obviously require slightly different approaches so I will cover those separately, the HTML setup is the same though regardless of the ActionScript version you use so I will start with that.


HTML

Things are very easy when it comes to the HTML, all that you need to do is make sure the <html> <body> and <object> elements all have a 100% width and height, and also clear the <body> element's margin and padding. To make this example as simple as possible I have thrown everything into a single HTML page but I would normally keep the CSS in its own file.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>

    <title>demo</title>

    <style type="text/css">
        html, body, object
        {
            width:100%;
            height:100%;
            display:block;
        }
        html
        {
            /* hides the browser's scrollbars */
            overflow:none;
        }
        body
        {
            margin:0;
            padding:0;
        }
    </style>

</head><body>

    <object type="application/x-shockwave-flash" data="demo.swf">
        <param name="movie" value="demo.swf"/>
    </object>

</body></html>
How you add the Flash object to the page is up to you, most people these days use JavaScript code in one form or another, the important part is the CSS.

ActionScript 2.0

The following code is the basic setup you will need for ActionScript 2.0.

Code:
//
// STAGE SETUP
//

Stage.align = "TL";
Stage.scaleMode = "noScale";

//
// STAGE RESIZE LISTENER
//

var stageListener:Object = {
    onResize: resizeHandler
};

Stage.addListener( stageListener );

//
// STAGE RESIZE HANDLER
//

function resizeHandler():Void
{
    var w:Number = Stage.width;
    var h:Number = Stage.height;
}

//
// INITIALIZE
//

resizeHandler()
OK. That is all pretty simple, you first setup the Stage so it won't scale the SWF ( it will instead change the canvas size ), you then add a listener to the Stage that will tell Flash which function needs to be called when the SWF is resized, finally, you define that function. You must also remember to call the function manually as soon as possible otherwise nothing will be resized properly until the SWF is first resized.

You use the resizeHandler() function to resize and/or reposition all of your MovieClips etc, the stageWidth and stageHeight values will represent the current width and height of the SWF file, the rest is basic math ( as seen in the downloadable source files ).


ActionScript 3.0

The following code is the basic setup you will need for ActionScript 3.0.

Code:
//
// STAGE SETUP
//

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

//
// STAGE RESIZE LISTENER
//

stage.addEventListener( Event.RESIZE, resizeHandler );

//
// STAGE RESIZE HANDLER
//

function resizeHandler( e:Event=null ):void
{
    var w:Number = stage.stageWidth;
    var h:Number = stage.stageHeight;
}

//
// INITIALIZE
//

resizeHandler()
Again, simple stuff. I don't think I really need to explain what is going on in that code because the functionality is exactly the same as the ActionScript 2.0 version ( see above ).

Loading Runtime Contents

One thing that seems to trip a few people up is the loading of images etc at runtime, normally a question along the lines of "why isn't my image aligning properly?" appears around the forums. The thing to remember in this situation is this: The contents of the SWF are being resized and/or repositioned by the resizeHandler() function, and that function has no idea when a new image or new SWF file has been loaded. So, as soon as the image or SWF has loaded you should manually call the resizeHandler() function, that will allow you resize and/or reposition the loaded image or SWF ( or start a tween etc ).

Demo Files

The following ZIP file contains demos for ActionScript 2.0 and ActionScript 3.0.

FSF-Demos.zip

For convenience the code can be found in the first frame of the FLA files. You will need Flash CS3 to open the FLA files.

+

Good luck Ultrashockers!
postbit arrow 9 comments | 3350 views postbit arrow Reply: with Quote   
Coffee Monster
Nutrox is offline Super Moderator
seperator
Posts: 12,121
2004-04-25
Age: 32
Nutrox lives in United Kingdom
17
Nutrox's Avatar
seperator

Ultrashock Member Comments:
Tlew Tlew is offline Tlew lives in Canada 2009-02-14 #2 Old  
Hey Nutrox, I haven't looked at it fully but i thought your mono demos had everything needed to pull off full screen flash sites without the need of the swf object.....asking just to make sure.
Reply With Quote  
blasthaus blasthaus is offline blasthaus lives in United States 2009-04-09 #3 Old  
Hey thanks for posting this, very helpful, however the AS3 version doesn't seem to work for me on my Mac, any thoughts why? that's so strange. I'm updated on plugins and browsers, and the code should work. puzzled...
Reply With Quote  
ngodoc's Avatar ngodoc ngodoc is offline ngodoc lives in Viet Nam 2009-11-10 #4 Old  
Thank you very much about this post. This problem i'm finding. and now, i found it ) ha ha.

@blasthaus: this file is coding by FLASH 9, CS 3 can read it.
Reply With Quote  
bioskoop bioskoop is offline bioskoop lives in South Africa 2009-11-16 #5 Old  
Excellent, thanks. Just one problem...the zip file cannot be found. Oops, would really have like to have the examples for myself to check out.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2009-11-17 #6 Old  
The ZIP file is available again.
Reply With Quote  
bioskoop bioskoop is offline bioskoop lives in South Africa 2009-11-26 #7 Old  
Dude, you have no idea how gratefull I am for this. Its helped me so much.
Thank you nutrox, you have helped me get out of the rut ive been stuck in.
Cheers.
Reply With Quote  
Gramophone Gramophone is offline 2010-01-09 #8 Old  
Thanks for this... I've got a question. What if I want SquareCC to resize? What am I supposed to do?
I've seen this, and I've always wanted to do it, but I don't know how.
Reply With Quote  
HelgaValerie HelgaValerie is offline HelgaValerie lives in United States 2010-01-11 #9 Old  
thanks much for posting. just what I neeeded
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2010-01-11 #10 Old  
Quote: Originally Posted by Gramophone View Post
Thanks for this... I've got a question. What if I want SquareCC to resize? What am I supposed to do?
It depends how you want the shape to resize really, you could do something like this:

Code:
function resizeHandler( e:Event=null ):void {
    var w:Number = stage.stageWidth;
    var h:Number = stage.stageHeight;

    // resize 'shape' so it is always a fixed distance from the stage boundary
    var distance:Number = 40;
    shape.x = distance;
    shape.y = distance;
    shape.width  = w - ( distance * 2 );
    shape.height = h - ( distance * 2 );
}
You could also get creative with masks and resize those instead of the things they are masking.
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: