Archive | December 2008

How to fill Stage background with a target picture

I’d like to give 3 ways to to fill Stage background with a target picture,just like the background-repeat effect in CSS for the web page.

You should first ensure there is a MovieClip holding the picture with a linkedID in the Library.Surely it is highly recommonded to load an extenal swf file,with the MovieClip in its own library,into the main swf.And it would be much easier to change background without updating the main swf by this way.

1.attachMovie and duplicateMovieClip the MovieClip to fill the Stage(AS2.0)
If you have to publish your swf version lower than 8(7 or lower),attachMovie and duplicateMovieClip the target bitmap MovieClip to fill the whole Stage.

var a:Number = Math.floor(Stage.width / img_width);
var b:Number = Math.floor(Stage.height / img_height);
for (var i:Number = 0; i<=b; i++)
{
	for (var j:Number = 0; j<=a; j++) 
	{
		bg_mc.duplicateMovieClip("bg_"+i+"_"+j, bg_mc.getNextHighestDepth(), {_x:j*img_width, _y:i*img_height});
	}
}

2.load the background MovieClip and fill the Stage with Bitmap(AS2.0)
If you’re allowed to publish your swf version higher than 7(8 or higher),you can extend your background MovieClip to fill the whole Stage with bitmap like this.Compared to Method 1,it obviously saves lots of resources.

var bmpData:BitmapData = new BitmapData(img_width, img_height);
bmpData.draw(img_target);
for(var i:Number = 0;i<math.ceil(Stage.height/img_height);i++)
 {
 	for(var j:Number = 0;j<math.ceil(Stage.width/img_width);j++)
 	{
 		var bmp:Bitmap = new Bitmap(bmpData);
 		bmp.y = img_height*i;
 		bmp.x = img_width*j;
 	}
 }

3.load the background MovieClip and fill the Stage with Graphic fill(AS3.0)
If you’re allowed to publish your swf version higher than 8(9 or higher),why not try Graphic Fill.It is used just as the filling tool in PhotoShop.

var bmpData:BitmapData = new BitmapData(img_width, img_height);
bmpData.draw(img_target);
var graphic:Graphics = shell.graphics;
graphic.beginBitmapFill(bmpData);
graphic.drawRect(0,0,stage.width,stage.height);
graphic.endFill();