Dictation Machine Beta_clean

by andrewexex886 forked from forked from: Dictation Machine Beta (diff: 609)
♥0 | Line 129 | Modified 2011-09-26 12:57:05 | MIT License
play

ActionScript3 source code

/**
 * Copyright andrewexex886 ( http://wonderfl.net/user/andrewexex886 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tYXA
 */

// forked from andrewexex886's forked from: Dictation Machine Beta
// forked from andrewexex886's Dictation Machine Beta
package {
    import flash.net.URLRequest;
    import flash.media.Sound;
    import flash.text.TextFormat;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.display.Sprite;
    import flash.ui.Keyboard;
    public class FlashTest extends Sprite {
        private var butt:Sprite;
        private var tf:TextField;
        private var tf2:TextField;
        private var vocalLibE:Array=[""];
        private var vocalLibC:Array=["點擊Enter開始"];
        private var vocalLibS:Array=[""];
        private var vocalLibLenght:int;
        private var nowVocalNum:int=0;
        private var fStage:Stage;
        private var mySound:Sound = new Sound();
        //XML
        private var SelectedCourseName:Array;
        private var SelectedCourseTotal:int;
        private var VocabXML:XML;
        private var VocabTotal:int;
        public function FlashTest() {
                fStage=stage;
                //XML Data
                SelectedCourseName=["A_life_as_hard_as_stone"];
                //SelectedCourseName=["long_term_vision","Anthropology","Ashes","Baby","Blind","Bulletin","Beauty","Binding"];
                SelectedCourseTotal=SelectedCourseName.length;
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            VocabXML=;
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                //XML setup                
                XML_to_Array();
                //set Length
                vocalLibLenght=vocalLibE.length;
                //setup textField                      
                tf = new TextField();
                this.addChild(tf);
                tf.type=TextFieldType.INPUT;
                tf.border=true;
                tf.y=50;
                tf.width=200;
                tf.height=40;
                tf2 = new TextField;
                tf2.text=vocalLibC[0];
                tf2.selectable=false;
                tf2.width=200;
                tf2.height=40;
                this.addChild(tf2);
                var txtF:TextFormat= new TextFormat("Arial",30);
                //tf.defaultTextFormat=txtF;   
                tf.defaultTextFormat=txtF;
                tf2.defaultTextFormat=txtF;
                tf2.setTextFormat(txtF);
                //Button
                butt=new Sprite();
                butt.graphics.beginFill(0,1);;
                butt.graphics.drawRect(0,0,50,50);
                butt.graphics.endFill();
                butt.x=300;
                butt.buttonMode=true;
                addChild(butt);
                butt.addEventListener(MouseEvent.CLICK,postAnsButt);
                stage.addEventListener(KeyboardEvent.KEY_DOWN,postAnsKeyboard);
        }
        private function postAnsButt(E:MouseEvent=null):void{
            if(checkAnswer(tf.text)){
                //randomGetVocal();
                orderGetVocal();
            }else{
                tf2.appendText("X");
                }
            tf.text="";
            mySound.play();            
        }
        private function postAnsKeyboard(E:KeyboardEvent=null):void{
            if(E.keyCode==Keyboard.ENTER){
                postAnsButt();
            }else if(E.keyCode==Keyboard.F1){
                tf.text=vocalLibE[nowVocalNum];
                }
        }
        private function randomGetVocal():void{
            nowVocalNum=Math.round(Math.random() * (vocalLibLenght-1));
            tf2.text=vocalLibC[nowVocalNum];
        }
        private function orderGetVocal():void{
            if(nowVocalNum>=(vocalLibLenght-1)){
                nowVocalNum=0;
            }else{
            nowVocalNum++;
            }
            tf2.text=vocalLibC[nowVocalNum];
            playmp3(vocalLibS[nowVocalNum]);
        }
        private function checkAnswer(ans:String):Boolean{
            if(ans==vocalLibE[nowVocalNum]){
                return true;
            }
            return false;
        }
        private function playmp3(mp3url:String=null):void{
            mySound = new Sound();
            var request:URLRequest = new URLRequest(mp3url);
            mySound.addEventListener(Event.ID3, Id3Handler);
            mySound.load(request);
            mySound.play();
        }
        private function Id3Handler(event:Event):void {
            trace(String("播放mp3:"+mySound.id3.artist));
        }
        private function XML_to_Array():void{
            VocabTotal=VocabXML.Vocabulary.length();
            var k:int=0;
            for(k=0;k<VocabTotal;k++){
              if(check_Selected_Course(VocabXML.Vocabulary.Course[k])){
                vocalLibE.push(VocabXML.Vocabulary.Eng[k]);
                vocalLibC.push(VocabXML.Vocabulary.Chi[k]);
                vocalLibS.push(VocabXML.Vocabulary.Sou[k]);
              }
            }
        }
        private function check_Selected_Course(incourse:String):Boolean{
            var i:int=0;
            while(i<SelectedCourseTotal){
               if(incourse==SelectedCourseName[i]){
                    return true;
               }
               i++;
            }
            return false;
        }
    }
}

Forked