Skip to main content
Known Participant
June 23, 2010
Answered

How To Make Flash Continuously Play?

  • June 23, 2010
  • 1 reply
  • 1650 views

I have a countdown timer and a click counter and I don't want for either of those to reset if the page were closed / refreshed.  What I want is that once the timer and click counter start, nothing will interupt them.  But the problem is that if i simply go to another page and back it resets everything.  Can anyone help with this?  Here is my code...

var numOfDays=30;
var total_time=numOfDays*24*60*60
var timer:Timer=new Timer(1000,total_time);
timer.addEventListener(TimerEvent.TIMER,onTimerRuns)
timer.start();
function onTimerRuns(e:TimerEvent=null)
{
        var sec=total_time-timer.currentCount;
        sectxt.text=String(sec%60);

        var min=Math.floor(sec/60);
        mintxt.text=String(min%60);

        var hour=Math.floor(min/60);
        hrtxt.text=String(hour%24);

        var day=Math.floor(hour/24);
        daytxt.text=String(day);
}
onTimerRuns();


var clicks:Number=0;
bidbutton.addEventListener(MouseEvent.CLICK,addTime);
function addTime(e:MouseEvent):void
{
    total_time += 30;
    clicks+=.01;
    price.text=formatF(clicks);
}

function formatF(n:Number):String
{
    n = Math.round(n*100)/100;
    var s:String = n.toString();
    if(s.indexOf(".")==-1)
    {s=s+".";}
    var n = s.split(".")[0];
    var f:String = s.split(".")[1].toString();
    while(f.length<2)
    {f=f+"0";}
    return n+"."+f;
}

This topic has been closed for replies.
Correct answer kglad

use:

var alreadyStarted:Boolean

if(!alreadyStarted){

alreadyStarted=true;


var numOfDays=30;
var total_time=numOfDays*24*60*60
var timer:Timer=new Timer(1000,total_time);
timer.addEventListener(TimerEvent.TIMER,onTimerRuns)
timer.start();

onTimerRuns(null);


var clicks:Number=0;
bidbutton.addEventListener(MouseEvent.CLICK,addTime);

}


function onTimerRuns(e:TimerEvent=null)
{
        var sec=total_time-timer.currentCount;
        sectxt.text=String(sec%60);

        var min=Math.floor(sec/60);
        mintxt.text=String(min%60);

        var hour=Math.floor(min/60);
        hrtxt.text=String(hour%24);

        var day=Math.floor(hour/24);
        daytxt.text=String(day);
}
function addTime(e:MouseEvent):void
{
    total_time += 30;
    clicks+=.01;
    price.text=formatF(clicks);
}

function formatF(n:Number):String
{
    n = Math.round(n*100)/100;
    var s:String = n.toString();
    if(s.indexOf(".")==-1)
    {s=s+".";}
    var n = s.split(".")[0];
    var f:String = s.split(".")[1].toString();
    while(f.length<2)
    {f=f+"0";}
    return n+"."+f;
}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 23, 2010

use:

var alreadyStarted:Boolean

if(!alreadyStarted){

alreadyStarted=true;


var numOfDays=30;
var total_time=numOfDays*24*60*60
var timer:Timer=new Timer(1000,total_time);
timer.addEventListener(TimerEvent.TIMER,onTimerRuns)
timer.start();

onTimerRuns(null);


var clicks:Number=0;
bidbutton.addEventListener(MouseEvent.CLICK,addTime);

}


function onTimerRuns(e:TimerEvent=null)
{
        var sec=total_time-timer.currentCount;
        sectxt.text=String(sec%60);

        var min=Math.floor(sec/60);
        mintxt.text=String(min%60);

        var hour=Math.floor(min/60);
        hrtxt.text=String(hour%24);

        var day=Math.floor(hour/24);
        daytxt.text=String(day);
}
function addTime(e:MouseEvent):void
{
    total_time += 30;
    clicks+=.01;
    price.text=formatF(clicks);
}

function formatF(n:Number):String
{
    n = Math.round(n*100)/100;
    var s:String = n.toString();
    if(s.indexOf(".")==-1)
    {s=s+".";}
    var n = s.split(".")[0];
    var f:String = s.split(".")[1].toString();
    while(f.length<2)
    {f=f+"0";}
    return n+"."+f;
}

xravielxAuthor
Known Participant
June 23, 2010

I tried using the new code and the same thing still happened as before it's restarting the timer and all

kglad
Community Expert
Community Expert
June 23, 2010

Oh ok I get it now thanks guys!


if you're closing the flash player, using the sharedobject would probably be what you want to use.