Dynamic background color

Peter Hall posted a solution to setting the background color using Actionscript, actually drawing a rectangle on the background below any other elements on the document. Someone on the Macromedia Flash Forum re-iterated that question, …

Peter Hall posted a solution to setting the background color using Actionscript, actually drawing a rectangle on the background below any other elements on the document. Someone on the Macromedia Flash Forum re-iterated that question, so I thought some of you may still would like to have a look at a modified version.

This one will only fill the viewable stage and not the whole maximum allowable space of 5760 px of the Flash Document Scene. Code follows in the full message.

//define a var for sh and sw
//let us add 2 px each as flash eats them
//in the Stage Object from the actual flash dimension
var sh = Stage.height + 4;
var sw = Stage.width + 4;
var rp = 0;//let us have a start point
trace("stage height = " + sh + " & stage width = " + sw + " in pixels");
setBG = function (col) {
 with (_level0) {
  clear();
  if (col != undefined) {
   beginFill(col);
   moveTo(rp, rp);
   lineTo(sw, rp);
   lineTo(sw, sh);
   lineTo(rp, sh);
   endFill();
  }
 }
 this.__bg__ = col;
};
//
getBG = function () {
 return this.__bg__;
};
//
Stage.addProperty("bgColor", getBG, setBG);
delete setBG;
delete getBG;
//Usage
Stage.bgColor = 0x0000ff;


Don't like it? There are lots of published articles, pick a random one.

oCricket

Brajeshwar posted this article on Tue, Feb 18th, 2003 at 5:20 pm
Categorized under Technology and has the following tags

Prev Article: Blogging in news again

Next Article: (mt) got a new look


Possibly Related Articles

Archives: Visit the Archives for more articles.

Comments Post Yours

There are 15 responses so far. You can follow any responses to this entry through the RSS feed. You can skip to the end and leave a response. Pinging is currently not allowed.

  1. Thanks brajeshwar This should come in handy.
    I noticed a couple of problems, This fixes it(replace existing code with ):

    var sw = Stage.width + 4;

    and….

      beginFill(col);
    moveTo(rp, rp);
    lineTo(sw, rp);
    lineTo(sw, sh);
    lineTo(rp, sh);
    endFill();

  2. Updated! I did not even test the code beforehand, hhhmmmm. I will take care next time.

  3. If you change the code to the following,
    it also works when you have the Stage.scaleMode set to “NoScale”, and the Stage.align to “LT”

    // We don't want the Flash to Scale
    // but use dynamic alignment instead
    Stage.scaleMode = "noScale"
    Stage.align = "LT"
    
    var rp = 0;//let us have a start point
    
    setBG = function (col) {
     with (_level0) {
      clear();
      if (col != undefined) {
       beginFill(col);
       moveTo(rp, rp);
       //let us add 2 px each as flash eats them
       //in the Stage Object from the actual flash dimension
       lineTo(Stage.width + 4, rp);
       lineTo(Stage.width + 4, Stage.height +4);
       lineTo(rp, Stage.height +4);
       endFill();
    
      }
     }
     this.__bg__ = col;
    };
    //
    getBG = function () {
     return this.__bg__;
    };
    //
    Stage.addProperty("bgColor", getBG, setBG);
    // add a listener to _level0
    // so that the bg is redrawn when a user resizes the Stage
    Stage.addListener(_level0)
    _level0.onResize = function  (){Stage.bgColor = Stage.bgColor}
    delete setBG;
    delete getBG;
    //Usage
    Stage.bgColor = 0x0000ff;
    
  4. // property:
    MovieClip.prototype.addProperty(”bkColor”, function () { return this.bkColor }, function © {
    if (arguments.length <= 0) return;
    var n = this == _root ? {_width : Stage.width + 4, _height: Stage.height + 4} : this;

    with (this) {
    clear();
    beginFill©;
    lineTo(n._width, 0);
    lineTo(n._width, n._height);
    lineTo(0, n._height);
    endFill();
    }

    this.__proto__.bkColor = c;
    });
    // usage:
    this.bkColor = 0xFF0000;
    trace(this.bkColor);

  5. Hey there!
    This script is just what i was looking for but it has one problem it seems.

    For cdrom use i am making a fullscreen projector using these FScommands:
    fscommand(”fullscreen”, “true”);
    fscommand(”showmenu”, “false”);
    fscommand(”allowscale”, “false”);

    Now, when i add this script the background color change works but it does not change the complete background though. it starts it only at the left top of the flash document (at position 0,0). So the top and the right part of the monitor window will be the old background color. How can i fix this and be sure it will work on all resolutions? btw, im a total newbie at actionscripting i have to admit.

    Greets,
    David

  6. David, just replace the initials variables that defines initial position, stage height and stage width to be full screen, in short, use this instead
    moveTo(-2880, -2880);
    lineTo(-2880, 2880);
    lineTo(2880, 2880);
    lineTo(2880, -2880);
    lineTo(-2880, -2880);

  7. AHa great, Thanks!
    What we tried was this;
    var sh = Stage.height + 3000;
    var sw = Stage.width + 3000;
    var rp = -1200;//let us have a start point

    cheers,

    David

  8. one more question now were at it, I am starting with a white background that turns darkblue at scene 2. would it be possible to gradually fade it from white to blue instead of having it switch instantly over 25 frames time for instance?

    :p

  9. can you insert the date to the comments? it would be helpful

  10. i need your help urgently if you feel free and having little bit time to give me to answer my few question that i have bg compenet i have seen your given code and try its work fine after fixing small erors. i want to edit bg color from external text file can you help me to solve this issue actully i have not much experaince in flash i am new but i can say i can work little bit good ………..i hope you will reply me as soon as possible when you have spare time….

  11. I keep getting a static script error on this code? i’m using Mx pro 2004? Please help…

  12. You are very welcome to visit my website.

    Johny Hobson o

  13. I like this BUT just wondering what the ‘this.__bg__ = col;’ and ‘return this.__bg__’ are for??

  14. Could this dynamic background color, script by any chance be put into flash 8 scripting. I have tryed but cant seem to figure it out. Thank you.

  15. //define a var for sh and sw
    //let us add 2 px each as flash eats them
    //in the Stage Object from the actual flash dimension
    var sh = Stage.height + 4;
    var sw = Stage.width + 4;
    var rp = 0;//let us have a start point
    trace("stage height = " + sh + " &amp; stage width = " + sw + " in pixels");
    setBG = function (col) {
     with (_level0) {
      clear();
      if (col != undefined) {
       beginFill(col);
       moveTo(rp, rp);
       lineTo(sw, rp);
       lineTo(sw, sh);
       lineTo(rp, sh);
       endFill();
      }
     }
     this.__bg__ = col;
    };
    //
    getBG = function () {
     return this.__bg__;
    };
    //
    Stage.addProperty("bgColor", getBG, setBG);
    delete setBG;
    delete getBG;
    //Usage
    Stage.bgColor = 0x0000ff;
    

