Flash原创:纯代码打造“反应测试游戏”【AS 3.0】

4 篇文章 0 订阅
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.filters.BlurFilter;

var radius:Number = new Number(100);
var timeArr:Array = new Array("无");
var times:Number = 0;
var curEllipse:MovieClip;
var UI_mc:MovieClip;
var about_mc:MovieClip;
var game_mc:MovieClip;
var over_mc:MovieClip;
var end_mc:MovieClip;
var hit:MovieClip;
var bg:MovieClip;
var process:Boolean = false;
var cTimer:Timer;
var wornTimes:Number = 0;
var sumTimes:Number = 30;
var mark:Number = 0;
main();

function createEllipse()
{
	var ellipse:MovieClip = new MovieClip  ;

	var x_:Number = new Number(150);
	var y_:Number = new Number(stage.stageHeight - 100);

	ellipse.graphics.beginFill(0x006600,1);
	ellipse.graphics.drawCircle(0,0,radius);
	ellipse.graphics.endFill();

	curEllipse = ellipse;
	curEllipse.x = x_;
	curEllipse.y = y_ - 50;
}
function createButton(txt:String,color=0x0066FF):MovieClip
{
	var rect:MovieClip = new MovieClip  ;
	var rect2:MovieClip = new MovieClip  ;
	var btnText:TextField = new TextField  ;
	var btnTextFormat:TextFormat = new TextFormat(null,30,0xFFFFFF);
	var shadowFilter1:DropShadowFilter = new DropShadowFilter(3);
	var shadowFilter2:DropShadowFilter = new DropShadowFilter(5);
	var w:Number = new Number(150);
	var h:Number = new Number(60);

	rect.graphics.beginFill(color,1);
	rect.graphics.drawRect(0,0,w,h);
	rect.filters = [shadowFilter2];

	rect2.graphics.beginFill(0x6600FF,1);
	rect2.graphics.drawRect(0,0,w,h);
	rect2.visible = false;

	btnText.autoSize = "center";
	btnText.width = w;
	btnText.x = w / 2 - 4;
	btnText.y = (h - 34) / 2 - 3;
	btnText.text = txt;
	btnText.selectable = false;
	btnText.filters = [shadowFilter1];
	btnText.setTextFormat(btnTextFormat);

	var myButton:MovieClip = new MovieClip  ;
	myButton.addChild(rect);
	myButton.addChild(rect2);
	myButton.addChild(btnText);

	myButton.addEventListener(MouseEvent.ROLL_OVER,function(e:MouseEvent){rect2.visible=true;});
	myButton.addEventListener(MouseEvent.ROLL_OUT,function(e:MouseEvent){rect2.visible=false;});

	return myButton;
}
function createUI()
{
	var titleText:TextField = new TextField  ;
	var titleTextFormat:TextFormat = new TextFormat(null,60,0x66FF00);
	var shadowFilter:DropShadowFilter = new DropShadowFilter(5);
	var startBtn:MovieClip = createButton("Play Game",0x009900);
	var aboutBtn:MovieClip = createButton("About");

	titleText.autoSize = "center";
	titleText.text = "反应测试";
	titleText.x = stage.stageWidth / 2 - 40;
	titleText.y = 80;
	titleText.selectable = false;
	titleText.filters = [shadowFilter];
	titleText.setTextFormat(titleTextFormat);

	startBtn.x = 50;
	startBtn.y = stage.stageHeight - 150;
	aboutBtn.x = stage.stageWidth - aboutBtn.width - 50;
	aboutBtn.y = stage.stageHeight - 150;

	UI_mc = new MovieClip  ;
	UI_mc.addChild(titleText);
	UI_mc.addChild(startBtn);
	UI_mc.addChild(aboutBtn);

	aboutBtn.addEventListener(MouseEvent.CLICK,function(e){turning(UI_mc,about_mc);});
	startBtn.addEventListener(MouseEvent.CLICK,function(e){turning(UI_mc,game_mc);});
	addChild(UI_mc);
}
function createAbout()
{
	var titleText:TextField = new TextField  ;
	var titleTextFormat:TextFormat = new TextFormat(null,20,0x000000);
	var backBtn:MovieClip = createButton("Back",0xFF0000);

	titleText.autoSize = "left";
	titleText.text = "制作:正弦前锋;\n来自:Flash吧_百度贴吧;\n说明:本游戏是一款反应测试游戏。进入游戏后请玩家做好准备,随机秒数后将出现一个绿色圆形,出现后立即按空格键,系统将自动计算出玩家的反应时间。一共需要测试" + String(sumTimes) + "次。如果未出现图形就按键,予以警告并重做本次测试。警告超过三次,游戏自动退出。";
	titleText.x = 30;
	titleText.y = 70;
	titleText.width = stage.stageWidth - 80;
	titleText.wordWrap = true;
	titleText.multiline = true;
	titleText.selectable = false;
	titleText.setTextFormat(titleTextFormat);

	backBtn.x = (stage.stageWidth-backBtn.width)/2;
	backBtn.y = stage.stageHeight - 120;

	about_mc = new MovieClip  ;
	about_mc.addChild(titleText);
	about_mc.addChild(backBtn);
	about_mc.alpha = 0;
	about_mc.visible = false;

	backBtn.addEventListener(MouseEvent.CLICK,function(e){turning(about_mc,UI_mc);});
	addChild(about_mc);
}
function createBG()
{
	bg = new MovieClip  ;
	hit = new MovieClip  ;
	bg.graphics.beginFill(0xFFFFFF,1);
	bg.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
	hit.graphics.beginFill(0xFFFFFF,0);
	hit.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);

	addChild(bg);
	addChild(hit);
	setHit(false);
}
function createGame()
{
	var numText1:TextField = new TextField  ;
	var timeText1:TextField = new TextField  ;
	var numText2:TextField = new TextField  ;
	var timeText2:TextField = new TextField  ;
	var infoText:TextField = new TextField  ;
	var regularFormat:TextFormat = new TextFormat(null,20,0x000000);
	var infoFormat:TextFormat = new TextFormat(null,20,0x009900);
	var infoFormat2:TextFormat = new TextFormat(null,20,0xFF0000);
	var backBtn:MovieClip = createButton("Quit",0xFF0000);

	numText1.text = "已完成测试数:";
	timeText1.text = "上次反应时间:";
	numText1.width = 140;
	timeText1.width = 140;
	infoText.width = 200;
	numText2.text = "0";
	timeText2.text = "无";
	numText1.x = 30;
	numText1.y = 30;
	timeText1.x = 30;
	timeText1.y = 60;
	numText2.x = 30 + numText1.width;
	numText2.y = 30;
	timeText2.x = 30 + timeText1.width;
	timeText2.y = 60;
	infoText.text = "随机出现...";
	infoText.x = 30;
	infoText.y = 90;
	numText1.selectable = false;
	numText2.selectable = false;
	timeText1.selectable = false;
	timeText2.selectable = false;
	infoText.selectable = false;
	numText1.setTextFormat(regularFormat);
	numText2.setTextFormat(regularFormat);
	timeText1.setTextFormat(regularFormat);
	timeText2.setTextFormat(regularFormat);
	infoText.setTextFormat(infoFormat);
	backBtn.x = stage.stageWidth - backBtn.width - 50;
	backBtn.y = stage.stageHeight - 150;

	game_mc = new MovieClip  ;
	game_mc.addChild(numText1);
	game_mc.addChild(numText2);
	game_mc.addChild(timeText1);
	game_mc.addChild(timeText2);
	game_mc.addChild(infoText);
	game_mc.addChild(backBtn);

	game_mc.alpha = 0;
	game_mc.visible = false;
	game_mc.name = "instance28";
	game_mc.addEventListener(Event.ENTER_FRAME,th);

	backBtn.addEventListener(MouseEvent.CLICK,function(e){exitGame();});
	stage.addEventListener(KeyboardEvent.KEY_DOWN,worn);

	addChild(game_mc);

	function worn(e)
	{
		if (e.keyCode == 32 && process)
		{
			cTimer.reset();
			infoText.text = "警告:请勿抢按!";
			infoText.setTextFormat(infoFormat2);
			var wornTimer:Timer = new Timer(600);
			wornTimer.start();
			wornTimer.addEventListener(TimerEvent.TIMER,resetWorn);
			function resetWorn()
			{
				playGame();
				wornTimer.reset();
				infoText.text = "随机出现...";
				infoText.setTextFormat(infoFormat);
			}
			if (wornTimes==3)
			{
				exitGame();
			}
			else
			{
				wornTimes++;
			}
		}
	}
	function th(e:Event)
	{
		numText2.text = String(times);
		timeText2.text = timeArr[times];
		numText2.setTextFormat(regularFormat);
		timeText2.setTextFormat(regularFormat);
	}
}
function createOver()
{
	var backBtn:MovieClip = createButton("Back",0x009900);
	var titleText:TextField = new TextField  ;
	var titleTextFormat:TextFormat = new TextFormat(null,60,0xFF0000);
	var shadowFilter:DropShadowFilter = new DropShadowFilter(5);

	backBtn.x = (stage.stageWidth-backBtn.width)/2;
	backBtn.y = stage.stageHeight - 150;

	titleText.autoSize = "center";
	titleText.text = "游戏结束";
	titleText.x = stage.stageWidth / 2 - 40;
	titleText.y = 80;
	titleText.selectable = false;
	titleText.filters = [shadowFilter];
	titleText.setTextFormat(titleTextFormat);

	over_mc = new MovieClip  ;
	over_mc.addChild(backBtn);
	over_mc.addChild(titleText);

	over_mc.alpha = 0;
	over_mc.visible = false;

	backBtn.addEventListener(MouseEvent.CLICK,function(e){turning(over_mc,UI_mc);});
	addChild(over_mc);
}
function createEnd()
{
	var backBtn:MovieClip = createButton("Back");
	var titleText:TextField = new TextField  ;
	var titleTextFormat:TextFormat = new TextFormat(null,60,0x009900);
	var shadowFilter:DropShadowFilter = new DropShadowFilter(3);

	backBtn.x = (stage.stageWidth-backBtn.width)/2;
	backBtn.y = stage.stageHeight - 150;

	titleText.autoSize = "center";
	titleText.text = "平均成绩:" + String(mark) + " s";
	titleText.x = stage.stageWidth / 2 - 40;
	titleText.y = 80;
	titleText.selectable = false;
	titleText.filters = [shadowFilter];
	titleText.setTextFormat(titleTextFormat);

	end_mc = new MovieClip  ;
	end_mc.addChild(backBtn);
	end_mc.addChild(titleText);

	end_mc.alpha = 0;
	end_mc.visible = false;

	backBtn.addEventListener(MouseEvent.CLICK,function(e){turning(end_mc,UI_mc);});
	end_mc.addEventListener(Event.ENTER_FRAME,th);

	addChild(end_mc);

	function th(e)
	{
		titleText.text = "平均成绩:" + String(mark) + " s";
		titleText.setTextFormat(titleTextFormat);
	}
}
function playGame()
{
	if (times<sumTimes)
	{
		appear();
	}
	else
	{
		var sum:Number = 0;

		for (var i:int=1; i<=sumTimes; ++i)
		{
			sum +=  timeArr[i];
		}

		sum /=  sumTimes;
		sum = Math.floor(sum*1000)/1000;
		mark = sum;

		turning(game_mc,end_mc);
	}
}
function appear()
{
	var randomNum:Number = 0;
	while (randomNum<0.1 || randomNum>0.5)
	{
		randomNum = Math.random();
	}
	trace(randomNum*10);
	cTimer = new Timer(randomNum*10*1000);
	cTimer.start();
	cTimer.addEventListener(TimerEvent.TIMER,startTiming);
	process = true;
	function startTiming()
	{
		cTimer.reset();
		var t0:Number = getTimer();
		stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyIsDown);
		addChild(curEllipse);
		process = false;
		function onKeyIsDown(e:KeyboardEvent)
		{
			if (e.keyCode == 32)
			{
				removeChild(curEllipse);
				cTimer.removeEventListener(TimerEvent.TIMER,startTiming);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyIsDown);
				var t1:Number = getTimer();
				times++;
				timeArr.push((t1-t0)/1000);
				playGame();
			}
		}
	}
}
function setHit(p:Boolean)
{
	setChildIndex(hit,numChildren-1);
	hit.visible = p;
}
function turning(mc1:MovieClip,mc2:MovieClip)
{
	setHit(true);

	var tween:Tween = new Tween(mc1,"alpha",Strong.easeOut,1,0,0.5,true);
	tween.addEventListener(TweenEvent.MOTION_FINISH,f);

	function f()
	{
		mc2.visible = true;
		mc1.visible = false;
		tween = new Tween(mc2,"alpha",Strong.easeInOut,0,1,0.5,true);
		if (mc2.name == "instance28")
		{
			newPlay();
			tween.addEventListener(TweenEvent.MOTION_FINISH,function(e:TweenEvent){setHit(false); appear();});
		}
		else
		{
			tween.addEventListener(TweenEvent.MOTION_FINISH,function(e:TweenEvent){setHit(false);});
		}
	}
}
function ellipseFilter()
{
	var blur:BlurFilter;
	var inde:Boolean = false;
	var lowest:Number = 10;
	var highest:Number = 50;
	var blurX:Number = lowest;
	var blurY:Number = lowest;
	var speed = (highest - lowest) / 10;
	curEllipse.addEventListener(Event.ENTER_FRAME,changeFilter);
	function changeFilter(e)
	{
		blur = new BlurFilter(blurX,blurY,1);
		curEllipse.filter = [blur];
		if (inde)
		{
			blurX -=  speed;
		}
		else
		{
			blurX +=  speed;
		}
		if (blurX==lowest)
		{
			inde = false;
		}
		else if (blurX==highest)
		{
			inde = true;
		}
		blurY = blurX;
		curEllipse.filters = [blur];
	}
}
function exitGame()
{
	if (process)
	{
		cTimer.reset();
	}
	else
	{
		removeChild(curEllipse);
	}
	turning(game_mc,over_mc);
}
function newPlay()
{
	timeArr = new Array("无");
	times = 0;
	process = false;
	wornTimes = 0;
	mark = 0;
}
function main()
{
	createBG();
	createUI();
	createEnd();
	createGame();
	createOver();
	createAbout();
	createEllipse();
	ellipseFilter();
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值