A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: loading external jpg's into swf at random...

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    107

    loading external jpg's into swf at random...

    hi all...
    I would like to use a high res photo as a background of an swf which is full page. I would like to have multiple backgrounds but have the swf call for the jpg's at random so its a different pic each time a visitor enters. Im assuming calling the jpgs externally would be best AND i want the movie to automatically scale the high res photo to the size of the flash movie.
    what would the code be for this?
    any suggestions and help is appreciated!
    Thanks
    and Happy New Year!
    -A

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    PHP Code:
    Array.prototype.getRandomElement = function(){
        return 
    this[Math.round(Math.random()*(this.length-1))];
    };

    var 
    images = ["horse.jpg","cat.jpg","mouse.jpg"];

    var 
    currentimage images.getRandomImage();

    var 
    holderclip this.createEmptyMovieClip(0,0);

    var 
    loader = new MovieClipLoader();
    loader.onLoadInit = function(target){
    target._width Stage.width;
    target._height Stage.height;
    };
    loader.loadClip(currentimage,holderclip); 

  3. #3
    Senior Member
    Join Date
    Feb 2001
    Posts
    107

    Thank you!

    Thank you so much for your response :-)

    big help!!!
    -A

  4. #4
    World Kit Vote Holder Abelius's Avatar
    Join Date
    Feb 2002
    Location
    US
    Posts
    963
    Actually, there is a line to be added there:

    Array.prototype.getRandomElement = function()
    {
    return this[Math.round(Math.random() * (this.length - 1))];
    };

    var images = ["horse.jpg", "cat.jpg", "mouse.jpg"];

    var currentimage = images.getRandomElement(); // <--- name needs to match with initial function name

    var holderclip = this.createEmptyMovieClip(0, 0);

    var loader = new MovieClipLoader();

    loader.onLoadInit = function(target)
    {
    target._width = Stage.width;
    target._height = Stage.height;
    };

    loader.addListener(this); // <-------------- this is the line to be added

    loader.loadClip(currentimage,holderclip);

    Cordially,
    Abelius
    www.worldkit.com

  5. #5
    Senior Member
    Join Date
    Feb 2001
    Posts
    107

    now heres a question...

    ok, heres a question about random pictures....
    What I have is a transparent swf and behind that I want to load random high res backgrounds. I have a code to resize the image (in ratio) along with the size of the browser.
    Below is the code I have so far. How can I have this load random background images?

    Code:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>test</title>
    <script language="javascript">AC_FL_RunContent = 0;</script>
    <script src="AC_RunActiveContent.js" language="javascript"></script>
        <title>test</title>
    <style type="text/css">
    body {
    margin:0px;
    background-color:#;
    }
    
    
    img.bg {position:absolute;
    left:0px;
    top:0px;
    z-index:0
    }
    #image { position:absolute; top:0px; left:0px; width:100%;
    height:100%; z-index:1; } 
    #flash { position:absolute; top:0px; left:0px; width:100%;
    height:100%; z-index:2; } 
    </style></head>
    <body>
    
    <div id="flash"><OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" WIDTH="100%" HEIGHT="100%" CODEBASE="http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0">
    <PARAM NAME="MOVIE" VALUE="index.swf">
    <PARAM NAME="PLAY" VALUE="true">
    <PARAM NAME="LOOP" VALUE="true">
    <PARAM NAME="WMODE" VALUE="transparent">
    <PARAM NAME="QUALITY" VALUE="high">
    <EMBED SRC="index.swf" WIDTH="100%" HEIGHT="100%" PLAY="true" LOOP="true" WMODE="transparent" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
    </EMBED>
    </OBJECT>
    </div>
    
    <script type="text/javascript">
    function resizeImg() {
    
    var mybg = document.getElementById('mybgimg');
    var winW = (window.innerWidth) ? window.innerWidth : document.body.offsetWidth;
    var winH = (window.innerHeight) ? window.innerHeight : document.body.offsetHeight;
    var newImgH = 0;
    var newImgW = 0;
    var imgW = mybg.getAttribute('width');
    var imgH = mybg.getAttribute('height');
    var winR = winW/winH
    var oldRatio = imgW/imgH;
    var newRatio;
    
    
    if(winR <= oldRatio)
    {
    newImgH = winH;
    newImgW = Math.round(winH * (imgW/imgH));
    }
    else
    {
    newImgH = Math.round(winW * (imgH/imgW));
    newImgW = winW;
    }
    newRatio = newImgW/newImgH;
    
    // these document.write is what I used to see what was going on and it helped
    // figure out the math for the if else statement above.
    //document.write("width = " + winW + " and height = " + winH + "<br />");
    //document.write("pic width = " + imgW + " and height = " + imgH + "<br />");
    //document.write("new pic width = " + newImgW + " and height =" + newImgH + "<br />");
    //document.write("window ratio = " + (winW/winH) + "<br />");
    //document.write("Old Pic ratio = " + oldRatio + "<br />");
    //document.write("New Pic ratio = " + newRatio + "<br />");
    
    
    //mybg.style.height = null; don't need these next three lines after all
    //mybg.style.width = null;
    
    //(winH <= winW) ? mybg.style.height = winH + 'px' : mybg.style.width = winW + 'px';
    
    mybg.setAttribute('width',newImgW);
    mybg.setAttribute('height',newImgH);
    }
    
    window.onload = resizeImg;
    window.onresize = resizeImg;
    </script>
    <p>
    
    
    <div id=image><img class="bg" border="0" src="bg1.jpg" id="mybgimg" width="1024" height="682"></div>
    
    
    </span>
    
    
    </body>
    </html>

  6. #6
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    Quote Originally Posted by Abelius
    Actually, there is a line to be added there:

    Array.prototype.getRandomElement = function()
    {
    return this[Math.round(Math.random() * (this.length - 1))];
    };

    var images = ["horse.jpg", "cat.jpg", "mouse.jpg"];

    var currentimage = images.getRandomElement(); // <--- name needs to match with initial function name

    var holderclip = this.createEmptyMovieClip(0, 0);

    var loader = new MovieClipLoader();

    loader.onLoadInit = function(target)
    {
    target._width = Stage.width;
    target._height = Stage.height;
    };

    loader.addListener(this); // <-------------- this is the line to be added

    loader.loadClip(currentimage,holderclip);

    actually that's not true. you don't need to add the listener to the moviecliploader object, but yes i did typo the method call.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center