Post yours

Sidenotes

Quick notes, scribbles, somehow related to this website and to what I do. Or perhaps I'm just plain lazy to make them into a full article.

What a bad team can do to a Startup?

Flash/Flex developer, Orian Marx, started his Startup with much zeal and enthusiasm but had to give-in to failures because of none other but the team - the founders involved. His article outlined the story so ...13th Oct, 2008

Introduction to Adobe AIR (NetTuts)

Nettuts have a nice Introduction to Adobe AIR which focus on using HTML/JS to build an AIR Application. It covers -- Adobe AIR Architecture, How to install AIR, Get the development SDK, Configurations, gets on to ...2nd Oct, 2008

Decide Team Type from Books

What do you think of the "Team-Type" if they got these books recently? What "Team-Type" do these books suggest? ( surveys) 29th Sep, 2008

What do investors look for in a startup?

A great video Interview with Venture Hacker Naval Ravikant answering the age-old question: What do you look for in a startup? Investors look for two things that are paramount above all: # Great team: It's obvious. It's a ...27th Sep, 2008

You don't mess with the 'Englineer Bother'

(Via: Hiring Horror) 24th Sep, 2008

View the Sidenotes Archive

Play the Penguin Game

Recommended

  • Downloads All downloads, Free and Open Source.
  • Forum Oinam’s technical discussion forum where developers and designers can discuss all technical topics.
  • o! Just Me Of colorful cultures, entertainment, media, life hacks, music, books and movies from hollywood & bollywood.
  • AS 2.0 Reference Reference for ActionScript 2.0 Programming Language used in Flash. Primarily stashed here for my own personal reference.
  • oCricket oCricket is about Cricket and people enthusiastic about it.
  • My Special Job My Special Job is a place where you can look for your weird necessities, strangely superb employees, when your need are more of those hackers, geeks, and ultimate rockstars in the Internet Technology.
  • Ode to Apple Dedicated to Apple - Mac, iPhone, iPod, iTunes, Quicktime, Apple TV and all the awesome softwares for the Apple Mac.

Download free Brajeshwar Wordpress Theme

Brajeshwar

Brajeshwar I firmly believe in keeping things simple, easy for users and I envison pushing the technical envelop time and again for the betterment of viable commercial and practical applications. More about me.

Brajeshwar Personal Identity Portal powered by VeriSign Labs

Brajeshwar's affinity with Adobe

My Photos

More photos on Flickr

Member of 9rules Network

Since its inception on 11th June, 2001, "Brajeshwar" has 864 Articles and 6,009 comments, contained within 20 categories and 1,233 tags.