Tiled Image Background

  • AmieCutie
  • Newbie
  • Newbie
  • User avatar
  • Posts: 12

Post 3+ Months Ago

So I have a navigation background bar that adjusts it's with to the stage and the height is defined in it's XML file. Now this works great, I'm trying to add a tiled image to the background. The image is the same height as the background graphic so it only needs to be repeated on the x axis. Below is my code, I feel something small may be off because it looks right but it doesn't want to work. :(

Code: [ Select ]
protected function _initMenuBg():void
        {
            var size : Object = StageController.getInstance().getSize();
            var h : int = size.h;
            var w : int = size.w;
            _mainMenuBg = new Sprite();
            _mainMenuBg.alpha = 0;
            Tweener.addTween(_mainMenuBg, { alpha:1, time:tweenerTime, transition:"easeoutquad" } );
            
            var bgloader = new Loader();
            bgloader.load(new URLRequest("assets/images/nav-background.png"));
            var bitground:BitmapData = new BitmapData(bgloader.width, bgloader.height, false);
            bitground.draw(bgloader);
            
            _mainMenuBg.graphics.beginBitmapFill(bitground, null, true, true);
            DrawRectUtil.draw(_mainMenuBg, w, _mainMenuBgHeight, _mainMenuBgColor, _mainMenuBgAlpha);
            _mainMenuBg.graphics.endFill();

            container.addChildAt(_mainMenuBg, 0);
        }
  1. protected function _initMenuBg():void
  2.         {
  3.             var size : Object = StageController.getInstance().getSize();
  4.             var h : int = size.h;
  5.             var w : int = size.w;
  6.             _mainMenuBg = new Sprite();
  7.             _mainMenuBg.alpha = 0;
  8.             Tweener.addTween(_mainMenuBg, { alpha:1, time:tweenerTime, transition:"easeoutquad" } );
  9.             
  10.             var bgloader = new Loader();
  11.             bgloader.load(new URLRequest("assets/images/nav-background.png"));
  12.             var bitground:BitmapData = new BitmapData(bgloader.width, bgloader.height, false);
  13.             bitground.draw(bgloader);
  14.             
  15.             _mainMenuBg.graphics.beginBitmapFill(bitground, null, true, true);
  16.             DrawRectUtil.draw(_mainMenuBg, w, _mainMenuBgHeight, _mainMenuBgColor, _mainMenuBgAlpha);
  17.             _mainMenuBg.graphics.endFill();
  18.             container.addChildAt(_mainMenuBg, 0);
  19.         }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 473

Post 3+ Months Ago

Not sure if you have figured out the issue yet but the one thing I can think of is that bgloader.width, bgloader.height and image data are not set since there is very little in between the load() call and where you use the info.

Taken from http://help.adobe.com/en_US/FlashPlatfo ... phics.html
ACTIONSCRIPT Code: [ Select ]
package {
    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.geom.Matrix;
 
    public class Graphics_beginBitmapFillExample extends Sprite {
 
        private var url:String = "image1.jpg";
        private var loader:Loader = new Loader();
 
        public function Graphics_beginBitmapFillExample() {
 
            var request:URLRequest = new URLRequest(url);
           
            loader.load(request);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, drawImage);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        }
 
        private function drawImage(event:Event):void {
 
            var mySprite:Sprite = new Sprite();
            var myBitmap:BitmapData = new BitmapData(loader.width, loader.height, false);
 
            myBitmap.draw(loader, new Matrix());
           
            var matrix:Matrix = new Matrix();
            matrix.rotate(Math.PI/4);
           
            mySprite.graphics.beginBitmapFill(myBitmap, matrix, true);
            mySprite.graphics.drawRect(100, 50, 200, 90);
            mySprite.graphics.endFill();
           
            addChild(mySprite);
        }
 
         private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Unable to load image: " + url);
        }
    }  
}
 
  1. package {
  2.     import flash.display.Sprite;
  3.     import flash.display.BitmapData;
  4.     import flash.display.Loader;
  5.     import flash.net.URLRequest;
  6.     import flash.events.Event;
  7.     import flash.events.IOErrorEvent;
  8.     import flash.geom.Matrix;
  9.  
  10.     public class Graphics_beginBitmapFillExample extends Sprite {
  11.  
  12.         private var url:String = "image1.jpg";
  13.         private var loader:Loader = new Loader();
  14.  
  15.         public function Graphics_beginBitmapFillExample() {
  16.  
  17.             var request:URLRequest = new URLRequest(url);
  18.            
  19.             loader.load(request);
  20.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, drawImage);
  21.             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  22.         }
  23.  
  24.         private function drawImage(event:Event):void {
  25.  
  26.             var mySprite:Sprite = new Sprite();
  27.             var myBitmap:BitmapData = new BitmapData(loader.width, loader.height, false);
  28.  
  29.             myBitmap.draw(loader, new Matrix());
  30.            
  31.             var matrix:Matrix = new Matrix();
  32.             matrix.rotate(Math.PI/4);
  33.            
  34.             mySprite.graphics.beginBitmapFill(myBitmap, matrix, true);
  35.             mySprite.graphics.drawRect(100, 50, 200, 90);
  36.             mySprite.graphics.endFill();
  37.            
  38.             addChild(mySprite);
  39.         }
  40.  
  41.          private function ioErrorHandler(event:IOErrorEvent):void {
  42.             trace("Unable to load image: " + url);
  43.         }
  44.     }  
  45. }
  46.  


They use an event listener to draw after the loading has completed giving time for your loader object to have a height, width and image data. If you haven't solved your issue I hope this helps a bit

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 17 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.