2013-02-11 88 views
1

這是簡單的代碼,但我無法解決.. 我clik a.button,所以計時器運行,但如果我點擊b.button,計時器stil正在運行。如何停止這個計時器?由於 (對不起我的英文不好)不能停止計時器AS3

a.addEventListener(MouseEvent.CLICK, starts); 
    function starts(e:MouseEvent) 
    { 
      time(0); 
    } 
    b.addEventListener(MouseEvent.CLICK, stops); 
    function stops(e:MouseEvent) 
    { 
      time(1); 
    } 
    function time(cc) 
    { 
     var timer:Timer = new Timer(1000,100); 
     timer.addEventListener(TimerEvent.TIMER, tmer); 
     timer.start(); 
     function tmer(e:TimerEvent):void 
     { 
      trace("start"); 
     } 
     if(cc == 1) 
     { 
      trace("stop"); 
      timer.stop(); 
      timer.removeEventListener(TimerEvent.TIMER, tmer); 
     } 
     cc=0; 
    } 

回答

1

嘗試這樣的:

a.addEventListener(MouseEvent.CLICK, starts); 
function starts(e:MouseEvent) 
{ 
    time(0); 
} 
b.addEventListener(MouseEvent.CLICK, stops); 
function stops(e:MouseEvent) 
{ 
    time(1); 
} 

var timer:Timer; 
function time(cc:int) 
{ 
    if(cc == 0) 
    { 
     if(!timer) 
      timer = new Timer(1000,100);   
     timer.addEventListener(TimerEvent.TIMER, tmer); 
     timer.start(); 
    } 
    else if (cc == 1) 
    { 
     trace("stop"); 
     timer.stop(); 
     timer.removeEventListener(TimerEvent.TIMER, tmer); 
    } 
} 

function tmer(e:TimerEvent):void 
{ 
    trace("start"); 
} 
+0

hmmmm ..感謝的BitmapData .. – dikagaulzzz 2013-02-11 02:21:42

-1

這是因爲第二次您要創建一個新的Timer對象。

var timer:Timer = new Timer(1000,1000); 
function tmer(event:TimerEvent):void{ 
    trace("The timer is working"); 
} 
timer.addEventListener(TimerEvent.TIMER, tmer); 
timer.stop(); 
a.addEventListener(MouseEvent.CLICK, starts); 
    function starts(e:MouseEvent) 
    { 
      timer.reset(); 
      timer.start(); 
    } 
    b.addEventListener(MouseEvent.CLICK, stops); 
    function stops(e:MouseEvent) 
    { 
      timer.stop(); 
    } 
+0

感謝montycarlo ...:d – dikagaulzzz 2013-02-11 02:22:31

1

試試這個:

var timer:Timer = new Timer(1000,100); 

// To verify if timer is running or stopping 
timer.addEventListener(TimerEvent.TIMER, tickTock); 
function tickTock(e:TimerEvent):void 
{ 
    trace("tick tock"); 
} 

a.addEventListener(MouseEvent.CLICK, starts); 
function starts(e:MouseEvent) 
{ 
     time(0); 
} 

b.addEventListener(MouseEvent.CLICK, stops); 
function stops(e:MouseEvent) 
{ 
     time(1); 
} 

function time(cc) 
{ 
    if(cc == 0) 
    { 
     trace("start"); 
     timer.start(); 
    } 
    else if(cc == 1) 
    { 
     trace("stop"); 
     timer.stop(); 
    } 
} 

或者乾脆:

var timer:Timer = new Timer(1000,100); 

// To verify if timer is running or stopping 
timer.addEventListener(TimerEvent.TIMER, tickTock); 
function tickTock(e:TimerEvent):void 
{ 
    trace("tick tock"); 
} 

a.addEventListener(MouseEvent.CLICK, starts); 
function starts(e:MouseEvent) 
{ 
     trace("start"); 
     timer.start(); 
} 

b.addEventListener(MouseEvent.CLICK, stops); 
function stops(e:MouseEvent) 
{ 
     trace("stop"); 
     timer.stop(); 
} 
+0

感謝khalics ...:d – dikagaulzzz 2013-02-11 02:22:08