placing more than two players in a card game

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

placing more than two players in a card game

Postby capdetrons » 17 Feb 2014, 14:26

Hi,

I'm trying to get started to create a four player card game and this is the code I have in the handleInternalEvent function in the extension. Could anyone tell me if this would be the right way to assign ids to the players?

The original tris example is :

Code: Select all

if (u.getPlayerIndex() == 1)
         p1id = u.getUserId()
      else
         p2id = u.getUserId()


I am trying it like this and I get the error that my code can't cal the method getName in the extension:

(Error in extension [ botifarra.as ]: TypeError: Cannot call method "getName" of undefined (botifarra.as#1627) Internal: 50 -- Line number: 49 in file: botifarra.as)

Code: Select all

if (u.getPlayerIndex() != -1)
   {
      numPlayers++;
         
      if (u.getPlayerIndex() == 1)
         p1id = u.getUserId()
      else if (u.getPlayerIndex() == 2)
         p2id = u.getUserId()
      else if (u.getPlayerIndex() == 3)
         p3id = u.getUserId()
      else if(u.getPlayerIndex() == 4)
         p4id = u.getUserId()

      if(numPlayers == 4 && !gameStarted)
      {
         startGame();
}
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: placing more than two players in a card game

Postby Bax » 19 Feb 2014, 09:00

In the code you posted there's no call to a getName method, so the error must originate somewhere else.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: placing more than two players in a card game

Postby capdetrons » 19 Feb 2014, 12:17

Alright, I got something going... even though I still get the message where the getName method can not get called.

I have all the players in the client and I did seat them correctly recurring to the _global.myID variable for each one, but I keep wondering about the message besides not seeing the player id number in the administrator panel.

extension.png
(58.71 KiB) Not downloaded yet
playersInRoom1.png
(60.57 KiB) Not downloaded yet
playersInRoom2.png
(60.63 KiB) Not downloaded yet
Last edited by capdetrons on 19 Feb 2014, 15:55, edited 1 time in total.
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: placing more than two players in a card game

Postby Bax » 19 Feb 2014, 15:20

The error is telling you that one of those users[xxx] is undefined, while it should return a User object. Therefore, being undefined, it can call the getName method on it.
You have to check where the users array is created, how users are added to it and why one of the elements is undefined.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: placing more than two players in a card game

Postby capdetrons » 19 Feb 2014, 19:35

Hi, thanks Bax... but if it were undefined I wouldn't see that player's name in the client after the startGame function goes on, would I?

What I really wanted to know is whether the code to handle the internal event looked right?

Code: Select all

function handleInternalEvent(evt)
{
   evtName = evt.name
   
   // Handle a user joining the room
   if (evtName == "userJoin")
   {

      if (currentRoomId == undefined)
      {
         currentRoomId = evt["room"].getId()
      }

      u = evt["user"]

      users[u.getUserId()] = u

if (u.getPlayerIndex() != -1)
   {
      numPlayers++;
         
      if (u.getPlayerIndex() == 1)
         p1id = u.getUserId()
      else if (u.getPlayerIndex() == 2)
         p2id = u.getUserId()
      else if (u.getPlayerIndex() == 3)
         p3id = u.getUserId()
      else if(u.getPlayerIndex() == 4)
         p4id = u.getUserId()

      if(numPlayers == 4 && !gameStarted)
      {
         startGame();
}
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: placing more than two players in a card game

Postby Lapo » 19 Feb 2014, 21:30

It seems pretty much right, but in order to know you will have to run it :)
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: placing more than two players in a card game

Postby Bax » 20 Feb 2014, 10:17

Are you sure you are creating a Room of type "game"? Because alle the player IDs are 0, while in a game Room each user (unless they join as spectators) should have an ID set.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: placing more than two players in a card game

Postby capdetrons » 20 Feb 2014, 10:41

Good day,

thanks for replying.

I wouldn't know, but the tris example doesn't show the player's id in the administrator either. Debugged In Flash it does.

But, so far, so good :) Everything seems to be working now. Thanks

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 69 guests