开发者

Why is this throwing errors?

I am building a cube from an image in flash. The loader loads a sprite-map of the images:

+----++----++----++----++----++----+
|frnt||rght||back||left||top ||bot |
|    ||    ||    ||    ||    ||    |
+----++----++----++----++----++----+

The error I'm getting is:

Error #2005: Parameter 0 is of the incorrect type. Should be type BitmapData.

On the line front.draw(src, null, null, null, clip); (confirmed by commenting out code and not receiving an error). But I'm not sure why. I defined src as BitmapData, and a trace of src produces [object BitmapData].

private function loaderCompleteListener(e:Event):void
{
  log('Complete');
  var front:BitmapData,
    right:BitmapData,
    back:BitmapData,
    left:BitmapData,
    top:BitmapData,
    bottom:BitmapData,
    srcData:Bitmap,
    src:BitmapData,
    clip:Rectangle;

  try
  {
    srcData = this._imageLoader.content as Bitmap;
    src = srcData.bitmapData;
    front = new BitmapData(src.height, src.height, false, 0);
    right = new BitmapData(src.height, src.height, false, 0);
    back = new BitmapData(src.height, src.height, false, 0);
    left = new BitmapData(src.height, src.height, false, 0);
    top = new BitmapData(src.height, src.height, false, 0);
    bottom = new BitmapData(src.height, src.height, false, 0);
    clip = new Rectangle(0, 0, src.height, src.height);

    //This is the line that causes the error
    front.draw(src, null, null, null, clip); //<----
    clip.x += src.height;
    right.draw(src, null, null, null, clip);
    clip.x += src.height;
    back.draw(src, null, null, null, clip);
    clip.x += src.height;
    left.draw(src, null, null, null, clip);
    clip.x += src.height;
    top.draw(src, null, null, null, clip);
    clip.x += src.height;
    bottom.draw(src, null, null, n开发者_如何学Goull, clip);
  }
  catch ( e:Error )
  {
    log(e.message);
  }
}

What am I missing?

Edit to add:

One consideration may be the image size. I'm loading an image that's 1866×11196. I will test tomorrow to see if a smaller image works. It may just be that Flash can't handle images beyond a certain size.


The maximum width or length of bitmapdata is 8192, but as stated in the following link, not both. I.e. the total pixel count cannot exceed 16,777,215 pixels.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/BitmapData.html


As Chris pointed out, the built-in maximum for either height or width is 8192 for any BitmapData, so you will not be able to natively use an image that is 11196 in width.

However, there are workarounds, such as the BitmapDataUnlimited class, which you can find here.


First I need to thank @Chris, @EyeSeeEm, @Nathan Ostgard for leading me to the reason that I'm receiving an error. My expectation was to receive an IOErrorEvent.IO_ERROR if the image wasn't loaded correctly (that seems like an IO error to me at least), but Flash apparently didn't think that one through enough, which is why I'm getting a bogus error.

As everyone pointed out, the AS3 docs specify that the image cannot exceed a specific size.

Flash Player 9:

The maximum width and maximum height of a BitmapData object is 2880 pixels.

Flash Player 10:

In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData object is 8,191 pixels in width or height, and the total number of pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is 8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player 9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels in height and 2,880 in width.

But the important reason I'm getting an ArgumentError that doesn't seem to make sense is in the next lines in the CS4 docs:

Calls to any method or property of a BitmapData object throw an ArgumentError error if the BitmapData object is invalid (for example, if it has height == 0 and width == 0) or it has been disposed of via dispose().

So because my image was wider than 8191px, it was considered invalid, causing an inscrutable ArgumentError.

This makes sense in the context of the draw call, but still doesn't explain why I am able to call src.height without triggering an error.


Error is actually 'Parameter 0 is of the incorrect type. Should be type IBitmapDrawable' - not BitmapData. (Well on my machine anyway).

But BitmapData is indeed IBitmapDrawable - the problem lies in the fact an object typed as a certain class that hasn't been constructed, or equals null doesn't actually implement anything.

// Example
var src:BitmapData;
trace(src is IBitmapDrawable); // false

In your case, if srcData (the Bitmap object) doesn't have bitmapData it will return null and src (the BitmapData) is set to null. So when null is passed into draw() it freaks out because null isn't IBitmapDrawable.

For example if I do the following:

var o:BitmapData = new BitmapData(100, 100, false, 0);
o.fillRect(new Rectangle(0, 0, 100, 100), 0x334455);

var srcData:Bitmap = new Bitmap(o);
var src:BitmapData;

src = srcData.bitmapData;

trace(src is IBitmapDrawable);

var front:BitmapData = new BitmapData(100, 100, false, 0);
front.draw(src as IBitmapDrawable); // works!

But if I remove o (the bitmapdata) from srcData, such as:

var srcData:Bitmap = new Bitmap();
var src:BitmapData;

src = srcData.bitmapData;

trace(src is IBitmapDrawable);

var front:BitmapData = new BitmapData(100, 100, false, 0);
front.draw(src as IBitmapDrawable); // Doesn't work!

So again, in your case, the Bitmap object isn't receiving any input data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