A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: limit number of player in multiplayer game

  1. #1
    Member
    Join Date
    Oct 2004
    Posts
    40

    limit number of player in multiplayer game

    Problem:
    I have been asked to make a "X and Os" multiplayer game. As we all know this game requiered only 2 players.

    What I have come up with:
    The way I have started to code it, is by giving a gameID to all the players when they connect to the server. Everytime I have two player with same game id, the game can starts.
    The idea is that Everytime one player does a move, it terlls the server which then will send all clients the move (with method :application.users_so.send()) ; each client should then check wether it is a move of their oponent (by checking the client gameID) and react accordingly.

    Not too happy with my solution / questions:
    -- Is there a way to only send the oponent client rather than all the clients (something like application.users_so[clientID].send)?

    -- Should each game be a in a separate "room"? If so how would I do that?


    Help is much appriciated
    Thank you
    geraldine

  2. #2
    Senior Member
    Join Date
    Oct 2002
    Posts
    273
    Quote Originally Posted by geraldineB
    -- Is there a way to only send the oponent client rather than all the clients (something like application.users_so[clientID].send)?

    Yes... there's a way. At the beginning of the game, create a shared object that only the 2 players will connect to. Then use sharedObject.send to send your call over that shared object. That way, only the users connected to the SO will get the call.


    -- Should each game be a in a separate "room"? If so how would I do that?
    Depends on your how your application is structured, how many people will connect to the application concurrently, and whether you need any communication between the separate games (score tracking, for example). If you plan to have lots of games going on at once, I would create separate instances of the game. Otherwise, you can manage your separate games within the same instance of the application using shared objects.

    Perhaps if you explain your application in a little more detail, I can offer more advice.


    Help is much appriciated
    Thank you
    geraldine
    Last edited by JayCharles; 11-08-2005 at 05:10 PM.

  3. #3
    Member
    Join Date
    Oct 2004
    Posts
    40
    This is some kind of R&D project, and to say the truth I am not sure where it will go...

    I think that for now the first solution will do, but I can't figure out how to create dynamically the different shareObjects and have both client and server aware of them

    In all the example I viewed, the name of the shareObject is hard coded in both client and server side:

    client code has:
    users_so=SharedObject.getRemote("users_so", _root.client_nc.URI, 0)
    // attach the sharedObject to client_nc
    users_so.connect(_root.client_nc);


    server code has:
    // get the user.so shared object
    application.users_so=SharedObject.get("users_so", 0);


    Trying to figure it out...Working on it...
    Would love the method if you have some time...
    Thank you for your help
    geraldine

  4. #4
    Member
    Join Date
    Oct 2004
    Posts
    40
    I now figured out to create and access the sharedobject created for each game.


    Thank's again for your help.

    geraldine

  5. #5
    Senior Member
    Join Date
    Oct 2002
    Posts
    273
    The name of a shared object does not have to be hardcoded, as you can use variables (both on the client side and the server side) to name the shared object.

    Server side example:
    Code:
    myGameIdentifer = Math.round(Math.random()*100);
    myGame = "game_" + myGameIdentifier;
    my_so = SharedObject.get(myGame, false);
    Now we have a nonpersistent shared object named "game_xxxx" where xxxx will be a random number between 1 and 999

    Now comes the all important question... how do I connect the client to the shared object, if the name of the object isn't in the actionscript on the client side?

    The way I handle it is to have the client call a function on the server using netconnection.call . The function on the server will create the shared object, and return the object's name to the client. The client will then connect up to the shared object. For example (on the server)...
    Code:
    Client.Prototype.makeNewGame = function(){
         myGameIdentifer = Math.round(Math.random()*100);
         myGame = "game_" + myGameIdentifier;
         my_so = SharedObject.get(myGame, false);
         return myGame;
    }
    and on the client (example assumes your netconnection is named "nc")...

    Code:
    var res = new Object();
    res.onResult = function(myGame){
         // Add error checking here
         syncronizeSO(myGame);
    }
    
    function syncronizeSO(myGame){
         myGame_so = sharedObject.getRemote(myGame, nc.uri, false);
         // add SO status events here
    }
    
    nc.call("makeNewGame", res);
    So, what's happening here is that the client calls the server, the server creates the shared object and returns the SO name to the client, and the client side result handler fires the function for the client to connect to the SO.

    Now comes the next part... we need to figure out how to connect the second user. To do this, we first need to determine how we are "inviting" the second user to connect. As I see things, we have two options:

    1. Have a list of connected users, and allow the initiator of the shared object to invite another user to connect -or-

    2. Notify all of the other users that the object is available and let them choose to connect

    Once we have that figured out, we can determine how the rest of the process could go.

  6. #6
    Senior Member
    Join Date
    Oct 2002
    Posts
    273
    Quote Originally Posted by geraldineB
    I now figured out to create and access the sharedobject created for each game.


    Thank's again for your help.

    geraldine
    Glad to hear you have it sorted.

  7. #7
    Member
    Join Date
    Oct 2004
    Posts
    40
    The method I used is a bit different, and I'd like to make sure that it is not bad coding.
    In the server code I initialise the gameID to 0.
    When the first user connect, the server call the client to pass on the current game available for joining

    server:
    application.onAppStart = function() {
    application.nextID=0;
    application.gameid=0;
    application.gameList=new Array();
    }

    application.onConnect=function (newClient, name) {
    -- give id to the client
    -- create client object to add to share object

    -- figure share object name :
    shareObjectName = "user_so" + gameID

    -- add client object to share object

    -- accept connection
    -- call client and pass shareObject name to client
    newClient.call ("setShareObject", null, shareObjectName);

    -- add playerID in application.gameList:
    if first player:
    application.gameList[gameID][0]=client1 id
    if there is already a player in the list:
    application.gameList[gameID][1]=client2 id

    -- check how many player are connected to this game (check length of application.gameList[gameID]), if 2 players, increment application.gameID

    }

    on the client side

    client_nc.setShareObject = function(shareObjectName){
    trace ("getting share object" + shareObjectName );
    _root.shareObjectName=shareObjectName;
    connectionEstablished ()
    }

    function connectionEstablished () {
    trace ("connection established");
    // create remote shared object and connect to it
    users_so=SharedObject.getRemote(_root.shareObjectN ame, _root.client_nc.URI, 0);

    // attach the sharedObject to client_nc
    users_so.connect(_root.client_nc);


    // add a onSync handler so when the users_so is updated the userList also update
    users_so.onSync = function() {
    -- if I have two users, I am ready to start the game
    -- if I have one user, I tell the client to wait for an oponent
    }

    So basically, I do not give much choice to the player as to which game he is going to play; it is kind of a "first arrived, first served".


    Could you please let me know if you see some fault in my approch in creating the shareObjects.

    The other thing I was thinking about, was that I should probably create a Game class to deal with the actual players and moves rather than having the main script delaing with all of it. Could my game class be in charge of creating the ShareObjects for the game? Do you know of any example I could have a look at?

    Thank again for all your help.
    geraldine

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center