-
problem with BitmapData
Hi,
I've started writing this class that draws a stripe of random color across the stage within certain parameters. I'm calling an instance of this class and then calling it's newLine() method 25 times in a loop with greater x coords passed in each time, to draw these stripes across the stage.
Sometimes it completely works, no errors, but often it will stop after a seemingly random number of cycles through the loop and give me this error:
ArgumentError: Error #2015: Invalid BitmapData.
at flash.display::BitmapData()
at LineMaker/newLine()
at lineMaker_test_fla::MainTimeline/frame1()
Here's the class code:
PHP Code:
package
{
import flash.display.MovieClip;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Rectangle;
public class LineMaker
{
private var lineCount:Number = 0;
private var target_mc:MovieClip;
private var bgWidth:Number;
private var BMPD:BitmapData;
private var BMP:Bitmap;
//constructor
public function LineMaker(target:MovieClip, bgWidth:Number)
{
this.target_mc = target;
this.bgWidth = bgWidth;
}
//create a bitmap line
public function newLine(wMax:Number, h:Number, xInterval:Number, xPos:Number, yPos:Number)
{
var w:Number = Math.random() * wMax;
var xPosition:Number = xPos + Math.random() * xInterval;
var thisColor:Number = getRandomColor();
this.BMPD = new BitmapData(w, h, false, thisColor);
this.BMP = new Bitmap(this.BMPD);
var BMPref:DisplayObject = this.target_mc.stage.addChildAt(this.BMP, 0);
BMPref.x = xPos;
}
private function getRandomColor(): Number
{
var color:Number = Math.random() * 0xFFFFFF;
return color;
}
}
}
Can anyone explain why I might be getting this error? I thought it might have something to do with the way I'm getting my random color value, but the error doesn't seem to be at all correlated to the color value at the time of failure...
Many thanks!
-
Maybe w is sometimes too small for it to handle appropriately?
-
I though about that, but I trace the value that it fails on, and sometimes the value is low, sometimes high, but well within the range of what often simply works. I also thought maybe it didn't like a decimal value as color value so I added a round() call around the number and that still changes nothing.
Oh, wait, you said w. That I haven't addressed. Maybe you are right, I'll write in a lower limit and see what happens. You may well be right.
-
Awesome! You're exactly right. It can't deal with a BitmapData object less than 1px in width.
Thanks a bunch.