Copy link to clipboard
Copied
I have a little function:
public function makeBMD(dataObject,displayObject):void {
dataObject = new BitmapData(displayObject.width,displayObject.height, true, 0x00000000);
dataObject.draw(displayObject);
var bmp:Bitmap = new Bitmap(dataObject);
addChild(bmp);
}
Looks simple enough. I pass it a BitmapData variable (silBMD) and a MovieClip (theSilContainer). I want it to create a new BitmapData object, which I think it does since it also creates and adds the bmp to stage using the BitmapData object.
But, I have this other function:
public function testSolution():void {
makeBMD(playBMD,thePlayContainer);
var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;
trace ("0x" + diffBmpData.getPixel(100,100).toString(16)); // 0x332200
}
This creates a new object (just like it did to the first one) so that I can compare the two objects. What is happening when it gets called, however, is that I receive this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Copy link to clipboard
Copied
the only thing passed to either function is dataObject. and, from the code you showed, it doesn't appear playBMD or silBMD is non-null.
anyway, click file/publish settings/flash and tick "permit debugging". the line number with your (first) null object will be in the error message. fix that and work on the next (if any) error message.
Copy link to clipboard
Copied
Hey Kglad, thanks for the quick response.
Both silBMD and playBMD are defined like so:
package {
import flash.display.*;
import flash.geom.*;
import flash.events.MouseEvent;
import flash.text.*;
import fl.controls.*;
public class Test extends MovieClip {
var container = this;
.
.
.
// the solution bmds
var silBMD:BitmapData;
var playBMD:BitmapData;
The makeBMD function is called ONLY after the theSilContainer and thePlayContainer MovieClips have been added to the display list.
The debug doesn't tell me anything new. Even instantiating the silBMD and playBMD as empty bitmapdata returns the null reference error.
Copy link to clipboard
Copied
both of those are null. they are typed but they are null and that's what your error message is telling you.
you need to instantiate both of those before you try and use them.
Copy link to clipboard
Copied
I still don't understand what I'm doing here.
If I do this in the function
var silBMD:BitmapData = new BitmapData(theSilContainer.width,theSilContainer.height, true, 0x00000000);
silBMD.draw(theSilContainer);
trace(silBMD.width, silBMD.height);
var playBMD:BitmapData = new BitmapData(thePlayContainer.width,thePlayContainer.height, true, 0x00000000);
playBMD.draw(thePlayContainer);
trace(playBMD.width, playBMD.height);
var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;
trace ("0x" + diffBmpData.getPixel(0,0).toString(16))
Everything traces out EXCEPT for the last trace statement, which throws the error. Also, if I set up silBMD and playBMD as BitmapData variables outside of the function, and pass them in as parameters (along with the MovieClip they're receiving the data from), it doesn't work.
And the only difference I can see between my code and the example adobe code is that mine uses the draw method to draw the movieClip into the bitmapdata object.
Copy link to clipboard
Copied
those two bitmaps must have the same width and height and everything must be in the same scope. ie, those trace() outputs should be identical and those bitmapdata instances must be in the same timeline/function. are they?
Copy link to clipboard
Copied
They are not the same height and width. They are also not in the same function. But I can fix that...
Copy link to clipboard
Copied
when you prefix a variable with var inside a function body, you're making that variable local to the function. ie, it's not defined after that function executes.
in addition, you should read about the compare method so you can see the issue when the two bitmapdata objects have different size.
Copy link to clipboard
Copied
Ah, I was aware of using var in a function. I did define the silBMD and playBMD in the root of the package before, and then create the data using the makeBMD function which is why I posted here at the beginning (see my 1st and 2nd post--the 2nd indicates the vars are set up in the package).
If you're saying that's the only reason why it's throwing an error, then I'm back to square one.
Copy link to clipboard
Copied
in addition, you should read about the compare method so you can see the issue when the two bitmapdata objects have different size.
Copy link to clipboard
Copied
Okay, I got this to work, but I still don't know why one approach works and he other does not.
public function makeBMD(dataObject,displayObject):void {
dataObject = new BitmapData(displayObject.width, displayObject.height, true, 0x00000000);
dataObject.draw(displayObject);
}
public function makeBMDs():void {
silBMD = new BitmapData(theSilContainer.width, theSilContainer.height, true, 0x00000000);
silBMD.draw(theSilContainer);
playBMD = new BitmapData(thePlayContainer.width, thePlayContainer.height, true, 0x00000000);
playBMD.draw(thePlayContainer);
}
public function testSolution():void {
makeBMDs();
if (silBMD.height==playBMD.height && silBMD.width==playBMD.width){
var diffBmpData:BitmapData = silBMD.compare(playBMD) as BitmapData;
trace ("0x" + diffBmpData.getPixel(0,0).toString(16));
}
}
Passing silBMD and theSilContainer as paramaters to the makeBMD function always results in the error null.
Setting those same vars explicitly in he function makeBMDs works.
silBMD and playBMD are defined in the main package. Doesn't that make them globally available?
Anyway, thanks for all your attention to this thread.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more