Skip to main content
Known Participant
Sep 20, 2023 6:43 PM UTC
Answered

Troca de som de botões (javascript)

Olá!

Quero fazer uma tela que tem uma bandeira americana, uma bandeira brasileira e um objeto. Quando clico na bandeira americana e depois clico no objeto, ouço o nome do objeto em inglês. Se eu clico na bandeira brasileira e depois no objeto, ouço o nome do objeto em português (segue no anexo uma imagem do que quero fazer).

É possível, por meio de código, dizer que o som de um objeto seja trocado se um determinado botão for apertado? Como posso fazer isso?

Não quero colocar o respectivo som na bandeira, porque pretendo colocar mais elementos clicáveis na cena. 

 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Sep 21, 2023 3:14 PM UTC

    Hi.

     

    Here is an example:

    JavaScript code:

     

    var root = this;
    var usButton = root.usButton;
    var brButton = root.brButton;
    var appleButton = root.appleButton;
    var orangeButton = root.orangeButton;
    var highlight = root.highlight;
    var languages = { EN_US: "EnUS", PT_BR: "PtBR" };
    var language = languages.EN_US;
    var voice;
    
    function main()
    {
    	document.body.style.backgroundColor = lib.properties.color;
    	usButton.on("click", changeLanguage, null, false, { lang: languages.EN_US });
    	brButton.on("click", changeLanguage, null, false, { lang: languages.PT_BR });
    	appleButton.on("click", playVoice, null, false, { voicePrefix: "Apple" });
    	orangeButton.on("click", playVoice, null, false, { voicePrefix: "Orange" });
    }
    
    function changeLanguage(e, data)
    {
    	language = data.lang;
    	highlight.x = e.currentTarget.x;
    	highlight.y = e.currentTarget.y;
    }
    
    function playVoice(e, data)
    {
    	if (voice)
    		voice.stop();
    		
    	voice = playSound(data.voicePrefix + language);
    }
    
    main();

     

     

    FLA / download / source / files:
    https://bit.ly/3sZ6INX

     

    I hope this helps.


    Regards,

    JC

    11 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    Sep 20, 2023 6:54 PM UTC

    Olá!

     

    Yeah, it's possible.

     

    One approach would be to keep a global variable that stores the current language and based on the value of this variable you would play the corresponding sound for any object.

     

    So the flags would change this global variable value (e.g.: "ptBR") and the actual items would run a check in their click handler code to check if it is English or Brazilian Portuguese.

     

    Please let us know if you need an example of this kind of interactivity.

     

    Regards,

    JC

    Translate
    Known Participant
    Sep 20, 2023 7:17 PM UTC

    Olá, obrigada por responder.

    Na teoria eu entendo, mas não sei como fazer na prática... já vi como troca textos, mas não sei como é com áudios. Consegui fazendo um movieclip com a versão inglês e outro com a português. Ao clicar na bandeira, troca o movieclip. Mas queria algo que não precisasse fazer as duas versões.

    Você tem algum exemplo de código?

    Translate
    kglad
    Community Expert
    Community Expert
    Sep 21, 2023 3:10 AM UTC

    just assign a linkage id to your sounds and use code. eg,

     

    var sound1 = createjs.Sound.createInstance("soundId");
    sound1.play();

    Translate