1

I'm creating a quiz in flash using AS3. I'm having trouble trying to include sound when a user clicks a right or wrong answer.

How can I correctly play sound?

This is the code below

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
    if (myTextField11.text == answer1){
            trace("Correct answer!!");
            gotoAndPlay(2,"quiz1");
            count = count + 10;
            scoreBox.text = (count).toString();
            setTimeout(gotoAndPlay, 1250, 1, "quiz2");


        } else {
                trace(myTextField11);
                gotoAndPlay(3,"quiz1"); 
                var mySound:Sound = new WrongAnswer();
                WrongAnswer.play();
                setTimeout(gotoAndPlay, 1250, 1,"quiz2");

                }
}

I've now done this but no luck:

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
    if (myTextField11.text == answer1){
            trace("Correct answer!!");
            gotoAndPlay(2,"quiz1");
            count = count + 10;
            scoreBox.text = (count).toString();
            setTimeout(gotoAndPlay, 1250, 1, "quiz2");


        } else {
                trace(myTextField11);
                gotoAndPlay(3,"quiz1");

                MediaPlayer player = MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.WrongAnswer);
player.start();


            setTimeout(gotoAndPlay, 1250, 1,"quiz2");

            }
alan smith
  • 127
  • 1
  • 11

2 Answers2

1
MediaPlayer player=MediaPlayer.create(YourActivity.this,R.raw.nameofile);
player.start();

This code helps to play the music. Place the code inside the Onclick() method.

Tripathee Gaurav
  • 371
  • 3
  • 11
0

If you declared mySound as variable name for the WrongAnswer(); sound then use that exact same name when accessing by code.

import flash.media.Sound;

//button 1, see if the first text field equals the answer, if it does it plays the correct scene, if it doesnt it plays incorrect scene
myTextField11.addEventListener(MouseEvent.MOUSE_UP,state11);
function state11(evt:MouseEvent)
{
    if (myTextField11.text == answer1)
    {
        trace("Correct answer!!");
        gotoAndPlay(2,"quiz1");
        count = count + 10;
        scoreBox.text = (count).toString();
        setTimeout(gotoAndPlay, 1250, 1, "quiz2");
    } 
    else 
    {
        trace(myTextField11);
        gotoAndPlay(3,"quiz1"); 
        var mySound:Sound = new WrongAnswer();
        //WrongAnswer.play();
        mySound.play();
        setTimeout(gotoAndPlay, 1250, 1,"quiz2");
    }

}

If there's still issues then let me know. The alternative I was going to give was to just embed sound by code but that may not be necessary here...

VC.One
  • 14,790
  • 4
  • 25
  • 57