1

Sorry if this question has already been answered but I can't seem to find any relevant examples online. I basically have a class which loads a set of MovieClip objects and provides accessor functions to return them.

public function getMovieClip( mc:MovieClip ):Boolean
{
    if( allFilesLoaded )
    {
        mc = fileLoader.content;

        return true;
    }
    else
    {
        return false;
    }
}

Obviously this doesn't work, but it gives an idea of what I'm trying to do. I want to return a MovieClip to the calling code only if the object has been loaded.

PeddleSpam
  • 13
  • 3

2 Answers2

1

Try this:

// change the allFilesLoaded value to view the other result
var allFilesLoaded:Boolean = true;

trace(getMovieClip(null) );

function getMovieClip( mc:* ):*
{
    if( allFilesLoaded )
    {
        mc = new MovieClip();

        return mc;
    }
    else
    {
        return false;
    }
}

The asterisk denotes that the type of data that is returned is unknown, and we could both be Boolean as a MovieClip.

If allFilesLoaded then returns the mc (MovieClip) otherwise it returns false (Boolean).

Hope that helps.

Francis
  • 33
  • 11
  • I think that could work, but then would I have to use a dummy variable to store the return value and then check it with **typeof**? It seems a little messy using undefined return types. This would be so much simpler in C :P – PeddleSpam Jun 03 '11 at 20:24
  • You should’t have variable return values when you don’t really need them. Instead just return null. – poke Jun 03 '11 at 20:25
1

You can’t modify the parameter like that. Instead, return the MovieClip like this:

public function getMovieClip():MovieClip
{
    if ( allFilesLoaded )
    {
        return fileLoader.content;
    }
    else
    {
        return null;
    }
}

And then you can just use it like this, even with reading the MovieClip:

var mc:MovieClip; // defined somewhere

// later...
if ( ( mc = x.getMovieClip() ) )
{
    // all files were loaded and mc is not null.
}
else
{
    // files were not loaded and mc is null.
}
poke
  • 369,085
  • 72
  • 557
  • 602
  • I'm guessing this won't throw an error? In that case I could just assign the return value to a MovieClip in the calling code and check it for NULL? – PeddleSpam Jun 03 '11 at 20:27
  • @PeddleSpam: Added an example how you could use it. But yes, basically you check if the return value is not null (instead of checking if it is true, with your original function). – poke Jun 03 '11 at 20:28
  • Great this is a much cleaner solution, thanks ^_^ The class already dispatches an event for an unsuccessful load, but still I didn't like having a "potential" bug if for some reason the accessor gets called. – PeddleSpam Jun 03 '11 at 20:33