Change stage background color in AS3?
Asked Answered
F

8

15

I am using pure AS3 to build my project. I was wondering if there are anyways to change the stage background color through AS3...Thanks for the help....

Foreshorten answered 7/8, 2010 at 23:12 Comment(0)
S
23

like this:

[SWF(backgroundColor="0xec9900")]
public class Main extends Sprite
{
    }
Stepaniestepbrother answered 8/8, 2010 at 6:49 Comment(2)
The question is 'Change stage background...' and this answer tells you how to change the color of main MXML. Main MXML is a child of stage. Correct answer is stage.color = 0xABCDEF that gave Peter Gibson.Commentator
@Stepaniestepbrother His answer works with pure AS3. He just polluted his answer with crap you don't need.Quinquagesima
C
10

I have this in a creationComplete handler

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               creationComplete="on_init();">

private function on_init():void {
    stage.color = 0x000000;
}

Though I have a feeling it would work anywhere.

Capitalize answered 5/2, 2012 at 11:54 Comment(3)
There is no 'color' property for stage? Flash Player 10?Importune
Could be - this was flex4.6 on Flash Player 11 from memoryCapitalize
This answer is almost a perfect answer to the question above. When stage isn't null you can peek and poke the color of it. There is also an event: addEventListener(Event.ADDED_TO_STAGE, onAddToStage); (just remove the xml crap, I use pure AS3 and it works)Quinquagesima
N
9

This creates a shape and add it to the stage behind everything. To change the color anytime call: changeBGColor(0xFF0000) (to red)

It also maintains the size of the background (covering all area) when the windows is resized.

import flash.display.Sprite;
import flash.events.Event;

var default_bg_color:uint = 0xffffff;

var bgshape:Sprite;
stage.align = "TL";
stage.scaleMode = "noScale";

function initBG()
{
    bgshape = new Sprite();
    bgshape.graphics.beginFill(default_bg_color);
    bgshape.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
    addChildAt(bgshape, 0);
    stage.addEventListener(Event.RESIZE, resizeBGWithStage);
}
function changeBGColor(color:uint) 
{
    bgshape.graphics.beginFill(color);
    bgshape.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
}
function resizeBGWithStage(e:Event)
{
    try {
        bgshape.width = stage.stageWidth;
        bgshape.height = stage.stageHeight;
    } catch(e){}
}

initBG();
Nyctalopia answered 8/8, 2010 at 8:24 Comment(2)
Thanks Makram...but Wopdoowop's way is much easier... :D +1 thoughForeshorten
Interresting as an abstract class for every stages !Lindemann
W
5

You should be able to use the following line of Actionscript 3.0 to set the background color. 0x000000 for black, 0xFFFFFF for white and anything in between.

this.stage.color = 0x00000;

Wash answered 5/7, 2013 at 6:34 Comment(0)
P
4

You can set background colour on initialization, the way @Wopdoowop mentioned, but if you want to change it dynamically you would need to create your own bitmap/sprite/movieclip that would act as a background (should go below the rest of your content and have width and height of your stage) and change colour of that bitmap/sprite/movieclip.

Polished answered 8/8, 2010 at 8:1 Comment(0)
C
1
[SWF(width='700',height='525',backgroundColor='#000000',frameRate='30')]
public class RunTime extends Sprite {
Clarissa answered 13/7, 2013 at 12:51 Comment(0)
C
0

Try setting the backgroundColor of the application object.

Cordiform answered 7/8, 2010 at 23:27 Comment(1)
Thanks...would you please explain more details? I have tried google but couldn't find anything.....Thanks.Foreshorten
R
0

I suggest making a sprite and then making it in the back. This is the way I would do it.

Make sure to import flash.display.Sprite;

var bkg:Sprite=new Sprite();
//replace the 0x000000 with a hex code.
bkg.graphics.beginFill(0x000000, 1)
bkg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight)
bkg.graphics.endFill()
addChild(bkg)

a plus about this is that you can draw a background (if you want) either manually or with the code and then put it in through the code.

Rosmunda answered 29/10, 2016 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.