2011-05-10 109 views
0

如何在ActionScript 3中創建公交時刻表倒計時,我在網上搜索,但主要是爲假期或特定日期倒計時出現。我需要它是經常性的,公交車每30分鐘運行一次。在動作3倒計時

+0

你有沒有嘗試過自己?張貼你已經做了什麼的一些代碼。你使用TextField嗎?這是非特定的方式。 – DanielB 2011-05-10 15:34:21

+0

請詳細說明。你有沒有試過計時器? – taskinoor 2011-05-10 15:43:22

回答

0
package 
{ 
    import flash.display.Sprite; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 
    import flash.events.Event; 

    public class countdownTimer extends Sprite 
    { 
     private var timer:Timer = new Timer(1000); 
     private var countdown:Number = 60; 

     public function countdownTimer():void 
     { 
      init(); 
     } 
     private function init():void 
     { 
      timer.start(); 
      timer.addEventListener(TimerEvent.TIMER, action); 
     } 
     private function action(e:TimerEvent):void 
     { 
      countdown--; 
      trace(countdown); 
     } 
    } 
} 

它只是基本..希望它會幫助你。

1

我做了一個小的Flash應用程序,給你一個想法,你如何接近公交站牌與倒計時:

schedule.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<schedule> 
    <bus> 
     <name>1</name> 
     <times> 
      <time>1300</time> 
      <time>1400</time> 
      <time>1500</time> 
      <time>1600</time> 
      <time>1700</time> 
     </times> 
    </bus> 
    <bus> 
     <name>2</name> 
     <times> 
      <time>1300</time> 
      <time>1400</time> 
      <time>1500</time> 
      <time>1600</time> 
      <time>1700</time> 
     </times> 
    </bus> 
</schedule> 

Main.as(文檔類) :

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.utils.Timer; 

    [SWF(width="200", height="350", backgroundColor="0xFFFFFF", frameRate="32")] 
    public class Main extends Sprite 
    { 
     private var _scheduleUi:ScheduleUI; 

     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var urlLoader:URLLoader = new URLLoader(new URLRequest("xml/schedule.xml")); 
      urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete); 

     }// end function 

     private function onUrlLoaderComplete(e:Event):void 
     { 
      _scheduleUi = new ScheduleUI(XML(URLLoader(e.target).data)); 
      addChild(_scheduleUi); 

     }// end function 

    }// end class 

}// end package 

import flash.display.Sprite; 
import flash.events.Event; 
import flash.text.TextField; 
import flash.text.TextFieldAutoSize; 

internal class ScheduleUI extends Sprite 
{ 
    private var _schedule:Schedule; 
    private var _scheduleTextField:TextField; 

    public function ScheduleUI(scheduleXml:XML) 
    { 
     _schedule = new Schedule(scheduleXml); 
     _scheduleTextField = new TextField(); 
     _scheduleTextField.autoSize = TextFieldAutoSize.LEFT; 
     _scheduleTextField.multiline = true; 
     addChild(_scheduleTextField); 
     addEventListener(Event.ENTER_FRAME, onEnterFrame); 

    }// end function 

    private function onEnterFrame(e:Event):void { updateScheduleTextField() } 

    private function updateScheduleTextField():void 
    { 
     var scheduleString:String = new String(); 

     for each(var bus:Bus in _schedule.getBuses()) 
     { 
      scheduleString += "BUS: " + bus.name; 

      for each(var time:Time in bus.getTimes()) 
      { 
       scheduleString += "\n\n" + "\t" + "Time: " + time + ", Counddown: " + time.getCountDown(); 

      }// end for each 

      scheduleString += "\n\n"; 

     }// end for each 

     _scheduleTextField.text = scheduleString; 

    }// end function 

}// end class 

internal class Schedule 
{ 
    private var _schedule:XML; 

    public function Schedule(schedule:XML):void 
    { 
     _schedule = schedule; 

    }// end function 

    public function getBuses():Vector.<Bus> 
    { 
     var buses:Vector.<Bus> = new Vector.<Bus>(); 

     for each(var bus:XML in _schedule.bus) buses.push(new Bus(bus)); 

     return buses; 

    }// end function 

}// end function 

internal class Bus 
{ 
    private var _bus:XML; 

    public function get name():int { return _bus.name } 

    public function Bus(bus:XML) { _bus = bus } 

    public function getTimes():Vector.<Time> 
    { 
     var times:Vector.<Time> = new Vector.<Time>(); 

     for each(var time:XML in _bus.times.time) times.push(new Time(time)); 

     return times; 

    }// end function 

}// end class 

internal class Time 
{ 
    private var _time:XML; 

    public function Time(time:XML) { _time = time } 

    public function getCountDown():String 
    { 
     var hours:int = _time.toString().substring(0, 2); 
     var minutes:int = _time.toString().substring(2, 4); 

     var currentDate:Date = new Date(); 

     var date:Date = new Date(); 
     date.setUTCMilliseconds(0); 
     date.setUTCSeconds(0); 
     date.setUTCMinutes(minutes); 
     date.setUTCHours(hours - 1); 

     var countDownDate:Date = new Date(); 
     countDownDate.setTime((date.getTime() - currentDate.getTime())) 

     return (countDownDate.getTime() > 0) ? countDownDate.toTimeString().substring(0, 8) : String(0); 

    }// end function 

    public function toString():String { return _time } 

}// end class 
+0

別擔心我會在更新我的答案時解釋所有課程,但首先我需要運行該應用程序的屏幕截圖並將其添加到答案中。 – Taurayi 2011-05-11 14:21:31