播放随机声音并匹配影片剪辑测验AS3,CS4

时间:2009-10-12 12:52:38

标签: flash actionscript-3 flash-cs4

我正在创建一个测验,用户将对象(影片剪辑)与播放的声音片段进行匹配。声音存储在一个数组中,并选择一个随机的声音。然后动态创建4个随机影片剪辑,用于保存对象图像。我需要一种方法将声音片段链接到影片剪辑,以检查是否单击了正确的片段。这是迄今为止的代码:

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);
}

当然,此功能获取影片剪辑的索引只是获取生成的最后一个兰特数。我需要一种方法来将某种id嵌入到生成的影片剪辑中。然后,我可以简单地测试它是否与声音剪辑索引相同。然后我将对每个问题(总共10个)做同样的事情

希望很清楚,有人可以提供帮助。 非常感谢

1 个答案:

答案 0 :(得分:0)

我不明白你的问题。你是代码的老板。只是我们OOP - 有很多方法可以做到这一点:)

好的,最简单的一个:

// 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);
        }

    }
}

这就是全部。对不起最终的错误。写在浏览器中。未经测试。

相关问题