How to change the background of a Flash document using ActionScript 3.0?

Let me preface this question by saying that I am a .NET developer at heart, just helping a friend with the project he is working on.

I searched the Internet for something that, in my opinion, should be fairly simple. Here is what I have:

  • A standard Flash CS5 document with one layer called a background.
  • The document is associated with the Game class, which extends MovieClip.
  • I execute some logic in the "Game" class after calling the "Initialize" method.

I want to change the background color of a document at runtime to a different one (for example, a different color, gradient or image). Simple, right? Probably no. I can not understand. Can some friendly .NET friendship explain how to fix this?

+4
source share
3 answers

If you want the background to change color and not need to paint it, javascript may be a good solution to this problem.

what you change will depend on the insert code, but the parameter you want to change is bgcolor.

in the prototype, javascript will look something like this:

$('yourFlashContainerId').down('[name="bgcolor"]').writeAttribute('value','#000000'); 

to draw it in flash you can do something like this:

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

This will draw a square with a black background (change the hexadecimal text on line 2 to change the color), set its size to the size of the scene, set x and y to 0, then add it at the bottom of the display stack.

Any of these two methods should work.

Edit: another way:

You can also set the wmode parameter to "transparent" and change the background color containing the div.

assuming your built-in flash memory has the following meanings:

 <param name="wmode" value="transparent"> 

prototype:

 $('yourFlashContainerId').setStyle({'background-color':'#000'}); 

JQuery

 $('#yourFlashContainerId').css({'background-color':'#000'}); 

Native

 document.getElementById('yourFlashContainerId').style.background-color="#000"; 
+3
source

You can create the necessary backgrounds during development and, for convenience, place them at different levels. Convert each background to MovieClip - each with its own instance name (bg1, bg2, etc.).

In the constructor function, you can specify which background is visible and invisible by setting its .visible property:

 bg1.visible = true; bg2.visible = false; etc. 

If you want to change the backgrounds, just change the visibility properties. Using this method, you can have all kinds of backgrounds - bitmaps, gradients, simple colors, etc.

+1
source

The color of the document is actually set in the HTML attachment. To have a dynamic background color, I would suggest creating a background movie clip in which you will draw the color when you need. Draw a drawing that you can use the drawing API .

0
source

All Articles