在flash / html中同步两个计时器

时间:2012-10-30 13:24:07

标签: html flash

我正在建立一个有两个倒数计时器的简单网站。问题是他们没有同步。当网页加载时,一个计时器明显优于另一个计时器。这是使用的html

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css" media="screen">

    #flashContent { width:100%; height:100%; }

    </style>
</head>

<body >

<div id="flashContent" align="center" >

        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" id="Timer" align="middle">
            <param name="movie" value="Timer.swf" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#ffffff" />
            <param name="play" value="true" />
            <param name="loop" value="true" />
            <param name="wmode" value="transparent" />
            <param name="scale" value="showall" />
            <param name="menu" value="true" />
            <param name="devicefont" value="false" />
            <param name="salign" value="" />
            <param name="allowScriptAccess" value="sameDomain" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="Timer.swf" width="400" height="250">
                <param name="movie" value="Timer.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="play" value="true" />
                <param name="loop" value="true" />
                <param name="wmode" value="transparent" />
                <param name="scale" value="showall" />
                <param name="menu" value="true" />
                <param name="devicefont" value="false" />
                <param name="salign" value="" />
                <param name="allowScriptAccess" value="sameDomain" />
            <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflash">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
                </a>
            <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>

        <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="250" id="Timer" align="middle">
            <param name="movie" value="Timer.swf" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#ffffff" />
            <param name="play" value="true" />
            <param name="loop" value="true" />
            <param name="wmode" value="transparent" />
            <param name="scale" value="showall" />
            <param name="menu" value="true" />
            <param name="devicefont" value="false" />
            <param name="salign" value="" />
            <param name="allowScriptAccess" value="sameDomain" />
            <!--[if !IE]>-->
            <object type="application/x-shockwave-flash" data="Timer.swf" width="400" height="250">
                <param name="movie" value="Timer.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#ffffff" />
                <param name="play" value="true" />
                <param name="loop" value="true" />
                <param name="wmode" value="transparent" />
                <param name="scale" value="showall" />
                <param name="menu" value="true" />
                <param name="devicefont" value="false" />
                <param name="salign" value="" />
                <param name="allowScriptAccess" value="sameDomain" />
            <!--<![endif]-->
                <a href="http://www.adobe.com/go/getflash">
                    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
                </a>
            <!--[if !IE]>-->
            </object>
            <!--<![endif]-->
        </object>

        </div>

</body>

我在flash vid中使用了两层。一个包含背景元素(包括计时器的动态文本),另一个只包含这段代码:

停止();

var countDownTimer:Timer = new Timer(10);

countDownTimer.addEventListener(TimerEvent.TIMER,updateTimer);

countDownTimer.start();

function updateTimer(Event:TimerEvent):void {

var today:Date = new Date();
var destination:Date = new Date(2012,10,9,18,0,0,0);

if(destination.getTime()-today.getTime() < 0 ){

    Time_txt.text = "00:00:00:00";
    Title_txt.text = "It's Double Time!";

}else{

var daysLeft = Math.floor((destination.getTime()-today.getTime())/(1000*60*60*24));
var hoursLeft = Math.floor(((destination.getTime()-today.getTime())/(1000*60*60))-daysLeft*24);
var minutesLeft = Math.floor(((destination.getTime()-today.getTime())/(1000*60))-daysLeft*24*60-hoursLeft*60);
var secondsLeft = Math.floor(((destination.getTime()-today.getTime())/(1000))-daysLeft*24*60*60-hoursLeft*60*60-minutesLeft*60);

var days:String = new String(daysLeft);
var hours:String = new String(hoursLeft);
var minutes:String = new String(minutesLeft);
var seconds:String = new String(secondsLeft);

if(days.length < 2) days = "0" + days;
if(hours.length < 2) hours = "0" + hours;
if(minutes.length < 2) minutes = "0" + minutes;
if(seconds.length < 2) seconds = "0" + seconds;

var time:String = days +":"+ hours+":"+minutes+":"+seconds;

Time_txt.text = time;
Title_txt.text = "Jazz awaits in...";
}

}

1 个答案:

答案 0 :(得分:1)

使用LocalConnection(下面的链接)在两个文件之间创建连接。在一个...中运行计时器然后在您的updateTime()方法中,通过连接将计时器文本传递给另一个swf。的Presto!

localConnection.send(“myConnection”,“updateMethodName”,time);

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html