flash倒计时

flash倒计时 专栏收录该内容
1 篇文章 0 订阅

工作要求做一个flash倒计时。功能:play,pause,提醒时间变色,时间到停止,可以延时计时。

context是动态文本的实例名称,playpause_mc是影片剪辑按钮的实例名称。




import flash.utils.Timer;
import flash.events.TimerEvent;

//默认defminute分钟,提醒时间reminder分钟,currSec存储暂停时的秒数,超时另计overMinute5分钟
var defminute:Number = 15;//演讲时间,请填整数,如15,即演讲时间设置15分钟
var reminder:Number = 3;//提醒时间,请设置整数,如3,即剩余3分钟时开始提醒
var overMinute:Number = 5;//延时时间,请设置整数,如5,即延时5分钟
//设置结束 
var currSec:int = 0;
var defTime:int = Math.floor(defminute * 60);
var reminderSec:int = Math.floor(reminder * 60);
var overSec:int = Math.floor(overMinute * 60);

//初始值:未开始计时,暂停状态
var isPaused:Boolean = true;
var isTimeUp:Boolean = false;
var isOverTime:Boolean = false;

//文本样式
//初始文本状态
var dformat:TextFormat = new TextFormat();
dformat.color = 0x333333;
dformat.size = 80;

//提醒文本状态
var sformat:TextFormat = new TextFormat();
sformat.color = 0xFF6000;
sformat.size = 80;

//时间到文本状态
var tformat:TextFormat = new TextFormat();
tformat.color = 0xFF6000;
tformat.size = 50;

//实时变化的分钟秒
var minute:int;
var sec:int;
//分秒的文本
var minuteText:String = '';
var secText:String = '';

//set appearance of button, mode to true
playpause_mc.gotoAndStop("play");
playpause_mc.buttonMode = true;

currSec = defTime;
showTime(currSec);

//creat a new Timer
var minuteTimer:Timer = new Timer(1000,currSec);

//designates listeners for the interval and completion events
minuteTimer.addEventListener(TimerEvent.TIMER,onTick);
minuteTimer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);

// movie clip button control;
playpause_mc.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(event:MouseEvent):void
{
	if (isPaused)
	{
		//change state to playing, and play sound from position
		isPaused = false;
		//reverse the appearance of the button
		playpause_mc.gotoAndStop("pause");
		//over time ,change Timer ticking;
		if (isTimeUp && isOverTime )
		{
			minuteTimer.repeatCount = overSec;
			minuteTimer.reset();
			minuteTimer.start();
		}
		else
		{
			//starts the timer ticking;
			minuteTimer.start();
		}
	}
	else
	{
		isPaused = true;
		//change the appearance of the buttons
		playpause_mc.gotoAndStop("play");
		minuteTimer.stop();
	}
}

function onTick(event:TimerEvent):void
{
	if (isTimeUp)
	{
		//延时后第一次开始计时,currSec初始值设置0,延时未超时 isOverTime false
		if (isOverTime)
		{
			currSec = 0;
			isOverTime = false;
		}
		if (currSec < overSec)
		{
			currSec++;
		}
		else
		{
			currSec = 0;
		}
		countext.x = 0;
		countext.y = 0;
	}
	else
	{
		if (currSec>0)
		{
			currSec--;
		}
		else
		{
			currSec = 0;
		}
	}
	showTime(currSec);
}

function onTimerComplete(event:TimerEvent):void
{
	isPaused = true;
	//change the appearance of the buttons
	playpause_mc.gotoAndStop("play");
	dformat.size = 40;
	countext.text = "Time Up!";
	countext.setTextFormat(tformat);
	countext.x = 4;
	countext.y = 10;
	isTimeUp = true;//超时flag
	isOverTime = true;//延时超时flag
}

//动态文本显示时间
function showTime(timesec)
{
	var time = timesec;
	minute = Math.floor((time / 60));
	sec = Math.floor((time - minute * 60));

	if ((minute < 10))
	{
		minuteText = '0' + String(minute);
	}
	else
	{
		minuteText = String(minute);
	}

	if ((sec < 10))
	{
		secText = '0' + String(sec);
	}
	else
	{
		secText = String(sec);
	}

	countext.text = minuteText + ':' + secText;

	if (timesec<reminderSec)
	{
		countext.setTextFormat(sformat);
	}
	else
	{
		countext.setTextFormat(dformat);
	}
}


  • 1
    点赞
  • 0
    评论
  • 2
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

supersxn

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值