开发者

AS3: Return MovieClip from Function

开发者 https://www.devze.com 2023-03-11 09:59 出处:网络
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

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 o开发者_JS百科nly if the object has been loaded.


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.
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消