2009-10-12 50 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