Tutorial :Change stage background color in AS3?



Question:

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


Solution:1

like this:

[SWF(backgroundColor="0xec9900")]  public class Main extends Sprite  {      }  


Solution:2

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.


Solution:3

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();  


Solution:4

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;


Solution:5

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.


Solution:6

[SWF(width='700',height='525',backgroundColor='#000000',frameRate='30')]  public class RunTime extends Sprite {  


Solution:7

Try setting the backgroundColor of the application object.


Solution:8

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »