Skip to main content
November 13, 2015
Answered

I can not reset a timer flash AS3?

  • November 13, 2015
  • 1 reply
  • 652 views

I wrote the following clear a timer so that it counts form 0 again. The timer is not reset, it only stop and it save the last count.

The trace result is: 34900 and I want it to be 0. How to do it ?

myTimer.reset();

myTimer.removeEventListener(TimerEvent.TIMER, timerListener);

trace(getTimer());

Here is the full code of my project: (The comment linse are for me, not for you)

stop();

var i:int; i=0;

var c:int; c=0;

var timer_check:uint = 0;

var removeCheck:Boolean = true;

var mark:Number = 0;

var timerOff:Boolean = false;

var list:Array = [] ;

var timed: uint = 16 ;

for(var p:uint=1; p<=44; p++)

********************* if you want to add more pictures increas 44 to the new number of pictures

{

    list.push(p);

}

shuffle(list);

var good:Array = [];  copy

var testing:Array = [];

copy(list,good);

copy(list,testing);

good = good.splice (0,18);  cut the first 18 elements

var pic:Array = list.splice(0,9);

testing = testing.slice(0,45)

shuffle(testing);

var myTimer:Timer;  second: re-arrangeing some picturs using nine "if" conditions:

myTimer = new Timer(1000,34);

myTimer.addEventListener(TimerEvent.TIMER, timerListener);

function timerListener (e:TimerEvent):void{

timed -- ;

time.text = "Time:" + timed.toString ();

time.x = 340 ; time.y = 7;

if ( timed == 0 && removeCheck )  16000 getTimer() > 18000

  {

  timed = 16;

  for(var removeOldPic:uint = 0; removeOldPic < 9 ; removeOldPic++)

  {

  this ["p"+pic[removeOldPic]].x = 5000;

  }

  pic = list.splice(0,9);

  c=0;

  removeCheck = false;

  }

if (getTimer() > 34000)  after finishing the timer  34000

  {

  time.x = 5000;

  PicGraphic.text = "Pictures: " + (picCounter).toString () + " / " + numberOfTestingPictures.toString () ;

  PicGraphic.x = 280 ; PicGraphic.y = 7 ;

  for(var removeNewPic:uint = 0; removeNewPic < 9 ; removeNewPic++)

  {

  this ["p"+pic[removeNewPic]].x = 5000;

  }

  yesyes.x = 465 ; nono.x = 165; yesyes.y = 300 ; nono.y = 300

  //********************** contine removing event listeners *******

  timerOff = true;

  myTimer.reset();

  myTimer.removeEventListener(TimerEvent.TIMER, timerListener);

  trace(getTimer());

  }

while (c<9)

{

if (c==0)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 132 ;

this ["p"+pic].y = 60 ;

}

  if (c==1)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 312 ;

this ["p"+pic].y = 60 ;

}

  if (c==2)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 492 ;

this ["p"+pic].y = 60 ;

}

if (c==3) // new row

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 132;

this ["p"+pic].y = 240;

}

  if (c==4)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 312;

this ["p"+pic].y = 240;

}

  if (c==5)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 492;

this ["p"+pic].y = 240;

}

  if (c==6) // new row

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 132;

this ["p"+pic].y = 420;

}

  if (c==7)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 312;

this ["p"+pic].y = 420;

}

  if (c==8)

{

this ["p"+pic].width = 175 ;

this ["p"+pic].height = 175 ;

this ["p"+pic].x = 492;

this ["p"+pic].y = 420;

}

c++;

}

}

myTimer.start();

//-----------------------------------end---------------------------------------------

// buttons programming

var picCounter: uint = 0 ;

var numberOfTestingPictures: uint = randomRange(30,40);

yesyes.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);

function fl_ClickToGoToWebPage(event:MouseEvent):void

{

  PicGraphic.text = "Pictures: " + (picCounter + 1).toString () + " / " + numberOfTestingPictures.toString () ; // Code is repeated for refreshing its data

  for (var iyes:uint = 0 ; iyes < 17 ; iyes++)

  {

  if (testing [picCounter] == good [iyes] ) {mark++;}

  }

  // Don't forget to repeat the changes of the following "if statement"

  // in the nono button

  if (picCounter>= numberOfTestingPictures ) // finising the game and appearing the score

  {

  yesyes.x = 5000;

  nono.x = 5000;

  bar.x = 5000;

  this ["p"+testing[picCounter]] .x = 5000 ;

  stage.removeEventListener(Event.ENTER_FRAME, appearingPic);

  var fine:Number = ( mark / ( picCounter + 1 ) ) * 100 ;

  score.text = "Your score is: " + fine.toFixed(2) + " %";

  score.x = 225; score.y=200;

  xt2 .y = 400 ; playAgain.y = 300;

  PicGraphic.x = 5000;

  }

  picCounter++;

}

var negativeMark:uint = 0;

nono.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage1);

function fl_ClickToGoToWebPage1(event:MouseEvent):void

{

  PicGraphic.text = "Pictures: " + (picCounter + 1).toString () + " / " + numberOfTestingPictures.toString () ;

  for (var ino:uint = 0 ; ino < 17 ; ino++)

  {

  if (testing [picCounter] != good [ino]) {negativeMark++ ;}

  }

  if (negativeMark > 16)

  {

  mark++;

  negativeMark = 0;

  }

  if (picCounter>= numberOfTestingPictures )  // finising the game and appearing the score

  {

  yesyes.x = 5000;

  nono.x = 5000;

  bar.x = 5000;

  this ["p"+testing[picCounter]] .x = 5000 ;

  stage.removeEventListener(Event.ENTER_FRAME, appearingPic);

  var fine:Number = ( mark / ( picCounter + 1 ) ) * 100 ;

  score.text = "Your score is: " + fine.toFixed(2) + " %";

  score.x = 225; score.y=200;

  xt2 .y = 400 ; playAgain.y = 300;

  PicGraphic.x = 5000;

  }

  picCounter++;

}

stage.addEventListener(Event.ENTER_FRAME, appearingPic);

function appearingPic (event:Event)

{

  if (timerOff)

  {

  this ["p"+testing[picCounter]] .width = 175 ;

  this ["p"+testing[picCounter]] .height = 175 ;

  this ["p"+testing[picCounter]] .x = 312 ;

  this ["p"+testing[picCounter]] .y = 112;

  for (var tc=0 ; tc< testing.length - 1 ; tc++)

  {

  if (tc != picCounter){ this ["p"+testing[tc]].x = 5000 ;}

  }

  }

}

xt2.addEventListener(MouseEvent.CLICK, exitbutton2);

function exitbutton2(event:MouseEvent):void{import flash.system.fscommand;fscommand("quit");System.exit(0);}

playAgain.addEventListener(MouseEvent.CLICK, resetAllTheGame);

function resetAllTheGame(event:MouseEvent):void

{

i=0;

c=0;

timer_check=0;

removeCheck = true;

mark = 0;

list.length = 0;

timed = 16 ;

good.length = 0;

testing.length =0;

pic.length =0;

picCounter = 0;

gotoAndStop(1);

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

function randomRange(minNum:Number, maxNum:Number):Number // This function produces a random number

{

    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);

}

function removeDuplicate(arr:Array) : void{

    var i:int;

    var j: int;

    for (i = 0; i < arr.length - 1; i++){

        for (j = i + 1; j < arr.length; j++){

            if (arr === arr){

                arr.splice(j, 1);

            }

        }

    }

}

function shuffle(array_shuffle:Array) {

    var p_array_shuffle:int;

    var t_array_shuffle:*;

    var ivar:int;

    for (ivar = array_shuffle.length-1; ivar>=0; ivar--) {

        p_array_shuffle=Math.floor((ivar+1)*Math.random());

        t_array_shuffle = array_shuffle[ivar];

        array_shuffle[ivar] = array_shuffle[p_array_shuffle];

        array_shuffle[p_array_shuffle] = t_array_shuffle;

    }

}

function copy(copyA:Array,copyB:Array)  deep copy of two array, the source array is first

{

  for (var copy_counter:uint = 0; copy_counter < copyA.length - 1; copy_counter++)

  {

  copyB[copy_counter] = copyA[copy_counter];

  }

}

This topic has been closed for replies.
Correct answer dmennenoh

myTimer.reset();

myTimer.removeEventListener(TimerEvent.TIMER, timerListener);

trace(getTimer());

You are tracing a completely different timer. getTimer() is the ms since the movie started playing and has nothing to do with the myTimer Timer.

1 reply

dmennenohCorrect answer
Inspiring
November 13, 2015

myTimer.reset();

myTimer.removeEventListener(TimerEvent.TIMER, timerListener);

trace(getTimer());

You are tracing a completely different timer. getTimer() is the ms since the movie started playing and has nothing to do with the myTimer Timer.