views:

447

answers:

1

Say I loaded a bitMap into a bitmapData type called tileImage.

 tileImage = Bitmap(loader.content).bitmapData;

say I decided to add that bitmap into a sprite like below

this.graphics.beginBitmapFill(tileImage );
this.graphics.drawRect(0, 0,tWidth ,tHeight );

It would of course work. But say If I decided to add tileImage into a another bitMapData type like below

var tImage:BitmapData = new BitmapData(30,30);
tImage.copyPixels(tileImage,tRect,tPoint);

and I then added tImage to my sprite

this.graphics.beginBitmapFill(tImage);
this.graphics.drawRect(0, 0,tWidth ,tHeight );

I then get the following error

ArgumentError: Error #2015: Invalid BitmapData.

tRect and tPoint are all predefined and set. tRect x and y are 0,0 and the width and height are 30x30. tPoint is 0,0 as well. Yes I understand that this is a very brief explanation but I wanted to elaborate that a bitMapdata type that has its data from the copypixel method does not work with beginBitmapFill. but a varible that gets its data straigt from the source, does. One works, and one doesnt, yet they are both the same data types. why is this ?

A: 

Hmmm... not sure what is going on here...

It seems like something in the copyPixels method is not correct and is therefor not returning a valid BitmapData object.

Add a trace(tImage) after the copy and make sure you get

[object BitmapData]

This code works just fine for me:

package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    /**
     * ...
     * @author Sean Berry
     */
    public class BitmapDataDocumentClass extends MovieClip {

        private var _loader:Loader;
        private var _tileImage:BitmapData;
        private var _testSprite1:Sprite;
        private var _testSprite2:Sprite;

        public var tWidth:int = 200;
        public var tHeight:int = 200;

        public function BitmapDataDocumentClass() {
            _testSprite1 = new Sprite();
            _testSprite2 = new Sprite();
            _testSprite2.x = 250;
            _testSprite2.y = 0;
            addChild(_testSprite1);
            addChild(_testSprite2);
            _loader = new Loader();
            _loader.load(new URLRequest("Cow.png"));
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _loaded, false, 0, true);
        }

        private function _loaded($evt:Event):void {
            _tileImage = Bitmap(_loader.content).bitmapData;
            _testSprite1.graphics.beginBitmapFill(_tileImage);
            _testSprite1.graphics.drawRect(0, 0, tWidth, tHeight);
            _testSprite1.graphics.endFill();

            var tImage:BitmapData = new BitmapData(30, 30);
            trace(tImage);
            tImage.copyPixels(_tileImage, new Rectangle(0, 0, 30, 30), new Point(0, 0));

            _testSprite2.graphics.beginBitmapFill(tImage);
            _testSprite2.graphics.drawRect(0, 0, tWidth, tHeight);
            _testSprite2.graphics.endFill();

        }
    }
}
sberry2A