儿子不背乘法口诀肿么办
儿子二年级了,该背乘法口诀了。可这小子迷上了植物大战僵尸2,一点也不配合。木有办法,只好写了个小东东,给儿子练习。这是原型,后续完善中……
不上代码似乎就不对了哦。
package org.sunshine.game { import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TextEvent; import flash.media.Sound; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFieldType; import flash.text.TextFormat; public class CalcuBoy extends Sprite { public static const STATE_SHOW_TITLE:int = 10; public static const STATE_TITLE_SHOWING:int = 15; public static const STATE_INIT:int = 20; public static const STATE_SHOW_QUESTION:int = 30; //显示题目 public static const STATE_WAIT_ANSWER:int = 35; public static const STATE_SHOW_LEVEL:int = 40; public static const STATE_FEEDBACK_RIGHT:int = 45; //显示反馈【正确】 public static const STATE_FEEDBACK_ERROR:int = 46; //显示反馈【错误】 public static const STATE_SHOW_END:int = 50; public static const STATE_GAME_OVER:int = 60; public static const STATE_WAIT:int = 100; public var bg:MovieClip; public var titleMc:MovieClip; public var flag:MovieClip; public var gameState:int = 0; public var level:int = 2; public var score:int = 0; public var errArray:Array = new Array(); public var soundRight:Sound = new RightSound(); public var soundError:Sound = new ErrorSound(); //乘数和被乘数 public var tfNumber1:TextField = new TextField(); public var tfNumber2:TextField = new TextField(); public var tfAnswer:TextField = new TextField(); //记分板 public var sb:Scoreboard = new Scoreboard(); //对号 public var rightFeedbackMc:MovieClip = new RightMc(); public function CalcuBoy() { bg = new BackGround(); addChild(bg); addEventListener(Event.ENTER_FRAME,gameLoop); gameState = CalcuBoy.STATE_SHOW_TITLE; } public function gameLoop( e:Event ):void{ switch( gameState ) { case CalcuBoy.STATE_SHOW_TITLE: showTitle(); break; case CalcuBoy.STATE_TITLE_SHOWING: break; case CalcuBoy.STATE_INIT: initGame(); break; case CalcuBoy.STATE_SHOW_QUESTION: showQuestion(); break; case CalcuBoy.STATE_WAIT_ANSWER: break; case CalcuBoy.STATE_FEEDBACK_RIGHT: feedbackRight(); break; case CalcuBoy.STATE_FEEDBACK_ERROR: feedbackError(); break; } } public function feedbackError():void{ soundError.play(); tfAnswer.text = ""; stage.focus = tfAnswer; gameState = CalcuBoy.STATE_WAIT_ANSWER; } public function feedbackRight():void{ soundRight.play(); score ++; //show score sb.update({score:score}); //level up if(score>=(10 + level*2)){ level ++; score = 1; //show level sb.update({level:level,score:score}); if(level>9)level=9; } //show right feedback rightFeedbackMc.visible = true; rightFeedbackMc.gotoAndPlay(0); rightFeedbackMc.addEventListener(Event.ENTER_FRAME,onFeedbackRight); gameState = CalcuBoy.STATE_WAIT; } public function onFeedbackRight( e:Event ):void{ if( rightFeedbackMc.currentFrame >= rightFeedbackMc.totalFrames){ rightFeedbackMc.visible = false; rightFeedbackMc.removeEventListener(Event.ENTER_FRAME,onFeedbackRight); gameState = CalcuBoy.STATE_SHOW_QUESTION; } } public function showQuestion():void{ tfNumber1.text = level.toString(); tfNumber2.text = (Math.floor(Math.random()*9)+1).toString(); trace("show question: " + tfNumber1.text + " × " + tfNumber2.text); tfAnswer.text = ""; stage.focus = tfAnswer; gameState = CalcuBoy.STATE_WAIT_ANSWER; } public function showTitle():void{ titleMc = new GameTitle(); titleMc.x = (stage.stageWidth - titleMc.width)/2; titleMc.y = (stage.stageHeight - titleMc.height)/2 - titleMc.height; addChild(titleMc); stage.addEventListener(MouseEvent.CLICK,onEnterGame); gameState = CalcuBoy.STATE_TITLE_SHOWING; } public function onEnterGame( e:MouseEvent ){ gameState = CalcuBoy.STATE_INIT; removeChild(titleMc); stage.removeEventListener(MouseEvent.CLICK,onEnterGame); } public function initGame():void{ soundRight.play(); addChild(sb); rightFeedbackMc.x = 360; rightFeedbackMc.y = 150; addChild(rightFeedbackMc); rightFeedbackMc.visible = false; var tft:TextFormat = new TextFormat(); tft.size = 60; tft.color = 0x00FFFF; tfNumber1.defaultTextFormat = tft; tfNumber2.defaultTextFormat = tft; tfAnswer.defaultTextFormat = tft; tfNumber1.text = "?"; tfNumber2.text = "?"; tfAnswer.text = "?"; tfNumber1.x = 140; tfNumber1.y = 150; addChild(tfNumber1); tfNumber2.x = 260; tfNumber2.y = 150; addChild(tfNumber2); tfAnswer.x = 360; tfAnswer.y = 150; tfAnswer.type = TextFieldType.INPUT; tfAnswer.restrict = "0-9"; tfAnswer.autoSize = TextFieldAutoSize.LEFT; tfAnswer.maxChars = 2; addChild(tfAnswer); //tfAnswer.border = true; stage.focus = tfAnswer; tfAnswer.setSelection(0,1); tfAnswer.addEventListener(Event.CHANGE,onAnswer); //乘号 var timeImage:MovieClip = new TimeImg(); timeImage.x = 200; timeImage.y = 170; addChild(timeImage); //等号 var eqlImage:MovieClip = new EqlImg(); eqlImage.x = 310; eqlImage.y = 170; addChild(eqlImage); gameState = CalcuBoy.STATE_SHOW_QUESTION; } public function onAnswer( e:Event ):void { if( gameState == CalcuBoy.STATE_WAIT_ANSWER ) { var n1 = parseInt(tfNumber1.text); var n2 = parseInt(tfNumber2.text); var asr = parseInt(tfAnswer.text); trace("Right answer:" + n1*n2); trace("Your answer: " + asr); if(n1*n2 == asr){ trace("u r right."); gameState = CalcuBoy.STATE_FEEDBACK_RIGHT; }else { if( tfAnswer.text.length == String(n1*n2).length ){ var fml:Formula = new Formula(n1,n2); errArray.push(fml); trace("Error formula pushed in errArray."); gameState = CalcuBoy.STATE_FEEDBACK_ERROR; } } } } //trace 显示列表 public function traceDisplayList( container:DisplayObjectContainer,intentString:String="" ):void { var child:DisplayObject; for( var i:int=0;i<container.numChildren;i++){ child = container.getChildAt(i); trace(intentString,child,child.name); if(child is DisplayObjectContainer){ traceDisplayList(DisplayObjectContainer(child),intentString + " "); } } } } }
比较简单,游戏状态切换也直接用了switch,后续在美化界面的同时,重构掉。