2009-10-12 49 views
0

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

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

当然目前这个获取影片剪辑索引的函数只是获取生成的最后一个rand值。我需要一种方法将某种id嵌入到生成的影片剪辑中。然后,我可以简单地测试它是否与声音片段索引相同。然后,我会为每个问题做相同的处理(共10个)

希望是明确的,有人可以帮忙。 很多谢谢

回答

0

我不明白你的问题。你是你的代码的老板。只要我们面向对象 - 有这样做的真的很多方面吧:)

好,最简单的一个:

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

    } 
} 

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

+0

感谢您的回复。问题的确是我需要能够将点击的按钮与音频剪辑的数字进行比较。然后我可以看到他们是否点击了正确的答案。我确信OOP是一条可行的路,但我在闪光灯中没有做太多工作。你认为最好的选择? – whamo 2009-10-12 18:05:42