I'm pretty sure it's because the loader works asynchronously
Correct
but I have no idea how to modify it to be able to return the image from the function
There is no way to do that.
Why?
Because it is asynchronous.
You can't return a result of a request straight after the request has been made.
The execution of the code does not pause and wait for the response to come back, so your function will hit the return statement before you get the data, and that is why it is returning null
.
The only way to handle this is to listen for the response (Event.COMPLETE) and run a callback function after the response from the server has been received.
So following your code example, you could do:
private static var _callback :Function;
public static function loadImage(path:String, callback:Function):void
{
_callback = callback;
// ....
and then:
public static function check(e:Event):void
{
// ...
_callback( whateverYouWantToReturnGoesHere );
This should work assuming that you only have one image loaded at a time (as your methods are static), otherwise you will need to implement some sort of request queue handling.
I hope this helps.