views:

1444

answers:

4

I am having problems loading a bitmapData. I am getting the following error

Engine Init //trace
loadimage//trace
ArgumentError: Error #2015: Invalid BitmapData.
    at flash.display::BitmapData()

Below is my code. it appears it happens after the trace loadimage

package com.objects {

    import flash.display.Sprite;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.net.*;
    import flash.events.*;
    import flash.display.LoaderInfo;

    public class gameObject extends Sprite {

        protected var w:Number;
        protected var h:Number;
        protected var image:BitmapData;
        protected var canvas:Bitmap;
        protected var px:Number;
        protected var py:Number;

        public function gameObject():void
        {
            init();
        }

        private function init():void
        {

        }

        public function loadImage(imageDir:String, w:Number, h:Number, px:Number, py:Number):void
        {
            this.w = w;
            this.y = y;
            this.px = px;
            this.py = py;

            trace("loadimage");
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageComplete);
            loader.load(new URLRequest(imageDir));
        }

         private function imageComplete(e:Event):void {
            var loader:LoaderInfo = LoaderInfo(e.target);

            image = Bitmap(loader.content).bitmapData;
            drawImage();
         }

         private function drawImage():void
         {
            var tilePoint:Point = new Point(0,0);
            var tileRect = new Rectangle(py,px,w,h);
            trace(loader.content);
            var canvasData:BitmapData = new BitmapData(w,h);
            trace("got canvas data");
            canvasData.copyPixels(image,tileRect,tilePoint);
            trace("copied pixels");
            canvas = new Bitmap(canvasData);
         }
    }
}

And my call the the method is like so

balls = new Array();
            balls[0] = new gameObject();
            balls[0].loadImage("com/images/ball.gif", 15,15,0,0);

When I trace the Loader.content, below is what shows

Engine Init
loadimage
[object Bitmap]
ArgumentError: Error #2015: Invalid BitmapData.
    at flash.display::BitmapData()
A: 

I think you want to reference the Loader of the e.target, not the LoaderInfo. Try to trace(loader.content) right before your image= line and see if it tells you what type of data content is.

justkevin
I tried that and it says I cant convert LoaderInfo into a Loader(), so LoaderInfo is correct also when I trace loader.content, I get ...[object Bitmap] which means an bitmap object is in it.
numerical25
what is strange is that it works fine if I hard code canvasData width and height. but if i use the varibles, it doesnt
numerical25
A: 

Try changing your imageComplete() into this:

private function imageComplete(e:Event):void {
    var loader:LoaderInfo = LoaderInfo(e.target);
    var bitmap:Bitmap = loader.content as Bitmap;
    if(bitmap){
        image = bitmap.bitmapData;
        drawImage();
    } else {
        trace("invalid data!");
    }
}

That should allow you to catch the error before it happens. The next question is WHY your bitmap isn't parsed okay.

grapefrukt
hey, I traced loader.content, it shows [object Bitmap]
numerical25
A: 

Ok , now I fixed it. If you take a good look at the code, you will see the property Y being set for my class varible 'w'

numerical25
A: 

also check if your image isn't too large. I got this error on images which were bigger then 3000 pixels ...

Frank