views:

914

answers:

1

Hello

I am creating a quiz in which the user matches an object (movie clip), to a sound clip that is played. The sounds are stored in an array and a random one is chosen. Then 4 random movie clips are created dynamically which hold the object image. I need a way to link the sound clip to the movie clips to check if the correct one was clicked. Here is the code so far:

var randSound = Math.round(Math.random()*1);         // Rand no 0-4
var sounds:Array = [cat, doorCreek];                 // Sound array
var soundClip:Sound = new sounds[randSound];         // Create random sound

sound_btn.addEventListener(MouseEvent.CLICK, playSound);    // Re-play sound button
function playSound(e:MouseEvent) { soundClip.play();  }

var clips:Array =[cat, door, wind, water];      // Movie clip array (will be longer)

// Add objects to stage
for(var i=0; i<4; i++)
{   
    var rand = Math.round(Math.random()*4);     // 4 clips as answer options

    var myRandClip:MovieClip=new clips[rand];      // Create random movieclip 

    // Create listener for the movieclip
    myRandClip.addEventListener(MouseEvent.CLICK, getIndex);

    function getIndex(e:MouseEvent)
    {
     trace(rand);
    }

    this.addChild(myRandClip);
}

Of course at the moment this function to get the index of the movie clip just gets the last rand number generated. I need a way to embed some sort of id into the generated movie clip. I can then simply test if it is the same as the sound clips index for example. I'll then be doing the same for each question (10 in total)

Hope that is clear and someone can help out. Many Thanks

A: 

I don't understand your problem. You are the Boss of your code. Just us OOP - there are really many ways of doing it right :)

Ok, one of the simplest:

// in constructor or AddToStage event handler, or just in timeline

soundIndex = Math.round(Math.random()*soundArray.length);
playSoundFormArray(soundIndex);

var btn:MyButton;
for(var i:uint=0; i< buttonsArray.length; i++){
   btn = MyButton(index, checkFunction);
   // changee position, etc.
}

//.....

private function checkFunction(index:uint):void
{
    if(soundIndex == btnIndex){
      // your code
    }
}

///MyButton.as
// Simple Sprite extended class with click event handler
package{
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    public class MyButton extends Sprite 
    {
     private var _callBack:Function;
     private var _index:uint;

     public function Main(index:uint, callBack:Function, label:String="btn") 
     {
      _index = index;
                    _callback = callBack;
                    addEventListener(Event.ADDED_TO_STAGE, init);

                    // customizing button
                    var textField:TextField = new TextField();
                    textField.text = label;
                    // or your code

     }

     private function init(e:Event):void 
     {
      addEventListener(Event.Click, clickHandler);
     }

            private function clickHandler(e:MouseEvent):void 
     {
      _callBack(_index);
     }

    }
}

That's all. Sorry for eventually mistakes. Written in browser. Not tested.

Konrad
Thanks for your reply. The problem really is that I need to be able to compare the button clicked to a number held for the audio clip. Then I can see if they clicked the correct answer. I'm sure OOP is the way to go but i have not done much of it in flash. That the best option you reckon?
whamo