Resizing external loaded image relative to the stage size

Hi there,

I’m a not very experienced actionscript code writer, but i know how most things work.

but i can’t seem to make one thing work i really need.
i want to have an background image on a no-scale flash file. (so the menu’s and all keep the same size while you can resize your browser window etc.

this is my code: all works how it is supposed to i only want to resize the images (background1 and background2)



this.addEventListener(Event.ENTER_FRAME,alles);
this.addEventListener(Event.ENTER_FRAME,follow);

bgmenu.btn_bg1.addEventListener(MouseEvent.CLICK, bg1);
bgmenu.btn_bg2.addEventListener(MouseEvent.CLICK, bg2);
bgmenu.btn_bg3.addEventListener(MouseEvent.CLICK, bg3);
bgmenu.btn_bg4.addEventListener(MouseEvent.CLICK, bg4);
bgmenu.btn_bg5.addEventListener(MouseEvent.CLICK, bg5);
klrbtnmnu.addEventListener(MouseEvent.ROLL_OVER, klrbtnmnuover);
klrbtnmnu.addEventListener(MouseEvent.ROLL_OUT, klrbtnmnuout);

klrbtnmnu.q.ubruin75.addEventListener(MouseEvent.CLICK, ubruin75click);
klrbtnmnu.q.ubruin85.addEventListener(MouseEvent.CLICK, ubruin85click);
klrbtnmnu.q.ugrijs75.addEventListener(MouseEvent.CLICK, ugrijs75click);
klrbtnmnu.q.ugrijs85.addEventListener(MouseEvent.CLICK, ugrijs85click);
klrbtnmnu.q.ugroen85.addEventListener(MouseEvent.CLICK, ugroen85click);
klrbtnmnu.q.sbruin75.addEventListener(MouseEvent.CLICK, sbruin75click);
klrbtnmnu.q.sbruin85.addEventListener(MouseEvent.CLICK, sbruin85click);
klrbtnmnu.q.sgroen85.addEventListener(MouseEvent.CLICK, sgroen85click);
klrbtnmnu.q.sbernstein65.addEventListener(MouseEvent.CLICK, sbernstein65click);
klrbtnmnu.q.sorange40.addEventListener(MouseEvent.CLICK, sorange40click);
klrbtnmnu.q.sdrive75.addEventListener(MouseEvent.CLICK, sdrive75click);
klrbtnmnu.q.ddbruin.addEventListener(MouseEvent.CLICK, ddbruinclick);
klrbtnmnu.q.ddgrijs.addEventListener(MouseEvent.CLICK, ddgrijsclick);
klrbtnmnu.q.ddlagune.addEventListener(MouseEvent.CLICK, ddlaguneclick);
klrbtnmnu.q.ddocean.addEventListener(MouseEvent.CLICK, ddoceanclick);
klrbtnmnu.q.fsmoke.addEventListener(MouseEvent.CLICK, fsmokeclick);
klrbtnmnu.q.fazur.addEventListener(MouseEvent.CLICK, fazurclick);
klrbtnmnu.q.fterra.addEventListener(MouseEvent.CLICK, fterraclick);
klrbtnmnu.q.fblackcamel.addEventListener(MouseEvent.CLICK, fblackcamelclick);
klrbtnmnu.q.fblackblue.addEventListener(MouseEvent.CLICK, fblackblueclick);
klrbtnmnu.q.pgroen.addEventListener(MouseEvent.CLICK, pgroenclick);
klrbtnmnu.q.pgrijs.addEventListener(MouseEvent.CLICK, pgrijsclick);
klrbtnmnu.q.pbruin.addEventListener(MouseEvent.CLICK, pbruinclick);
klrbtnmnu.q.f2geel.addEventListener(MouseEvent.CLICK, f2geelclick);
klrbtnmnu.q.f2oranje15.addEventListener(MouseEvent.CLICK, f2oranje15click);
klrbtnmnu.q.f2rood20.addEventListener(MouseEvent.CLICK, f2rood20click);
klrbtnmnu.q.f2blauw65.addEventListener(MouseEvent.CLICK, f2blauw65click);
klrbtnmnu.q.f2blauw15.addEventListener(MouseEvent.CLICK, f2blauw15click);


//variabelen

var glasKleur:String = "ubruin75"
var backGround:String = "bg1"
var glasNaam:String = "Uni-kleuren Bruin 75%"
var oudeGlasKleur:String = "null"
var oudeBackGround:String = "null"
var imageLoader:Loader;



//main functie, hier hoeft niks meer aan verandert te worden

function loadImage(path:String):Loader{
    var request:URLRequest = new URLRequest(path);
    var loader:Loader = new Loader();
    loader.load(request);
    return loader;
};

function alles (evt:Event){

    
    bgmenu.y=stage.stageHeight-78;
    klrbtnmnu.x=stage.stageWidth-200;
    klrbtnmnu.y=stage.stageHeight-450;
    
    leftLine.height=stage.stageHeight;
    topLine.width=stage.stageWidth;
    rightLine.height=stage.stageHeight;
    rightLine.x=stage.stageWidth-1;
    bottomLine.width=stage.stageWidth;
    bottomLine.y=stage.stageHeight-1;
    if (glasKleur != oudeGlasKleur || backGround != oudeBackGround){
        var image3 = loadImage("images/"+backGround+"/"+backGround+"normal.jpg");
        var image2 = loadImage("images/"+backGround+"/"+backGround+"zon.jpg");
        image2.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus);
        function onProgressStatus(e:ProgressEvent) {   
            // this is where progress will be monitored     
            var bytestotal = e.bytesTotal;
            var bytesloaded = e.bytesLoaded;
            var pctLoaded:int = bytesloaded * 100 / bytestotal;
            //trace(pctLoaded);
            loaderImage.x=100;
            loaderText.x=80;
            loaderImage.scaleX = pctLoaded/250;
            //Display the % loaded in textfield
            loaderText.text = String(pctLoaded) + "%";
            if(pctLoaded == 100){
                loaderImage.x=-3000;
                loaderText.x=-3000;
            }
            if(pctLoaded == 0){
                loaderText.x=-3000;
            }
        }
        image3.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus2);
        function onProgressStatus2(e:ProgressEvent) {   
            // this is where progress will be monitored     
            var bytestotal2 = e.bytesTotal;
            var bytesloaded2 = e.bytesLoaded;
            var pctLoaded2:int = bytesloaded2 * 100 / bytestotal2;
            //trace(pctLoaded);
            loaderImage2.x=100;
            loaderText2.x=80;
            loaderImage2.scaleX = pctLoaded2/250;
            //Display the % loaded in textfield
            loaderText2.text = String(pctLoaded2) + "%";
            if(pctLoaded2 == 100){
                loaderImage2.x=-3000;
                loaderText2.x=-3000;
            }
            if(pctLoaded2 == 0){
                loaderText2.x=-3000;
            }
        }
        witrand.klrdescription.text = glasNaam;
        if (glasKleur == "ubruin75" || glasKleur == "ubruin85" || glasKleur == "ugrijs75" || glasKleur == "ugrijs85" || glasKleur == "ugroen85" || glasKleur == "sbernstein65" || glasKleur == "sbruin75" || glasKleur == "sbruin85" || glasKleur == "sdrive75" || glasKleur == "sgroen85" || glasKleur == "sorange40" || glasKleur == "pgrijs" || glasKleur == "pgroen" || glasKleur == "pbruin" || glasKleur == "f2geel" || glasKleur == "f2oranje15" || glasKleur == "f2rood20" || glasKleur == "f2blauw65" || glasKleur == "f2blauw15"){ 
            var image1 = loadImage("images/"+backGround+"/"+backGround+glasKleur+".jpg");
            image1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressStatus3);
            function onProgressStatus3(e:ProgressEvent) {   
                // this is where progress will be monitored     
                var bytestotal3 = e.bytesTotal;
                var bytesloaded3 = e.bytesLoaded;
                var pctLoaded3:int = bytesloaded3 * 100 / bytestotal3;
                //trace(pctLoaded);
                loaderImage3.x=100;
                loaderText3.x=80;
                loaderImage3.scaleX = pctLoaded3/250;
                //Display the % loaded in textfield
                loaderText3.text = String(pctLoaded3) + "%";
                if(pctLoaded3 == 100){
                    loaderImage3.x=-3000;
                    loaderText3.x=-3000;
                }
                if(pctLoaded3 == 0){
                    loaderText3.x=-3000;
                }
            }
            oudeGlasKleur = glasKleur;
            oudeBackGround = backGround;
            background2.addChild(image1);
            background1.addChild(image2);
            viewport.cacheAsBitmap = true;
            background1.cacheAsBitmap = true;
            background2.cacheAsBitmap = true;
            background2.mask = viewport;
            viewport.gotoAndStop(2);
            overlay.gotoAndStop(1);
            
        } else if (glasKleur == "dbruin" || glasKleur == "dgrijs" || glasKleur == "dlagune" || glasKleur == "docean" || glasKleur == "fsmoke" || glasKleur == "fazur" || glasKleur == "fterra" || glasKleur == "fblackcamel" || glasKleur == "fblackblue"){
            oudeGlasKleur = glasKleur;
            oudeBackGround = backGround;
            viewport.gotoAndStop(2);
            overlay.gotoAndStop(glasKleur);
            background2.mask = viewport;
            background2.addChild(image3);
            background1.addChild(image2);
        } else {
            background1.gotoAndStop("error");
            background2.gotoAndStop("empty");
            overlay.gotoAndStop(1);
            viewport.gotoAndStop(1);
            witrand.gotoAndStop(2);
        }
    }
}

