Welcome

Welcome to my blog. I mainly blog about the basics of running Flash games over the internet, but sometimes I get distracted. Here you will find my efforts at programming turn based Flash games, plus links to all the games I have written. You are free (as in beer) to get all the code, just email me - nicolas_evans at yahoo.com (sorry you cant click on it - I dont want more spam).

Building Multiplayer Games in Flash.

The Posts

Thursday, March 09, 2006

Hi. This is where I explain how to connect a flash game to the internet. Well start to explain, as its a long process. First a warning, I spent much of the development time trying to get Loadvars.sendAndLoad() working. I never did and I suggest you avoid it too and use Loadvars.load() instead.
First a note about errors. Sometimes Flash will send a message, and fail. It just happens. So everytime I send a message I include what to do if there is a failure. (I also keep a count of errors and give up completely when I reach 5.) This code I put into fnMessageFail(). This method is overwritten by each send. I have never seen other programmers do this, I expect they use inheritance in a proper OOP way, but my way works and seems easier.
Once again I put everything in a class, you can read my previous blogs why I like this so much. Here it is, I try to explain it in my next blog.
class MyCommunications {

  private var gsURL:String;
  var gmyLoadVars:LoadVars;
  var gnMessageID:Number;
  private var gnErrorCount:Number;

  function MyCommunications (pURL:String) {
      gsURL = pURL;
      gnErrorCount = 0;
      gmyLoadVars = new LoadVars();
      gmyLoadVars.onLoad = function(success:Boolean) {
          if (success) {
              if (gnErrorCount > 0)
                  gnErrorCount--;
              _level0.myCommunications.fnMessageSuccess();
          }
          else if (gnErrorCount < 5) {
              gnErrorCount++;
              _level0.myCommunications.fnMessageFail();         
          }
          else {
              trace("Error loading/parsing LoadVars.");
              _level0.gotoAndStop("FatalError");
          }
      }
  }
  function fnLoadVariables(pString:String) {
      trace("fnLoadVariables")
      gmyLoadVars.load(gsURL + "scramble.jsp" + pString +
"&nocache=" + Math.random(), "GET");
  }

  function fnSendToServer (pAsPlayer) {
      var i:Number,j:Number;
      var arTemp:Array = new Array();
   
      j = 0;
      arTemp[j++] = gsURL + "scramble.jsp" +
          "?action=INSERT" +
          "&filename=" + _level0.myScrambleGame.gnGameNumber +
          "&pno=" + pAsPlayer +
          "&nTurn=" + _level0.myScrambleGame.gnTurn +
          "&rnd=" + _level0.myScrambleGame.gnRound +
          "&p2score=" + _level0.myScrambleGame.player2Hand.nScore +
          "&pwd=" + _level0.myScrambleGame.gsPassword +
          "&p1hand=" ;
      for (i=0;i<_level0.myScrambleGame.player1Hand.arPlayerTiles.length;i++) {
          arTemp[j++] = _level0.myScrambleGame.player1Hand.arPlayerTiles[i]._name + ",";
      }         
      arTemp[j++] = "&p2hand=";
      for (i=0;i<_level0.myScrambleGame.player2Hand.arPlayerTiles.length;i++) {
          arTemp[j++] = _level0.myScrambleGame.player2Hand.arPlayerTiles[i]._name + ",";
      }         
      arTemp[j++] = "&board=";
      for (i=0;i<_level0.myScrambleGame.garBoard.length;i++) {
          if (_level0.myScrambleGame.garBoard[i] != "Empty")
              arTemp[j++] = _level0.myScrambleGame.garBoard[i]._name;
          arTemp[j++] = ",";
      }
      arTemp[j++] = "&order=" ;
      for (i=0;i<_level0.myScrambleGame.garTileOrder.length;i++) {
          arTemp[j++] = _level0.myScrambleGame.garTileOrder[i] + ",";
      }
      arTemp[j++] = "&p1toopen=" + _level0.myScrambleGame.player1Hand.bToOpen +
          "&p2toopen=" + _level0.myScrambleGame.player2Hand.bToOpen +
          "&p1score=" + _level0.myScrambleGame.player1Hand.nScore +
          "&messageID=";
      gnMessageID = Math.floor(Math.random() * 1000);
      arTemp[j++] = "" + gnMessageID;
      gmyLoadVars.load(arTemp.join(""),gmyLoadVars,"POST");

  }
  function fnParseVariablesFromServer() {

      if (parseInt(gmyLoadVars.nErrorNumber) != 0) {
          trace("Error received from server " + gmyLoadVars.nErrorNumber)
          _level0.gotoAndStop("ConnectionError")
      }
      _level0.myScrambleGame.gnPlayerNumber = parseInt(gmyLoadVars.pno); 
      _level0.myScrambleGame.gnTurn = parseInt(gmyLoadVars.nTurn);
      _level0.myScrambleGame.gnRound = parseInt(gmyLoadVars.rnd) ;

      _level0.myScrambleGame.gsPassword = rTrim(gmyLoadVars.pwd);
      _level0.myScrambleGame.player1Hand.arPlayerTiles =
      fnConvertStringToObjects(gmyLoadVars.p1hand);
      _level0.myScrambleGame.player2Hand.arPlayerTiles =
          fnConvertStringToObjects(gmyLoadVars.p2hand);
      _level0.myScrambleGame.garBoard = fnConvertStringToObjects(gmyLoadVars.board);
      _level0.myScrambleGame.garTileOrder = gmyLoadVars.order.split(",");
      _level0.myScrambleGame.garTileOrder.pop(); // remove empty space at end
      _level0.myScrambleGame.player1Hand.bToOpen =
          fnConvertStringToBoolean(gmyLoadVars.p1toopen);
      _level0.myScrambleGame.player2Hand.bToOpen =
          fnConvertStringToBoolean(gmyLoadVars.p2toopen);
      _level0.myScrambleGame.player1Hand.nScore = parseInt(gmyLoadVars.p1score);
      _level0.myScrambleGame.player2Hand.nScore = parseInt(gmyLoadVars.p2score);
  }
  function fnConvertStringToObjects (pString:String):Array {
      trace("fnConvertStringToObjects")
      var i:Number;
      var arTemp =new Array();
      var arTempOut = new Array();

      arTemp = pString.split(",");     
      for (i =0;i<arTemp.length;i++) {
          if (arTemp[i] == "undefined" || arTemp[i] == "")
              arTempOut.push("Empty");     
          else if (_level0[arTemp[i]])
              arTempOut.push(_level0[arTemp[i]] );
      }
      return arTempOut;
  }
  function fnConvertStringToBoolean(pString:String):Boolean {

      if (pString.substr(0,4) == "true")
          return true;
      else
          return false;
      }
  function rTrim(s) {
      var i:Number;
      var sTemp = s;
   
      i = sTemp.length;
      while (i > 0) {
          if (sTemp.charAt(i-1) == " " ||
              sTemp.charAt(i-1) == "\n" ||
              sTemp.charAt(i-1) == "\r") {
              i--;
          }
          else {
              break;
          }
      }
      return sTemp.substr(0,i);
  }
  function fnMessageSuccess() {
      trace ("fnMessageSuccess");
  // overwritten by each message
  }
  function fnMessageFail() {
      trace ("fnMessageFail");
  // overwritten by each message
  }
}

# posted by nicolas_evans @ 6:37 AM
Comments: Post a Comment




This page is powered by Blogger. Isn't yours?