Skip to content

Instantly share code, notes, and snippets.

@itoz itoz/gist:997062
Created May 28, 2011

Embed
What would you like to do?
マウスオーバーでタイマーストップしマウスアウトで「残り時間から」再開
/**
*============================================================
* copyright(c) 2011 www.romatica.com
* @author itoz
* 2011/05/29
*============================================================
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.utils.getTimer;
/**
* マウスオーバーでタイマーストップ
* マウスアウトで「残り時間から」再開
*/
public class TestTimer extends Sprite
{
private var _btn : Sprite;
private var _mainTimer : Timer;
private var _latestTimerTime : int;
private var _nextDelayTime : Number;
private const TIMER_SPAN:int = 2000;//何ミリ秒間隔で実行したいか
public function TestTimer()
{
_btn = addChild(new Sprite()) as Sprite;
_btn.graphics.beginFill(0x333333);
_btn.graphics.drawRect(0,0,100,50);
_btn.graphics.endFill();
_btn.addEventListener(MouseEvent.ROLL_OVER, onOver);
_btn.addEventListener(MouseEvent.ROLL_OUT, onOut);
_btn.buttonMode = true;
doTimer(0);
}
// タイマー実行
private function doTimer(millSec : int) : void
{
deleteTimer();
_mainTimer = new Timer(millSec, 1);
_mainTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
_mainTimer.start();
}
// タイマー終了
private function onTimerComplete(event : TimerEvent) : void
{
trace(' > timer comp ');
saveLatestTime();
doTimer(TIMER_SPAN);
}
// マウスアウト
private function onOut(event : MouseEvent) : void
{
trace("[out] wait = " + _nextDelayTime);
_latestTimerTime = getTimer();
doTimer(_nextDelayTime);
}
// マウスオーバー
private function onOver(event : MouseEvent) : void
{
trace("[over] stop ");
// オーバーされたとき、次のタイマー実行まで残り何秒だったか保持
_nextDelayTime = _mainTimer.delay - (getTimer() - _latestTimerTime );
saveLatestTime();
deleteTimer();
}
// 最後に実行された時間を保持
private function saveLatestTime() : void
{
_latestTimerTime = getTimer();
}
// タイマー消去
private function deleteTimer() : void
{
if (_mainTimer != null) {
_mainTimer.stop();
_mainTimer = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.