简体   繁体   中英

How to access bgcolor from swfobject?

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

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, eg 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 (eg 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, eg when there are HTML elements behind it that need to be visible.

//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM