Changing Background Color with Actionscript

One would think that such a basic task as dynamically changing backround is as easy as typing out one line of code. Unfortunately it's not that simple,  however it can be done and that's what matters.

swffile

The secret to the above movie are three things:

1) Using a Movieclip to act as the background

2) Using a code that colors the Movieclip

3) Emplying an stage listener that resizes the Movieclip if the stage is resizes

 

So, let me say a word on each of these:

1)

Remember to give the Movieclip an instance name. I called it rgbcolor.

2)

When using RGB value, don't forget to precede it with "0x".

bgcolor = new Color(bg_mc);
rgbcolor = "0x"+_root.rgb_color.text;
bgcolor.setRGB(rgbcolor);

Line 2 grabs the value of the Input Field and precedes it with "0x". Then we use the standard tint code for Symbols.

3)

function bgResize() {
Stage.align = "L,T";
bg_mc._width = Stage.width;
bg_mc._height = Stage.height;
}
stageListener = new Object();
stageListener.onResize = function() {
bgResize();
};
Stage.addListener(stageListener);
bgResize();

This code is to ensure that no matter what stage size is, the Movieclip will be exactly  the same size. You can read more here.

As usually,  downloadfiles below. Have fun!

AttachmentSize
change_bg_as.rar5.63 KB
change_bg_as.swf3.92 KB
Average: 4.3 (3 votes)