package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.*;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
/**
* ...
* @author ...
*/
public class dumiao extends Sprite
{
public var time:Timer;
public var input:TextField;
public var box:MovieClip;
public var gameStart:Boolean;
public var count:int = 0;
public function dumiao()
{
time = new Timer(1000);
gameStart = false;
input.restrict = "0-9";
box.stop();
box.addEventListener(MouseEvent.CLICK, clickHandler);
time.addEventListener(TimerEvent.TIMER, updateTimeHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN,enterHandler);
}
public function enterHandler(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.ENTER)
{
if (int(input.text) == count)
{
//trace("you win");
input.text = "you win";
input.type = TextFieldType.DYNAMIC;
stage.focus = null;
}
else
{
//trace("you lose");
input.text = "you lose";
input.type = TextFieldType.DYNAMIC;
stage.focus = null;
}
}
}
public function updateTimeHandler(e:TimerEvent):void
{
//input.text = String(time.currentCount);
if (time.currentCount == count)
{
time.stop();
box.gotoAndStop(2);
input.text = "";
stage.focus = input;
input.type = TextFieldType.INPUT;
box.addEventListener(MouseEvent.CLICK, clickHandler);
}
}
public function clickHandler(e:MouseEvent):void
{
gameStart = !gameStart;
trace(gameStart);
if (gameStart)
{
time.reset();
stratGame();
}
else
{
box.gotoAndStop(1);
input.text = "点击方块";
}
}
public function stratGame():void
{
count = int(Math.random() * 10 + 10);
trace(count);
time.start();
input.text = "计时开始";
input.type = TextFieldType.DYNAMIC;
box.removeEventListener(MouseEvent.CLICK, clickHandler);
}
}
}