问题描述:

I'm writing an online multiplayer text-based game in ActionScript 3, and trying to find a way to create a single object, and allow all the players modify it on-the-fly (I mean, when it's modified, the other clients will see its new value.)

Google tells me to use SharedObject. But I have no idea how. Moreover, I don't know if it does what I'm looking for.

So think of it as a chat room, where a single object (ChatRoom, which has a member Text:String)

how do I implement such a thing? :

function UserPressedEnter(event:KeyboardEvent,username:String,message:String)

{

ChatRoom.Text += "\n"+username+" : "+message;

}

Thanks !

网友答案:

If I'm understanding this correctly, you'll probably have to use some server-side code and send messages back and forth from the clients.

As far as I know SharedObject is for communicating between 2 separate flash instances on the same page. EDIT: Unless you are running an Adobe Flash Media Sever in which you could take advantage of remote communication.

Here is a simple example of sending data into php and having it return something. You can customize the serverside logic to work how you need it to.

If this is something that needs to be super speedy you might look at setting up a Node server using Redis instead of PHP.

AS

function UserPressedEnter(event:KeyboardEvent, username:String, message:String):void {
  var action:String = "http://www.mywebserver.com/save_messages.php";
  var loader:URLLoader = new URLLoader();
  loader.dataFormat = URLLoaderDataFormat.VARIABLES;

  var request:URLRequest = new URLRequest(action);
  request.method = URLRequestMethod.POST;      
  request.data = URLVariables({username:username, message:message});

  loader.addEventListener(Event.COMPLETE, completeHandler);
  loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpResponseHandler);
  loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  loader.addEventListener(Event.OPEN, openHandler);
  loader.load(request);
}

function completeHandler(event:Event):void {
  trace("");
  trace("******************************************** DATA RETRIEVED VIA OUTPUT RESPONSE *************************************************");
  trace(event.target.data);
  trace("*********************************************************************************************************************************");
  trace("");

  trace("Data returned by server: " + event.target.data);

  ChatRoom.Text += "\n" + event.target.data.username + " : " + event.target.data.message;

  var loader:URLLoader = event.target as URLLoader;
  loader.removeEventListener(Event.COMPLETE, completeHandler);
  loader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpResponseHandler);
  loader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
  loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
  loader.removeEventListener(Event.OPEN, openHandler);
}

PHP ("save_messages.php" from above "action" var)

<?php

  //Flash will send out name/value pairs in a POST/GET (post recommended)
  if (sizeof($_POST) > 0 || sizeof($_GET) > 0) {
    //If the POST/GET header has elements set success to true and print out the elements
    echo("success=true&");

    //The following simply parrots the values back to flash. Instead of printing out the array you could set up a DB and have them inserted or set up a web proxy to send the data elsewhere. 
    foreach ($_POST as $pKey => $pValue) {
      echo($pKey."=".$pValue."&");
    }

    foreach ($_GET as $gKey => $gValue) {
      echo($gKey."=".$gValue."&");
    }
  }
  else {
    //If the POST/GET header does not contain any information set success to false.
    echo("success=false&");
  }

  //Prints "complete" to let flash know the page has finished processing.
  echo("complete=true");

  //Flash will then read the echoed text which will be something like:
  //success=true&NAME=VALUE&NAME2=VALUE2&complete=true;

  //The "success" and "complete" vars are helpful in testing, please make sure they are accounted for.

?>
相关阅读:
Top