2

Flashbuilder generates a html where the bgcolor is stored(through javascript):

...  
var swfVersionStr = "10.0.0";
        <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
        var xiSwfUrlStr = "playerProductInstall.swf";
        var flashvars = {};
        var params = {};
        params.quality = "high";
        params.bgcolor = "#ff0000";
        params.allowscriptaccess = "sameDomain";
        params.allowfullscreen = "true";
...

How can I change that bgcolor DYNAMICALLY in as3? How do I access this flashvar params.bgcolor ?

Thanks

Benny
  • 2,250
  • 4
  • 26
  • 39
algro
  • 639
  • 4
  • 8
  • 25
  • What's the point? Just draw another background inside Flash. BTW: bgcolor is a param, not a flashvar, so I don't think you can access it directly. – RIAstar Aug 23 '11 at 13:42
  • It matters when I resize the browserwindow. In some cases I need a black and sometimes a white background. Drawing another background doesn't solve that. – algro Aug 23 '11 at 14:25
  • 1
    You should've put that in your question. @richarddolsson provided you with a part of the answer. Now you can use ExternalInterface to have JavaScript tell Flash which color to draw using his technique. – RIAstar Aug 23 '11 at 14:35
  • Or if the dimensions of the SWF embed is relative to the browser window size (e.g. full-browser, 100%) then you can do exactly what I'm suggesting in my answer, but add some logic to select the correct color in the Event.RESIZE event handler. – richardolsson Aug 23 '11 at 14:42

2 Answers2

3

A SWF file has a background color property embedded in it (like frame rate, default dimensions et c) but the embedding environment, which is usually HTML, can override all of these settings at embed time. Additionally, some of them can be overridden by ActionScript at runtime. However, SWF background color does not fall into this last category.

Instead, you can use the graphics API to draw a rectangle behind everything, e.g. by doing the following:

root.graphics.beginFill(0xffcc00); // Replace with your color
root.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);

If there is a risk that your SWF will resize, you should put the above two lines in an event handler and add that as a listener on the stage for Event.RESIZE, like so:

function handleStageResize(ev : Event) : void
{
    root.graphics.clear();
    root.graphics.beginFill(0xffcc00); // Replace with your color
    root.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}

// Add the listener
stage.addEventListener(Event.RESIZE, handleStageResize);

Beware also that there are some issues with certain older browsers where the main entry point of your ActionScript will be invoked before the dimensions of the stage have been properly set (meaning that stage.stageWidth and stage.stageHeight both return 0) so you might want to wait one frame before invoking the above drawing code the first time.

This method is much superior to using transparent window mode and modifying the background behind the SWF (in HTML) because using wmode=transparent can sometimes cause odd issues (e.g. keyboard input bugs in some browsers) and will reduce performance, often significantly.

Only ever use transparent wmode if you really need it to be transparent, e.g. when there are HTML elements behind it that need to be visible.

richardolsson
  • 4,041
  • 1
  • 17
  • 19
2
//as3
externalInterface.call("myFuction", "#FF0000");

//javascript 
 var myClr; 
 function myFuction(myVal)
 {
    myClr = myVal;
    window.action = actionFunc();
 }
 window.action = actionFunc();
 function actionFunc()
 {
    var flashvars = {};
    var params = {};
    var attributes = {};
    params.bgcolor = myClr;
    flashvars.mp3="mast.mp3";
    var so = new swfobject.embedSWF("player_slim.swf", "myContent", "300", "120", "9.0.0",true, flashvars, params, attributes);
    so.write("myContent");      
}

 <div id="myContent">
 </div>

try this way. its working.

Benny
  • 2,250
  • 4
  • 26
  • 39