You know that in HTML sites often use textures (those square images of approximately 100x100)?
Have you ever tried to use it with Flash?
They are very beautiful especially if used in a Flash site that is shown at 100%.
In this case we have to deal even with the resizing of the browser, in which case the user did.
How?
With Actionscript 3.0 we "attach" textures from library on runtime.
We have a texture with dimensions of 320x320 pixels:

Create a new FLA file and save it as main.fla.
Create a new AS file and save it as Main.as in the same folder where I have main.fla and texture.gif.
Main.as is Document Class of main.fla, initially implemented in this way
Code:
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Main extends MovieClip
{
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
trace('ok');
}
}
}
Now import texture.gif in main.fla, drag it from library to the Stage, converts it to MovieClip named mc_texture and take off it from the stage.
Then assign a class to mc_texture with name Texture.
Here's the script to create the resizable background:
Code:
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class Main extends MovieClip
{
private var holder_mc:MovieClip;
private const TEXTURE_SIDE:int=320;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.addEventListener(Event.RESIZE,onStageResize);
createBackground();
}
private function createBackground():void
{
removeTextures();
var columns:Number=Math.ceil(stage.stageWidth/TEXTURE_SIDE);
var rows:Number=Math.ceil(stage.stageHeight/TEXTURE_SIDE);
var total:Number=columns*rows;
for(var i:int=0;i < total;i++)
{
var texture_mc:MovieClip=new Texture();
texture_mc.x=Math.floor(i%columns)*TEXTURE_SIDE;
texture_mc.y=Math.floor(i/columns)*TEXTURE_SIDE;
holder_mc.addChild(texture_mc);
}
}
private function removeTextures():void
{
if(holder_mc!=null)
{
removeChild(holder_mc);
holder_mc=null;
}
holder_mc=new MovieClip();
addChild(holder_mc);
}
private function onStageResize(evt:Event):void
{
createBackground();
}
}
}
Bookmarks