タイピングゲーム超簡易版

by otherone

♥0 | Line 231 | Modified 2010-09-15 16:25:38 | MIT License
play

ActionScript3 source code

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

/*
タイピングゲーム超簡易版
*/

package {
    import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;
    /**
     * ...
     * @author 
     */

    public class Main extends MovieClip {
        //ステージサイズ
        private var w:uint = stage.stageWidth;
        private var h:uint = stage.stageHeight;
        private var qList:Array = [];

        private var KeyName:Array;
        
        private var questionTxt:TextField = new TextField;
        private var ancerTxt:TextField= new TextField;
        private var ancTxt:String = '';
        
        private var qNum:int = 0;
        private var aNum:int=0;
        private var qlen:int;
        private var qmax:int;
        private var format:TextFormat;
        private var missNum:int;
        private var opn:Sprite;
        private var main:Main;
        private var startFlg:Boolean;
        
        private var chktxt:TextField;

        public function Main() {

            KeyName =
            ["" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "A" ,
            "B" ,
            "C" ,
            "D" ,
            "E" ,
            "F" ,
            "G" ,
            "H" ,
            "I" ,
            "J" ,
            "K" ,
            "L" ,
            "M" ,
            "N" ,
            "O" ,
            "P" ,
            "Q" ,
            "R" ,
            "S" ,
            "T" ,
            "U" ,
            "V" ,
            "W" ,
            "X" ,
            "Y" ,
            "Z" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" ,
            "" 
            ];

            
            //openning
            opn= new Sprite();
            addChild(opn);
            var g:Graphics = opn.graphics;
            g.beginFill(0xFFFFFF, 1.0);// 単一色のスタイル設定
            g.drawRect(0, 0, w, h);// 矩形を描画
                
            //textFormat
            format = new TextFormat();
            format.size = 14;        // 文字のポイントサイズ
            format.color = 0xFF0000;    // 文字の色
            
            qList=["AIUEO","KAKIKUKEKO","SASHISUSESO","TACHITSUTETO"];//出題
            
            qmax = qList.length;//問題の最大値
            
            //opn.addEventListener(MouseEvent.CLICK,fn_click);
            //opn.buttonMode = true;
            
            //キーイベントフォーカス固定用、
            var focustxt:TextField=new TextField 
            focustxt.width = w;//横幅
            focustxt.height = h;//
            focustxt.x=focustxt.y = 0;
            focustxt.selectable = false;
            addChild(focustxt);
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
            
            
            chktxt=new TextField;             
            chktxt.text='EnterKeyでスタート';
            chktxt.x=w/2-(chktxt.width/2);
            chktxt.y=10;
            addChild(chktxt);
            }
        
        private function init():void {    
            startFlg=true
            questionTxt.width = 250;//横幅
            questionTxt.height = 20;//
            questionTxt.border = true;
            questionTxt.text = "出題1 :"+qList[qNum];
            questionTxt.x=w/2-(questionTxt.width/2);
            questionTxt.y = 100;
            questionTxt.selectable = false;
            questionTxt.setTextFormat(format);
            addChild(questionTxt);

            qlen = qList[qNum].length;

            ancerTxt.width = 250;//横幅
            ancerTxt.height = 20;//
            ancerTxt.border = true;
            ancerTxt.text = "入力文字がココに表示されます";
            ancerTxt.x=w/2-(ancerTxt.width/2);;
            ancerTxt.y = 200;
            ancerTxt.selectable = false;
            ancerTxt.setTextFormat(format);
            addChild(ancerTxt);            
        }
        
        
        private function KeyDown(e:KeyboardEvent):void {
            trace(e.keyCode);
            var input:String = inputString(e.keyCode);//入力したアルファベット
            var correctFlg:Boolean = codeCheck(qList[qNum], aNum, e.keyCode);//正解判定
            //trace(correctFlg);
            
            if (startFlg) {
                //出題
                if (correctFlg) {
                //trace("正解")
                chktxt.text='正解';
                ancTxt = ancTxt + input;
                if (aNum<qlen-1) {
                    aNum++;
                } else {
                    if (qmax-1>qNum) {
                        //
                        dataShift();//次の問題をセット
                    }else{
                        chktxt.text='全問終了';
                        }    
                }
            } else {
                missNum++
                //trace("ミス",missNum)
                chktxt.text='ミス!!';
            }
            ancerTxt.text = ancTxt;
            }else {
                //スタート前
                //trace("start!")
                chktxt.text='スタート';
                removeChild(opn); 
                init();
            }
            
        }
        
        private function dataShift():void {
            aNum = 0;
            qNum++;
            ancTxt = '';
            qlen = qList[qNum].length;
            questionTxt.text = "出題:"+(qNum+1)+" " + qList[qNum];
            ancerTxt.text = "";
        }
    
    

        
        public function inputString(code:int):String
        {
            return KeyName[code];
        }
        

        public function codeCheck(q:String, ancNum:int, code:int):Boolean
        {
            //trace("q=",q,"anc=",ancNum)
            
            var qtxt:String=q.substring(ancNum,ancNum+1)
            var cktxt:String=KeyName[code]
            var flg:Boolean
            
            if (qtxt == cktxt){
                flg = true;
                }else
                {
                flg = false;
                }
                
            return flg;
        }
    
    }
}