/**
* Copyright andrewexex886 ( http://wonderfl.net/user/andrewexex886 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2LoR
*/
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 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() {
fStage=stage;
// inittrace(stage);
//trace("start");
VocabXML=<VocabularyLibrary>
<Vocabulary>
<Course>long_term_vision</Course>
<Eng>struggle</Eng>
<Chi>艱難</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/s/st/str/struggle%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>long_term_vision</Course>
<Eng>ultimate</Eng>
<Chi>極限的</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/u/ul/ult/ultimate%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>long_term_vision</Course>
<Eng>patience</Eng>
<Chi>恆心</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/p/pa/pat/patience%23_gb_1.mp3</Sou>
</Vocabulary>\
<Vocabulary>
<Course>Anthropology</Course>
<Eng>anthropology</Eng>
<Chi>人類學</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/a/an/ant/anthropology%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Anthropology</Course>
<Eng>anthropologist</Eng>
<Chi>人類學家</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/a/an/ant/anthropologist%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Anthropology</Course>
<Eng>mongolia</Eng>
<Chi>蒙古</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/de/0/!Mongolia.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Ashes</Course>
<Eng>ash</Eng>
<Chi>骨灰</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/a/as/ash/ash%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Ashes</Course>
<Eng>indifferent</Eng>
<Chi>漠不關心</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/i/in/ind/indifferent%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Ashes</Course>
<Eng>incredibly</Eng>
<Chi>難以置信地</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/i/in/inc/incredibly%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Baby</Course>
<Eng>pregnancy</Eng>
<Chi>懷孕</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/p/pr/pre/pregnancy%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Baby</Course>
<Eng>carry</Eng>
<Chi>拿</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/lf/0/c/ca/car/carry%23_gb_1.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>Baby</Course>
<Eng>nappies</Eng>
<Chi>尿布</Chi>
<Sou>http://www.gstatic.com/dictionary/static/sounds/de/0/nappy.mp3</Sou>
</Vocabulary>
<Vocabulary>
<Course>World_of_difference_on_right_of_expression</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c1</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c2</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c3</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c4</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c5</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c6</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c7</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c8</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c9</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c10</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c11</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c12</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c13</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c14</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>c15</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d1</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d2</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d3</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d4</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d5</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d6</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d7</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d8</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d9</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d10</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d11</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d12</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d13</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d14</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
<Vocabulary>
<Course>d15</Course>
<Eng>c2vocab</Eng>
<Chi>c2 chi</Chi>
<Sou>c2s</Sou>
</Vocabulary>
</VocabularyLibrary>
;
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();
//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();});
//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);
}
private function leftpage(e:MouseEvent=null):void{
CoursePanel.x+=200;
}
private 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);
}
}