2011-09-22 6 views
0

は、私はビデオの進捗を示すイベント発火した値:繰り返しモジュラスを除外するには、

_currentPosition = 3.86秒 _currentPosition = 4.02秒 _currentPosition = 4.16秒を など

私がしようとしている何5秒ごとにサーバーに通知を送信して、進行状況を示します。 Math.floorを使用して秒数を最も近い整数に丸めることができます。それから私はモジュラスを使って5秒ごとに得ることができます。私が見ていないのは、5.02,5.15,5.36などがすべて対象となるため、(例えば)5のリピートを送信しない方法です。

私はenterframeと同様に、すぐに起動するイベントで実行されている次のようなものが必要です。しかし、私は...

var _sentNumber:int; 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
var _floorPosition:int = Math.floor(_currentPosition); //returns 5 
    if(_floorPosition % 5 == 0) //every five seconds, but this should only happen 
             // once for each multiple of 5. 
    { 
     if(_floorPosition != _sentNumber)  //something like this? 
     { 
     sendVariablesToServer("video_progress"); 
     } 
    _sentNumber = _floorPosition; 

おかげで、それは、どこにそれをリセットするために宣言する_sentNumber、のためにテストを行うには方法や場所はわかりません。

答えて

0

あなたはほとんどそこにいるようです。私はちょうどif文の内側に_sentNumber宣言を置くところ:

var _sentNumber:int = 0; 

private function onUpdate(...args:Array):void // or whatever your method signature is that handles the update 
{ 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
    var _floorPosition:int = Math.floor(_currentPosition); //returns 5 
    if(_floorPosition % 5 == 0 && _floorPosition != _sentNumber) //every five seconds, but this should only happen once for each multiple of 5. 
    { 
     sendVariablesToServer("video_progress"); 
     _sentNumber = _floorPosition; 
    } 
} 
+0

うん、それはそれだし。ありがとう! – David

+0

* *は*動作しますか? intは基本データ型です。したがって、var _sentNumber:intはvar _sentNumber:int = new int()と似ています。したがって、_sentNumberを通過するたびに0に再初期化する必要があります。したがって、このコードは失敗します。 – David

+0

私はあなたが単純にその場所にvar _sentNumber:intを置いたと思った!はい、あなたは間違いなくその宣言を関数の範囲から外して、再初期化されないようにする必要があります。私はこれを反映するために私の答えを更新します。 – meddlingwithfire

0
private var storedTime:Number = 0; 

private function testTime():void{ 
    //_currentPosition is the same as current time, i.e. 5.01, 5.15, etc. 
    if(_currentPosition-5 > storedTime){ 
    storedTime = _currentPosition 
    sendVariablesToServer("video_progress"); 
    } 
} 
関連する問題