var photoAspectRatio = 0;
this.addEventListener(Event.ENTER_FRAME,sizeListener);
function sizeListener (evt:Event){
    photoAspectRatio = background1.width/background1.height;
    trace(photoAspectRatio);
    if (photoAspectRatio != 0){
        //Get the new dimensions of the stage:
        var stageWidth:Number = Math.round(stage.stageWidth);
        var stageHeight:Number = Math.round(stage.stageHeight);
            
         var stageAspectRatio = stage.stageWidth/stage.stageHeight;
         
         //trace(stageAspectRatio);
        if (stageAspectRatio > photoAspectRatio ) {
             background1.width = stage.stageWidth;
             background1.height = background1.width/photoAspectRatio;
        } else {
             background1.height = stage.stageHeight;
             background1.width = background1.height*photoAspectRatio;
        }
    }
}

//volg functie, hoeft ook niks meer aan verandert te worden.

function follow (evt:Event){
    viewport.x = mouseX-150;
    viewport.y = mouseY-150;
    overlay.x = mouseX-150;
    overlay.y = mouseY-150;
    witrand.x = mouseX-150;
    witrand.y = mouseY-150;
    if(mouseX<350 && mouseY>stage.stageHeight-105){
        Mouse.show();
    } else if(mouseY>stage.stageHeight-305 && mouseX>stage.stageWidth-235) {
        Mouse.show();
    } else {
        Mouse.hide();
    }
}

