Update
Because of for dual screen Capabilities.screenResolutionX returns only size of main screen, setSize method is added in class.
You should set width and height in setBitmap and loadBitmap methods as well.
source files are updated.
Here is the simple class file that allows to set tile background in flash.
Background can be set from both flash IDE library (linkage) and from external path.
Using Capabilities.screenResolutionX and Capabilities.screenResolutionY we can set width and height of background to match the screen size and don't care for resizing.
Example:
TileBg.as
-
package com.abrahamyan.util
-
{
-
/**
-
* ...
-
* @author Armen Abrahamyan | http://www.abrahamyan.com
-
*/
-
-
import flash.display.BitmapData;
-
import flash.display.Loader;
-
import flash.display.Sprite;
-
import flash.events.Event;
-
import flash.net.URLRequest;
-
import flash.system.Capabilities;
-
public class TileBg extends Sprite
-
{
-
private var _bgWidth:Number;
-
private var _bgHeight:Number;
-
private var _bitmapdata:BitmapData;
-
public function TileBg()
-
{
-
-
}
-
-
public function setBitmap(pBitmapData:BitmapData, pWidth:Number=0, pHeight:Number=0):void
-
{
-
_bitmapdata = pBitmapData;
-
with (graphics)
-
{
-
clear();
-
beginBitmapFill(_bitmapdata);
-
moveTo(0, 0);
-
lineTo(pWidth, 0);
-
lineTo(pWidth, pHeight);
-
lineTo(0, pHeight);
-
lineTo(0, 0);
-
endFill();
-
}
-
-
}
-
public function loadBitmap(pUrl:String,pWidth:Number=0, pHeight:Number=0):void
-
{
-
_bgWidth = pWidth;
-
_bgHeight = pHeight;
-
var loader:Loader = new Loader();
-
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBitmapLoaded, false, 0, true);
-
loader.load(new URLRequest(pUrl));
-
-
}
-
public function setSize(pWidth:Number,pHeight:Number):void
-
{
-
setBitmap(_bitmapdata, pWidth, pHeight);
-
}
-
public function clear():void
-
{
-
graphics.clear();
-
}
-
private function onBitmapLoaded(e:Event):void
-
{
-
setBitmap(BitmapData(e.target.content.bitmapData), _bgWidth, _bgHeight);
-
}
-
}
-
-
}
Hi Armen,
Thanks for posting this.
Would it be better to use stageWidth rather than Capabilities.screenResolutionX?
_maxStageWidth = stage.stageWidth;
_maxStageHeight = stage.stageHeight;
Cheers,
Adrian
Hi Adrian.
If you use stage.stageWidth,for the full browser flash sites you have to take care of resizing. If new size bigger than prev one, you have to redraw background. Using Capabilities.screenResolutionX, Y- just sets max available size and skips checking for resizing.
Hi Armen,
I was searching for a Tiling utility but couldn’t find one quickly, but I found your blog article.
I thought the tiling functionality might be improved by encapsulating it in a static class - hope you can find this useful, thanks for the article.
Dan.
package {
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.Rectangle;
public class GraphicUtil {
public static function TileBitmap(tileLayer:Sprite, area:Rectangle, tile:BitmapData):Sprite{
with(tileLayer.graphics){
clear();
beginBitmapFill(tile);
moveTo(0, 0);
lineTo(area.width, 0);
lineTo(area.width, area.height);
lineTo(0, area.height);
lineTo(0, 0);
endFill();
}
return tileLayer;
}
}
}
Thanks for your class Armen.
I found a little drawback in a dual monitor setup, though. Stretching the browser window over two monitors crops the tiling. Capabilities.screenResolution returns the resolution of only one screen.
Pieter
Hi Pieter, thanks for finding that, I don’t have dual screen
Well, then the only way i see u can set it right: is use stage resize event, and every time stage resizes u just redrew tile bg.
I will post new updated version as soon as I back home
Source files are updated, you can set width and height in setBitmap and loadBitmap methods, and use setSize when stage resizes.
thx
Great post. I’ll try it out!