Use code to change background color in AS3?
Asked Answered
S

4

6

Is it possible to change the stage's background through actionscript?

How do I do it? (Code please.)

Shatzer answered 9/11, 2010 at 20:27 Comment(0)
M
8

The stages background color can be changed using its graphics object.

If you have a reference to the stage:

stage.graphics.beginFill( 0x00FF00 );
stage.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
stage.graphics.endFill();

If this is in your document class:

graphics.beginFill( 0x00FF00 );
graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
graphics.endFill();
Minelayer answered 10/11, 2010 at 2:6 Comment(0)
H
1

I just wanted to add that the stage is kind of the exception of the rule. so to answer your question directly without a solution the answer is no you can't because the stage itself is by default empty with nothing on it. then added to Alan's answer the stage is kind of the ugly duck some of the properties that it inherits as it inherits it's properties from the DisplayObjectContainer don't apply such as you can't set a mask to the stage or change it's x,y and so on. so you can't change the background but Alan's solution is perfect you draw dynamically a shape and place it on your stage and then give it what ever color you want.

Helgeson answered 11/11, 2010 at 0:2 Comment(0)
M
0

Edit: It looks like my original answer does not work.

You will have to add a sprite to the stage and set the color on that.

Something like this should work:

var bg:Sprite = new Sprite();
bg.graphics.beginFill(0xFF0000);
bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
bg.graphics.endFill();
bg.x = stage.stageWidth;
bg.y = stage.stageHeight;
stage.addChild(bg);

Original:

Try this to set it to red:

stage.opaqueBackground = 0xFF0000;

Just change 0xFF0000 to the color you want.

Mcdaniels answered 9/11, 2010 at 20:33 Comment(3)
The original works if you set stage.cacheAsBitmap = true, but it won't color outside the stage area. (Think about when using fullscreen or when scaling.)Shatzer
Sprite is not necessary in this case, simply use a ShapeBeguin
The Sprite isn't necessary. See my answer.Minelayer
G
0

You can also set the background for browser embedding with swfobject.

Here is more information and example code: http://board.flashkit.com/board/showthread.php?t=814461

Gastric answered 11/11, 2010 at 0:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.