2010-07-26 112 views
1

我一直在为我写的Facebook游戏开发一项新功能。游戏允许玩家在欧洲城市之间旅行,并提供商品以获取利润。我添加的这个功能增加了对游戏的寻路AI:它允许玩家选择要前往的城市,然后游戏会自动将玩家的火车沿着轨道从其起始城市移动到目的地城市。我使用AJAX和setTimeout()从后端获取数据,并使火车沿着连接城市的轨道移动。请参考代码,希望能够更好地理解我正在尝试做什么。试图同步AJAX请求

问题是setTimeout()会被频繁调用。我放了一个名为statusFinalDest的全局变量,它可能包含两个值:ENROUTE和ARRIVED。当列车是ENROUTE时,JS列车运动函数使用setTimeout调用它自己,直到后端返回ARRIVED的statusFinalDest,此时列车运动超时循环(假定)终止。但是,不是每次调用myEventMoveTrainManual()都会转回后端进程,而是更频繁地调用它,就好像它不识别statusFinalDest的已更改状态。我试图在代码中加入更多的限制结构来结束这种过度的行为,但它们似乎并没有工作。

下面是相关FBJS(Facebook的JS)代码:

function myEventMoveTrainManual(evt) { 
     if(mutexMoveTrainManual == 'CONTINUE') { 
     //mutexMoveTrainManual = 'LOCKED'; 
     var ajax = new Ajax(); 
     var param = {}; 
     if(evt) { 
      var cityId = evt.target.getParentNode().getId(); 
      var param = { "city_id": cityId }; 
     } 
     ajax.responseType = Ajax.JSON; 
     ajax.ondone = function(data) { 
      statusFinalDest = data.status_final_dest; 
      if(data.code != 'ERROR_FINAL_DEST') { 

      // Draw train at new location 
      trackAjax = new Ajax(); 
      trackAjax.responseType = Ajax.JSON; 
      trackAjax.ondone = function(trackData) { 
       var trains = []; 
       trains[0] = trackData.train; 
       removeTrain(trains); 
       drawTrack(trackData.y1, trackData.x1, trackData.y2, trackData.x2, '#FF0', trains); 

       if(data.code == 'UNLOAD_CARGO') { 
       unloadCargo(); 
       } else if (data.code == 'MOVE_TRAIN_AUTO' || data.code == 'TURN_END') { 
       moveTrainAuto(); 
       } else { 
       /* handle error */ 
       } 
       mutexMoveTrainManual = 'CONTINUE'; 
      } 
      trackAjax.post(baseURL + '/turn/get-track-data'); 
      } 
     } 
     ajax.post(baseURL + '/turn/move-train-set-destination', param); 
     } 

     // If we still haven't ARRIVED at our final destination, we are ENROUTE so continue 
     // moving the train until final destination is reached 
     // statusFinalDest is a global var 
     if(statusFinalDest == 'ENROUTE') { 
     setTimeout(myEventMoveTrainManual, 1000); 
     } 
} 

这里是后端PHP代码:

public function moveTrainSetDestinationAction() { 
    require_once 'Train.php'; 
    $trainModel = new Train(); 

    $userNamespace = new Zend_Session_Namespace('User'); 
    $gameNamespace = new Zend_Session_Namespace('Game'); 

    $this->_helper->layout()->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(); 

    $trainRow = $trainModel->getTrain($userNamespace->gamePlayerId); 
    $statusFinalDest = $trainRow['status_final_dest']; 
    if($statusFinalDest == 'ARRIVED') { 
     $originCityId = $trainRow['dest_city_id']; 
     $destCityId = $this->getRequest()->getPost('city_id'); 
     if(empty($destCityId)) { 
     // If we arrived at final dest but user supplied no city then this method got called 
     // incorrectly so return an error 
     echo Zend_Json::encode(array('code' => 'ERROR_FINAL_DEST', 'status_final_dest' => $statusFinalDest)); 
     exit; 
     } 

     $gameNamespace->itinerary = $this->_helper->getTrainItinerary($originCityId, $destCityId); 
     array_shift($gameNamespace->itinerary); //shift-off the current train city location 
     $trainModel->setStatusFinalDest('ENROUTE', $userNamespace->gamePlayerId); 
     $statusFinalDest = 'ENROUTE'; 
    } 
    $cityId = $trainRow['dest_city_id']; 
    if($trainRow['status'] == 'ARRIVED') { 
     if(count($gameNamespace->itinerary) > 0) { 
     $cityId = array_shift($gameNamespace->itinerary); 
     } 
    } 
    $trainRow = $this->_helper->moveTrain($cityId); 
    if(count($trainRow) > 0) { 
     if($trainRow['status'] == 'ARRIVED') { 
     // If there are no further cities on the itinerary, we have arrived at our final destination 
     if(count($gameNamespace->itinerary) == 0) { 
      $trainModel->setStatusFinalDest('ARRIVED', $userNamespace->gamePlayerId); 
      $statusFinalDest = 'ARRIVED'; 
     } 
     echo Zend_Json::encode(array('code' => 'UNLOAD_CARGO', 'status_final_dest' => $statusFinalDest)); 
     exit; 
     // Pass id for last city user selected so we can return user to previous map scroll postion 
     } else if($trainRow['track_units_remaining'] > 0) { 
     echo Zend_Json::encode(array('code' => 'MOVE_TRAIN_AUTO', 'status_final_dest' => $statusFinalDest)); 
     exit; 
     } else { /* Turn has ended */ 
     echo Zend_Json::encode(array('code' => 'TURN_END', 'status_final_dest' => $statusFinalDest)); 
     exit; 
     } 
    } 
    echo Zend_Json::encode(array('code' => 'MOVE_TRAIN_AUTO_ERROR')); 
    } 
+0

为了澄清一点可能的困惑:myEventMoveTrainManual()不是一个事件处理程序,但它是从事件处理程序调用的。 – 2010-07-26 01:56:45

回答

2

如果函数myEventMoveTrainManual正在从一个事件处理函数调用,你”每次事件发生时都会重新运行一次新的定时器。

一个简单的黑客应该帮助是开始一个新的人之前先杀死计时器:

var timerId; 

//... 
clearTimeout(timerId); 
timerId = setTimeout(myEventMoveTrainManual, 1000); 
//... 

但我认为你真正想做的是调用不同的功能,一个如果你的计时器循环,检查已经在运行,如果没有,请调用myEventMoveTrainManual。

+0

感谢您的及时响应。我在我的代码中添加了clearTimeout()。它确实改进了一些东西,但它并没有解决我的主要问题。现在我已经意识到我还有一个问题:所有这些过多的函数调用通过调用turnEnd()过于频繁地夸大了我的计数。这是一个严重的问题。无论如何,谢谢你的帮助。 – 2010-07-26 07:06:55