Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm making a pong clone for Windows Phone 7 and I'd love to add a multiplayer option. I've been trying to use this Ricky Tan's tutorial:

http://blogs.msdn.com/b/rickyt/archive/2011/06/15/windows-phone-peer-to-peer-multiplayer-game-using-sockets-in-xna.aspx

The problem is it is designed around the platformer example which has several classes such as Player, Level and Game whereas my game takes place in a single Game class with much less logic for player movements.

I've tried putting my handleInput() inside the Channel_PacketsReceived method but there's still no connection between devices. I've been at this for days and I can't get it. Is there something I'm missing? I'd love some help with this or if anyone knows a more straight forward tutorial with things happening in the same class.

Also if it matters my game includes the gsm example.

 //Sockets
    private UdpAnySourceMulticastChannel Channel;
    private Vector2 oldPosition;
    int identifier;

 public void HandleInput(int id, Vector2 position)
    {
           TouchCollection touchCollection = TouchPanel.GetState();
           foreach (TouchLocation touchLocation in touchCollection)
           {






               // upper left corner touche detection
               if (touchLocation.Position.Y < 400)
               {

                   if (touchLocation.Position.X < blueBar.X && touchLocation.Position.Y < 400)
                   {
                       blueBar.X -= 30;
                   }

                   else if (touchLocation.Position.X > blueBar.X + 128 && touchLocation.Position.Y < 400)
                   {
                       blueBar.X += 30;
                   }
                   else if (touchLocation.Position.X > blueBar.X && touchLocation.Position.X < blueBar.X + 128 && touchLocation.Position.Y < 400)
                       blueBar.X = (int)touchLocation.Position.X - 64;


               }
               BlueSaber.X = blueBar.X;

               // upper right corner touche detection
               if (touchLocation.Position.Y > 400)
               {

                   if (touchLocation.Position.X < redBar.X && touchLocation.Position.Y > 400)
                   {
                       redBar.X -= 30;
                   }

                   else if (touchLocation.Position.X > redBar.X + 128 && touchLocation.Position.Y > 400)
                   {
                       redBar.X += 30;
                   }
                   else if (touchLocation.Position.X > redBar.X && touchLocation.Position.X < redBar.X + 128 && touchLocation.Position.Y > 400)
                       redBar.X = (int)touchLocation.Position.X - 64;
               }
               RedSaber.X = redBar.X;

               if (touchLocation.Position.X > start.X &&
                   touchLocation.Position.X < start.X + start.Width &&
                   touchLocation.Position.Y > start.Y &&
                   touchLocation.Position.Y < start.Y + start.Height)
               {

                   play = true;
                   touches++;
                   if (redServe)
                   {
                       // default velocity
                       Random rand = new Random();

                       // randomize the ball orientation
                       switch (rand.Next(2))
                       {

                           case 0: ballVelocity.Y = -speed1; ballVelocity.X = speed1; break;

                           case 1: ballVelocity.Y = -speed1; ballVelocity.X = -speed1; break;
                       }
                   }

                   else if (!redServe)
                   {
                       // default velocity
                       Random rand = new Random();

                       // randomize the ball orientation
                       switch (rand.Next(2))
                       {
                           case 0: ballVelocity.Y = speed1; ballVelocity.X = speed1; break;

                           case 1: ballVelocity.Y = speed1; ballVelocity.X = -speed1; break;

                       }
                   }

               }
           }
    }

// Outside of update method

private void InitializeSockets()
    {
        this.Channel = new UdpAnySourceMulticastChannel(IPAddress.Parse("224.109.108.107"), 3007);
        this.Channel.PacketReceived += new EventHandler<UdpPacketReceivedEventArgs>(Channel_PacketReceived);
        this.Channel.Open();
    }

    void Channel_PacketReceived(object sender, UdpPacketReceivedEventArgs e)
    {
        string data = e.Message;
        Console.WriteLine(data);
        string[] pos = data.Split(',');

        //Discard packets that do not match
        if (pos.Length != 5)
        {
            return;
        }

        try
        {
            if (pos[0] != identifier.ToString()) //if not originated from this phone
            {

                    Vector2 position = new Vector2(float.Parse(pos[1]), float.Parse(pos[2]));
                    Vector2 velocity = new Vector2(float.Parse(pos[3]), float.Parse(pos[4]));
                   // UpdateOtherPlayer(int.Parse(pos[0]), position);
                    HandleInput(int.Parse(pos[0]), position);
              }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Caught unexpected exception: " + ex.Message);
        }
    }

    private void SendPosition(string position)
    {
        if (redBar.X != oldPosition.X)
        {
            oldPosition.X = redBar.X;

            this.Channel.Send(position);
        }
    }

and i am calling the SendPosition method inside of update

share|improve this question
add comment

1 Answer

handleInput() should be it's own function, and Channeletc should be it's own function. Try to clarify with sysouts what the program is doing: there is usually a draw area, and an update area. Handling input is done in update, drawing is done in draw, and if youre doing multiplayer then networking is done in its own area. Do not mix these up. Keep them clear and separate.

share|improve this answer
    
That's what I'm doing now but how do I set up the current position and the old position I'll update the question to show my code –  Nexus1216 Jan 6 '12 at 12:54
    
I'm still lost here I'd appreciate any help that anyone can provide. –  Nexus1216 Jan 18 '12 at 1:23
    
So, you are familiar with debugging, and printing output? Check that you are receiving new positions correctly, then check that they are being passed to the entity that wants the new position correctly. Breakpoints, output to console are your friends here so you can figure out what is happening. –  karmington Jan 18 '12 at 21:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.