简体   繁体   中英

Check whether a Movieclip exists in library

I am trying to add sound from library. Like backGroundSound:Sound = new BackGroundSound(); // BackGroundSound is mp3 file class name.

I want to use this as common for all developers who use Backgroundsound for their game.

I want to check before adding it like whether it is exported in library to prevent compilation error.

Please help me to solve this problem.

Try the following:

var classExist:Boolean = isClassExists("BackGroundSound");

private function isClassExists(className:String, domain:ApplicationDomain = null):Boolean
{
    var res:Boolean;

    if(domain)
    {
        res = domain.hasDefinition(className);
    }
    else
    {
        res = getDefinitionByName(className);
        //or the same
        //res = ApplicationDomain.currentDomain.hasDefinition(className);
    }

    return res;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM