A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: user input not syncing with countdown timer

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    5

    user input not syncing with countdown timer

    Hi guys,

    i made a count down timer with a user input area.when you run the swf file it works well for seconds only but if you add minutes and/or hours to the input area, the counter doesnt stop when it gets to the inputed time,instead it continues with 61, 62......and so on. i have been trying to make it sync with the inputed time but it doesnt. i really don't know what i am doing wrongly thats why i have come here for help.
    i named the input box: Time_txt, the dynamic countdown textbox is named: myText and a start button named Start_btn.


    thanks
    sarah

    the code is below,the .fla file is above 300kb



    import flash.events.Event;
    stop();

    var SelectedTime:int = Number(Time_txt.text);
    var timer:Timer = new Timer(1000, SelectedTime);
    Start_btn.addEventListener(MouseEvent.CLICK, StartTimer);
    timer.addEventListener(TimerEvent.TIMER, countdown);

    function StartTimer(e:MouseEvent):void {
    timer.start();
    trace(SelectedTime);
    }
    function countdown(event:TimerEvent) {
    var totalSecondsLeft:Number = SelectedTime + timer.currentCount;
    trace(timer.currentCount);
    if (Number(Time_txt.text) == totalSecondsLeft - 1) {
    timer.stop(); (gotoAndStop(2));
    } else {
    myText.text = timeFormat(totalSecondsLeft);
    }
    }
    function timeFormat(seconds:Number):String {
    var hours:int;
    var minutes:int;
    //var seconds:int;
    var sHours:String;
    var sMinutes:String;
    var sSeconds:String;
    if (seconds > 59) {
    minutes = Math.floor(seconds / 60);
    sMinutes = String(minutes);
    sSeconds = String(seconds / 60);
    }
    if (minutes > 59) {
    hours = Math.floor(minutes / 60);
    sHours = String(hours);
    sMinutes = String(minutes % 60);
    }

    if (minutes > 59) {
    hours = Math.floor(minutes / 24);
    sHours = String(hours);
    sMinutes = String(minutes % 24);
    }

    else {
    sHours = String(hours);
    sMinutes = String(minutes);
    sSeconds = String(seconds);
    }
    if (sSeconds.length == 1) {
    sSeconds = "0" + sSeconds;
    }

    if (sMinutes.length == 1) {
    sMinutes = "0" + sMinutes;
    }

    if (sHours.length == 1) {
    sHours = "0" + sHours;
    }

    return sHours + ":" + sMinutes + ":" + sSeconds;
    }

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    This might help you get sorted a bit more,
    PHP Code:
    var days:Number 0;
    var 
    hours:Number 0;
    var 
    minutes:Number 0;
    var 
    seconds:Number 0;

    Start_btn.addEventListener(MouseEvent.CLICKstartTimer);

    var 
    timer:Timer;

    function 
    startTimer(e:MouseEvent):void
    {
        
    timer = new Timer(1000);
        
    timer.addEventListener(TimerEvent.TIMERclock);
        
    timer.start();
    }

    function 
    clock(e:TimerEvent):void
    {
        
    seconds +=  1;
        if (
    seconds 59)
        {
            
    minutes +=  1;
            
    seconds 0;
        }
        if (
    minutes 59)
        {
            
    minutes 0;
            
    hours +=  1;
        }
        if (
    hours 23)
        {
            
    hours 0;
            
    days +=  1;
        }
        
    timeText.text String(numberFill(days) + days ":" numberFill(hours) + hours ":" numberFill(minutes) + minutes ":" numberFill(seconds) + seconds);
    }

    function 
    numberFill(val:Number):String
    {
        if (
    val 10)
        {
            return 
    "0";
        }
        else
        {
            return 
    "";
        }

    I havent used the input text, perhaps you can incorporate that.
    You will need to decipher whether the input is minutes seconds or hours or even days, then make your timer stop and do you explosion or whatever you wish to happen

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    or even more like so,
    PHP Code:
    var days:Number 0;
    var 
    hours:Number 0;
    var 
    minutes:Number 0;
    var 
    seconds:Number 0;

    var 
    count:Number;

    Start_btn.addEventListener(MouseEvent.CLICKstartTimer);

    var 
    timer:Timer;

    function 
    startTimer(e:MouseEvent):void
    {
        
    timer = new Timer(1000);
        
    timer.addEventListener(TimerEvent.TIMERclock);
        
    timer.start();
    }

    function 
    clock(e:TimerEvent):void
    {
        
    count timer.currentCount;
        
    seconds Math.floor(count 60);
        
    minutes Math.floor(count 60);
        
    hours Math.floor(minutes 60);
        
    days Math.floor(hours 24);

        
    timeText.text String(numberFill(days) + days ":" numberFill(hours) + hours ":" numberFill(minutes) + minutes ":" numberFill(seconds) + seconds);
    }

    function 
    numberFill(val:Number):String
    {
        if (
    val 10)
        {
            return 
    "0";
        }
        else
        {
            return 
    "";
        }


  4. #4
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    Hello fruitbeard, you know the initial challenge i had was to make the input text sync with the countdown timer.mine actually worked for seconds but didnt sync for minutes and hours. thats actually my challenge.All i need is for the sync to occur between the input text and countdown for seconds,minutes and hours,then i can take it from there.thanks

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I've given you the code to do so in post #1, what you need to do is tell the timer whether the input is seconds, minutes or hours.

    You original timer continued counting after 59 seconds, example, it continued with seconds: 61, 62 , 63 and so on.

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Mess around with this.

  7. #7
    Registered User
    Join Date
    Mar 2015
    Posts
    5
    Hi,

    Thanks again FruitBeard.I get the picture.I think I can customize this to suit my program.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center