function klrbtnmnuover (e:MouseEvent){
    Mouse.show();
    if(e.target.currentFrame <=12 && e.target.currentFrame >=1){
        if(e.target.currentFrame != 12){
            klrbtnmnu.gotoAndPlay(e.target.currentFrame + 12);
        }else{
            klrbtnmnu.gotoAndPlay("over");
        }
    }
}

function klrbtnmnuout (e:MouseEvent){
    if(e.target.currentFrame >= 13){
        if(e.target.currentFrame != 24){
            klrbtnmnu.gotoAndPlay(24 - e.target.currentFrame);
        }
        else{
            klrbtnmnu.gotoAndPlay("out");
        }
    }
}

function bg1 (e:MouseEvent){backGround = "bg1"}
function bg2 (e:MouseEvent){backGround = "bg2"}
function bg3 (e:MouseEvent){backGround = "bg3"}
function bg4 (e:MouseEvent){backGround = "bg4"}
function bg5 (e:MouseEvent){backGround = "bg5"}

function ubruin75click (e:MouseEvent){glasKleur = "ubruin75"; glasNaam = "Uni-kleuren Bruin 75%";}
function ubruin85click (e:MouseEvent){glasKleur = "ubruin85"; glasNaam = "Uni-kleuren Bruin 85%";}
function ugrijs75click (e:MouseEvent){glasKleur = "ugrijs75"; glasNaam = "Uni-kleuren Grijs 75%";}
function ugrijs85click (e:MouseEvent){glasKleur = "ugrijs85"; glasNaam = "Uni-kleuren Grijs 85%";}
function ugroen85click (e:MouseEvent){glasKleur = "ugroen85"; glasNaam = "Uni-kleuren Groen 85%";}
function sbruin75click (e:MouseEvent){glasKleur = "sbruin75"; glasNaam = "SunContrast Bruin 75%";}
function sbruin85click (e:MouseEvent){glasKleur = "sbruin85"; glasNaam = "SunContrast Bruin 85%";}
function sgroen85click (e:MouseEvent){glasKleur = "sgroen85"; glasNaam = "SunContrast Groen 85%";}
function sbernstein65click (e:MouseEvent){glasKleur = "sbernstein65"; glasNaam = "SunContrast Bernstein 65%";}
function sorange40click (e:MouseEvent){glasKleur = "sorange40"; glasNaam = "SunContrast Orange 40%";}
function sdrive75click (e:MouseEvent){glasKleur = "sdrive75"; glasNaam = "SunContrast Drive 75%";}
function ddbruinclick (e:MouseEvent){glasKleur = "dbruin"; glasNaam = "Dégradé Bruin 10% / 75%";}
function ddgrijsclick (e:MouseEvent){glasKleur = "dgrijs"; glasNaam = "Dégradé Grijs 10% / 75%";}
function ddlaguneclick (e:MouseEvent){glasKleur = "dlagune"; glasNaam = "Dégradé Lagune";}
function ddoceanclick (e:MouseEvent){glasKleur = "docean"; glasNaam = "Dégradé Ocean";}
function fsmokeclick (e:MouseEvent){glasKleur = "fsmoke"; glasNaam = "Fashion Smoke";}
function fazurclick (e:MouseEvent){glasKleur = "fazur"; glasNaam = "Fashion Azur";}
function fterraclick (e:MouseEvent){glasKleur = "fterra"; glasNaam = "Fashion Terra";}
function fblackcamelclick (e:MouseEvent){glasKleur = "fblackcamel"; glasNaam = "Fashion Black-Camel";}
function fblackblueclick (e:MouseEvent){glasKleur = "fblackblue"; glasNaam = "Fashion Black-Blue";}
function pgrijsclick (e:MouseEvent){glasKleur = "pgrijs"; glasNaam = "Polariserend Grijs";}
function pgroenclick (e:MouseEvent){glasKleur = "pgroen"; glasNaam = "Polariserend Groen";}
function pbruinclick (e:MouseEvent){glasKleur = "pbruin"; glasNaam = "Polariserend Bruin";}
function f2geelclick (e:MouseEvent){glasKleur = "f2geel"; glasNaam = "Funkleuren Geel 15%";}
function f2oranje15click (e:MouseEvent){glasKleur = "f2oranje15"; glasNaam = "Funkleuren Oranje 15%";}
function f2rood20click (e:MouseEvent){glasKleur = "f2rood20"; glasNaam = "Funkleuren Rood 20%";}
function f2blauw65click (e:MouseEvent){glasKleur = "f2blauw65"; glasNaam = "Funkleuren Blauw 65%";}
function f2blauw15click (e:MouseEvent){glasKleur = "f2blauw15"; glasNaam = "Funkleuren Blauw 15%";}


this is the part of the code that doesn’t really work how it’s supposed to work:

var photoAspectRatio = 0;
this.addEventListener(Event.ENTER_FRAME,sizeListener);
function sizeListener (evt:Event){
    photoAspectRatio = background1.width/background1.height;
    trace(photoAspectRatio);
    if (photoAspectRatio != 0){
        //Get the new dimensions of the stage:
        var stageWidth:Number = Math.round(stage.stageWidth);
        var stageHeight:Number = Math.round(stage.stageHeight);
            
         var stageAspectRatio = stage.stageWidth/stage.stageHeight;
         
         //trace(stageAspectRatio);
        if (stageAspectRatio > photoAspectRatio ) {
             background1.width = stage.stageWidth;
             background1.height = background1.width/photoAspectRatio;
        } else {
             background1.height = stage.stageHeight;
             background1.width = background1.height*photoAspectRatio;
        }
    }
}

for some reason when i try to trace photoAspectRatio, it starts with 0 and after four traces it gives me NaN.

i can’t figure it out, spend hours online searching but nothing worked.

PS: the PHP tags make it easier to read right?