Tile background in AS3

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:

Get Adobe Flash player

TileBg.as

Actionscript:
  1. package com.abrahamyan.util
  2. {
  3.     /**
  4.      * ...
  5.      * @author Armen Abrahamyan | http://www.abrahamyan.com
  6.      */
  7.    
  8.     import flash.display.BitmapData;
  9.     import flash.display.Loader;
  10.     import flash.display.Sprite;
  11.     import flash.events.Event;
  12.     import flash.net.URLRequest;
  13.     import flash.system.Capabilities;
  14.     public class TileBg extends Sprite
  15.     {
  16.         private var _bgWidth:Number;
  17.         private var _bgHeight:Number;
  18.         private var _bitmapdata:BitmapData;
  19.         public function TileBg()
  20.         {
  21.            
  22.         }
  23.        
  24.         public function setBitmap(pBitmapData:BitmapData, pWidth:Number=0, pHeight:Number=0):void
  25.         {
  26.             _bitmapdata = pBitmapData;
  27.             with (graphics)
  28.             {
  29.                 clear();
  30.                 beginBitmapFill(_bitmapdata);
  31.                 moveTo(0, 0);
  32.                 lineTo(pWidth, 0);
  33.                 lineTo(pWidth, pHeight);
  34.                 lineTo(0, pHeight);
  35.                 lineTo(0, 0);
  36.                 endFill();
  37.             }
  38.        
  39.         }
  40.         public function loadBitmap(pUrl:String,pWidth:Number=0, pHeight:Number=0):void
  41.         {
  42.             _bgWidth = pWidth;
  43.             _bgHeight = pHeight;
  44.             var loader:Loader = new Loader();
  45.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onBitmapLoaded, false, 0, true);
  46.             loader.load(new URLRequest(pUrl));
  47.            
  48.         }
  49.         public function setSize(pWidth:Number,pHeight:Number):void
  50.         {
  51.             setBitmap(_bitmapdata, pWidth, pHeight);
  52.         }
  53.         public function clear():void
  54.         {
  55.             graphics.clear();
  56.         }
  57.         private function onBitmapLoaded(e:Event):void
  58.         {
  59.             setBitmap(BitmapData(e.target.content.bitmapData), _bgWidth, _bgHeight);
  60.         }
  61.     }
  62.    
  63. }

Download source files

Releated Posts


8 Responses to “Tile background in AS3”


  1. 1 Adrian Parr

    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

  2. 2 admin

    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.

  3. 3 Dan

    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;
    }

    }
    }

  4. 4 Pieter

    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

  5. 5 admin

    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 :)

  6. 6 admin

    Source files are updated, you can set width and height in setBitmap and loadBitmap methods, and use setSize when stage resizes.
    thx

  7. 7 Tile Saws

    Great post. I’ll try it out!

  1. 1 菲克斯咨询 » Blog Archive » 3月第一周精彩博客整理

Leave a Reply