开发者

Play random sound and match to movie clip quiz AS3, CS4

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 movi开发者_开发知识库e 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