Dictation Machine Final
/**
* Copyright andrewexex886 ( http://wonderfl.net/user/andrewexex886 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cdcv
*/
// forked from andrewexex886's XML: Dictation Machine Beta V2
// forked from andrewexex886's Dictation Machine Beta V2
package {
import flash.net.URLLoader;
import flash.display.Loader;
import flash.system.Security;
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 Cbutt: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();
private var CoursePanel:Sprite;
private var wrongTimes:int=0;
//XML
private var SelectedCourseName:Array=[];
private var SelectedCourseTotal:int;
private var AllCourseName:Array=[];
private var AllCourseCB:Array=[];
private var AllCourseTotal:int;
private var VocabXML:XML;
private var VocabTotal:int;
//custom function
private var CF_RandomVocab:checkbox_t;
private var CF_SAF:checkbox_t;
private var CF_CtEMode:checkbox_t;
private var CF_withANS:checkbox_t;
public function FlashTest() {
if (stage) {
Security.allowDomain('www.gmodules.com');
var l:Loader = new Loader();
l.load(new URLRequest(proxy(loaderInfo.url)));
addChild(l);
return;
} else {
Security.allowDomain('swf.wonderfl.net');
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function proxy(url:String):String {
return 'http://www.gmodules.com/ig/proxy?url=' + encodeURIComponent(url);
}
private function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
var ul:URLLoader = new URLLoader(new URLRequest(proxy('http://dl.dropbox.com/u/7334821/Dictation_Machine_Vocabulary_2011-11-02.xml')));
ul.addEventListener(Event.COMPLETE, init2);
}
private function init2(e:Event=null):void{
fStage=stage;
//inittrace(stage);
//trace("start");
VocabXML=XML(e.target.data);
//custom function random,C>E mode, with ans
CF_RandomVocab= new checkbox_t("Random",false);
CF_SAF= new checkbox_t("speak after fail",true);
CF_CtEMode= new checkbox_t("swap Chi/Eng",false);
CF_withANS= new checkbox_t("with answer",false);
CF_SAF.x=CF_withANS.x=100;
CF_RandomVocab.y=CF_SAF.y=100;
CF_CtEMode.y=CF_withANS.y=120;
CF_RandomVocab.buttonMode=true;
CF_SAF.buttonMode=true;
CF_CtEMode.buttonMode=true;
CF_withANS.buttonMode=true;
addChild(CF_RandomVocab);
addChild(CF_SAF);
addChild(CF_CtEMode);
addChild(CF_withANS);
CF_RandomVocab.addEventListener(MouseEvent.CLICK,function():void{CF_RandomVocab.toggle();});
CF_SAF.addEventListener(MouseEvent.CLICK,function():void{CF_SAF.toggle();});
CF_withANS.addEventListener(MouseEvent.CLICK,function():void{CF_withANS.toggle();});
CF_CtEMode.addEventListener(MouseEvent.CLICK,function():void{CF_CtEMode.toggle();});
//start butt
Cbutt=new Sprite();
Cbutt.graphics.beginFill(0,1);;
Cbutt.graphics.drawRect(0,0,50,50);
Cbutt.graphics.endFill();
Cbutt.x=300;
Cbutt.y=100;
Cbutt.buttonMode=true;
addChild(Cbutt);
Cbutt.addEventListener(MouseEvent.CLICK,updateCourseVocab);
//Course part
CoursePanel=new Sprite();
addChild(CoursePanel);
getCourseVocab();
CoursePanelsetup();
//vocab part
//XML setup
XML_vocab_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=300;
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;
butt.visible=false;
addChild(butt);
butt.addEventListener(MouseEvent.CLICK,postAnsButt);
stage.addEventListener(KeyboardEvent.KEY_DOWN,postAnsKeyboard);
updateCourseVocab();
}
private function postAnsButt(E:MouseEvent=null):void{
if(checkAnswer(tf.text)){
if(CF_RandomVocab.getstate()){
//randomGetVocal();
DRandomGetVocal();
}else{
orderGetVocal();
}
if(!CF_SAF.getstate()){
mySound.play();
}
}else{
tf2.appendText("X");
mySound.play();
}
if(CF_withANS.getstate()){
tf.text=vocalLibE[nowVocalNum];
}else{
tf.text="";
}
}
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 DRandomGetVocal():void{
//Drop
vocalLibE.splice(nowVocalNum,1);
vocalLibC.splice(nowVocalNum,1);
vocalLibS.splice(nowVocalNum,1);
vocalLibLenght=vocalLibE.length;
//
if(vocalLibLenght != 0){
nowVocalNum=Math.round(Math.random() * (vocalLibLenght-1));
tf2.text=vocalLibC[nowVocalNum];
playmp3(vocalLibS[nowVocalNum]);
}else{
tf2.text="END";
tf.text="";
playmp3("");
}
}
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_vocab_to_Array():void{
VocabTotal=VocabXML.Vocabulary.length();
vocalLibE=[""];
vocalLibC=["點擊Enter重新開始"];
vocalLibS=[""];
var k:int=0;
if(CF_CtEMode.getstate()){
for(k=0;k<VocabTotal;k++){
if(check_Selected_Course(VocabXML.Vocabulary.Course[k])){
vocalLibC.push(VocabXML.Vocabulary.Eng[k]);
vocalLibE.push(VocabXML.Vocabulary.Chi[k]);
vocalLibS.push(VocabXML.Vocabulary.Sou[k]);
}
}
}else{
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;
}
private function getCourseVocab(e:MouseEvent=null):void{
VocabTotal=VocabXML.Vocabulary.length();
AllCourseName=[];
var k:int=0;
var oi:int=0;
var vocabCount:int=0;
var lastCourse:String="";
for(k=0;k<VocabTotal;k++){
if(VocabXML.Vocabulary.Course[k] != lastCourse){
lastCourse=VocabXML.Vocabulary.Course[k];
AllCourseName.push(VocabXML.Vocabulary.Course[k]);
AllCourseTotal=AllCourseName.length;
AllCourseCB.push(new checkbox_t(VocabXML.Vocabulary.Course[k],false));
AllCourseCB[oi].updatetext(VocabXML.Vocabulary.Course[k-1]+":"+vocabCount);
oi=AllCourseTotal-1;
AllCourseCB[oi].buttonMode=true;
AllCourseCB[oi].x=200*Math.floor((oi)/15);
AllCourseCB[oi].y=150+(oi%15)*20;
//trace(oi+" "+AllCourseCB[oi].x+" "+AllCourseCB[oi].y);
CoursePanel.addChild( AllCourseCB[oi]);
AllCourseCB[oi].addEventListener(MouseEvent.CLICK,CBClick);
vocabCount=0;
}
vocabCount++;
}
function CBClick(e:MouseEvent):void{
e.target.parent.toggle();
}
AllCourseCB[oi].updatetext(VocabXML.Vocabulary.Course[k-1]+":"+vocabCount);
}
private function updateCourseVocab(e:MouseEvent=null):void{
var i:int=0;
SelectedCourseName=[];
while(i<AllCourseTotal){
if(AllCourseCB[i].getstate()){
SelectedCourseName.push(AllCourseName[i]);
}
i++;
}
SelectedCourseTotal=SelectedCourseName.length;
nowVocalNum=0;
//XML setup
XML_vocab_to_Array();
//set Length
vocalLibLenght=vocalLibE.length;
postAnsButt();
}
private function CoursePanelsetup():void{
var leftbutt:Sprite=new Sprite();
leftbutt.graphics.beginFill(0,1);
leftbutt.graphics.drawRect(0,455,100,10);
leftbutt.graphics.endFill();
addChild(leftbutt);
leftbutt.buttonMode=true;
var rightbutt:Sprite=new Sprite();
rightbutt.graphics.beginFill(0,1);
rightbutt.graphics.drawRect(365,455,100,10);
rightbutt.graphics.endFill();
addChild(rightbutt);
rightbutt.buttonMode=true;
leftbutt.addEventListener(MouseEvent.CLICK,leftpage);
rightbutt.addEventListener(MouseEvent.CLICK,rightpage);
function leftpage(e:MouseEvent=null):void{
CoursePanel.x+=200;
}
function rightpage(e:MouseEvent=null):void{
CoursePanel.x-=200;
}
}
}
}
import flash.text.TextField;
import flash.display.Sprite;
class checkbox_t extends Sprite{
public var togglestate:Boolean=false;
private var buttback:Sprite;
private var buttup:Sprite;
private var textf:TextField;
public function checkbox_t(butttext:String="",startupboolean:Boolean=false){
togglestate=startupboolean;
buttback=new Sprite;
buttback.graphics.beginFill(0,1);
buttback.graphics.drawRect(0,0,16,16);
buttback.graphics.endFill();
addChild(buttback);
buttup=new Sprite;
buttup.graphics.beginFill(0xFFFFFF,1);
buttup.graphics.drawRect(1,1,14,14);
buttup.graphics.endFill();
addChild(buttup);
textf=new TextField;
textf.x=18;
textf.y=-2;
textf.width=200;
textf.selectable=false;
textf.text=butttext;
addChild(textf);
buttup.visible=!togglestate;
}
public function toggle():Boolean{
buttup.visible=togglestate;
togglestate=!togglestate;
return togglestate;
}
public function getstate():Boolean{
return togglestate;
}
public function updatetext(gettext:String=null):void{
if(gettext){textf.text=gettext;}
}
}
///// WONDERFL TRACE /////
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
function inittrace(s:Stage):void
{
WTrace.initTrace(s);
}
//global trace function
var trace:Function;
//wtreace class
class WTrace
{
private static var FONT:String = "Fixedsys";
private static var SIZE:Number = 12;
private static var TextFields:Array = [];
private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void
{
trace_stage = stg;
trace = wtrace;
}
private static function scrollup():void
{
// maximum number of lines: 100
if (TextFields.length > 100)
{
var removeme:TextField = TextFields.shift();
trace_stage.removeChild(removeme);
removeme = null;
}
for(var x:Number=0;x<TextFields.length;x++)
{
(TextFields[x] as TextField).y -= SIZE*1.2;
}
}
public static function wtrace(... args):void
{
var s:String="";
var tracefield:TextField;
for (var i:int;i < args.length;i++)
{
// imitating flash:
// putting a space between the parameters
if (i != 0) s+=" ";
s+=args[i].toString();
}
tracefield= new TextField();
tracefield.autoSize = "left";
tracefield.text = s;
tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE);
tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);
scrollup();
TextFields.push(tracefield);
}
}