9/6/2010 5:00:43 AM
Title:
Passing a reference through a function?
I have a strange problem when I try to create a function that loads an image from file. The reference to the bitmap data that I want to put the loaded image into, doesn't seem to be correctly remembered when flash calls the imageLoaded function. If I add the bitmapData variable to the stage in that function, it displays the image correctly. But, if in the same function, I add the variable which is being passed to the stage, I just get an empty texture. How can I return correctly the bitmap data that is being loaded?
private function _LoadTexture(path : String, bitmapData : BitmapData)
{
var imageLoader : Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
imageLoader.load(new URLRequest (path));
this._objectsLoading++;
function imageLoaded(event : Event)
{
bitmapData = Bitmap(imageLoader.content).bitmapData;
_FinishedLoading();
}
}
9/6/2010 5:10:45 AM
loading image is asynchronous process. Which means that your function execution is finished before the image is loaded . so it will always return you empty texture. Any function that is making external url calls can never return you externally loaded data, because function will not stop and wait for image to load and then return you data, but it will just return you the data immediately which would be empty.
9/6/2010 5:19:26 AM
I can see that, but doing some more experimenting, I found that returning the bitmap data like I did...
bitmapData = Bitmap(imageLoader.content).bitmapData;
..doesn't work, but using the copyPixels function works.
var temp : BitmapData = Bitmap(imageLoader.content).bitmapData;
bitmapData.copyPixels(temp , temp.rect, new Point(0,0));
Which is strange because the reference is stored, and can be used. I think that my problem was that I was changing where the bitmapData reference was pointing, and not the content itself if that makes any sense.
9/6/2010 5:44:51 AM
strange ,maybe this should also work
bitmapData = imageLoader.content as BitmapData ;
9/6/2010 5:51:11 AM
bitmapData = imageLoader.content as BitmapData ;
Nope... the only way that I found is to copy pixel by pixel, which is not very optimized, but at least it works.