1

I've got a code that reads a sound in the language chosen by the user. For now, my code is :

if (english == true){
 helloSound = new hello_english(); 
}
if (french == true){
 helloSound = new hello_french(); 
}

As I've got even more words and even more languages to add, I want to simplify my code.

What I'd like to do :

When app launch, the user choose the language.

english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);

function goEnglish(event:MouseEvent):void {
var audioChosen = "english";
}
function goFrench(event:MouseEvent):void {
var audioChosen = "french";
}

And then is it possible to do something like?

 helloSound = new hello_(audioChosen)(); 

So, in this example, the sound that will be play, would be : hello_english

2 Answers 2

3

Using getDefinitionByName

To instantiate an object using its class name from a string, you can use getDefinitionByName, as mentioned in this question: Instantiate a class from a string in ActionScript 3

// Find the relevant class:
var helloSoundFileClass:Class = getDefinitionByName("hello_english") as Class;
// Create a new instance of the class we found:
var helloSound:Sound = new helloSoundFileClass();

Example usage

Here's an example based on the original code:

var audioChosen:String;
var helloSound:Sound;
var goodbyeSound:Sound;

english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);

// Set a "default" language.
audioChosen = "english"
onLanguageChanged();

// When user chooses a language:
function goEnglish(e:MouseEvent):void {
    audioChosen = "english";
    onLanguageChanged();
}
function goFrench(event:MouseEvent):void {
    audioChosen = "french";
    onLanguageChanged();
}

function onLanguageChanged() {
    // Find the relevant classes:
    var helloSoundFileClass:Class = getDefinitionByName("hello_" + audioChosen) as Class;
    var goodbyeSoundFileClass:Class = getDefinitionByName("goodbye_" + audioChosen) as Class;

    // Create a new instance of the class we found:
    helloSound = new helloSoundFileClass();
    goodbyeSound = new goodbyeSoundFileClass();
}

...

// Later, we can play the sounds:
helloSound.play();
goodbyeSound.play();
Sign up to request clarification or add additional context in comments.

Comments

2

Just an alternative way to achieve desired result.

  • Has one central function setLanguage to check/set according to relevant button.
  • Loads MP3 files instead of using Sound from Library.

You could try:

//english.addEventListener(MouseEvent.CLICK, goEnglish, false, 0, true);
//french.addEventListener(MouseEvent.CLICK, goFrench, false, 0, true);

var helloSound :Sound;
var audioChannel :SoundChannel = new SoundChannel();
var isAudioPlaying :Boolean = false;
var audioChosen :String = "";

english.addEventListener(MouseEvent.CLICK, setLanguage);
french.addEventListener(MouseEvent.CLICK, setLanguage);
spanish.addEventListener(MouseEvent.CLICK, setLanguage);
japanese.addEventListener(MouseEvent.CLICK, setLanguage);
greek.addEventListener(MouseEvent.CLICK, setLanguage);

function setLanguage( evt :MouseEvent ) :void 
{
    //trace("clicked is : " + evt.target.name ); //# get instance name of clicked: eg... "english"
    var user_language = evt.target.name; //# will set to instance name of "evt" that triggered a "clicked" event

    //# check instance name of clicked object
    switch ( user_language )
    {
        case "english": trace("Hello World!"); audioChosen = "english"; break;
        case "french": trace("Bonjour Monde!"); audioChosen = "french"; break;
        case "spanish": trace("Hola Mundo!"); audioChosen = "spanish"; break;
        case "japanese": trace("こんにちは世界!"); audioChosen = "japanese"; break;
        case "greek": trace("γειά σου κόσμος!"); audioChosen = "greek"; break;

    }

    //# also can check with IF / ELSE version instead of the above SWITCH checking...
    //if( user_language == "english" ) { trace("Hello World!"); audioChosen = "english"; }
    //if( user_language == "japanese" ) { trace("こんにちは世界!"); audioChosen = "japanese"; }

    //# run some other function...
    do_Some_Action();
}

function do_Some_Action() :void 
{
    trace("User has chosen language: " + audioChosen);
    trace("::::::::::::::::::::::::::::::::::::::::");

    //# check if the URL is correct, example of expected: "hello_english.mp3"
    //var temp :String = "hello_" + audioChosen + ".mp3"; trace("temp is: " + temp);

    if ( audioChannel == null ) { audioChannel = new SoundChannel(); }
    if ( isAudioPlaying == true ) { audioChannel.stop(); }

    helloSound = new Sound();
    helloSound.load( new URLRequest( "hello_" + audioChosen + ".mp3" ) );
    audioChannel = helloSound.play();
    isAudioPlaying = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.