1

OK so I have this HashMap

private Map<String, Player> players = new HashMap<String, Player>();

Here is what I use to remove:

      public void destroy() {

players.remove("Red");
os.println(me.getUsername() + "|1|has left|yes|chat");

      }

I say Red because it's just a TEST right now. I will get the eventual correct one later. Anyways...

I use THIS to check.

  if (e.getKeyCode() == KeyEvent.VK_Q) {
            for (Player playert : players.values()) {
                                        c.append("\n < "+playert.getUsername() + " > ");
                        }
                    }

When I'm all by myself.. I press Q and I get:

< Dan >

then my friend Red logs in and I Press Q... I get:

< Dan >
< Red >

then he leaves I press Q and I get:

< Dan >
< Red >

So.. how come this isn't working?

Also, here is the code that gets called in init() when a player logs in the game (starts the applet)

 public void playerLogin() throws IOException {

            Random roll = new Random();
            int newNo = roll.nextInt(200);
            // me.getUsername() = "Guest #" + roll.nextInt(110);
            // String me.getUsername() = getParameter("name");

            me = new Player();
            me.setUsername(getParameter("name"));
            me.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
            me.setX(256);
            me.setY(256);
            me.setMap(1);
            me.setCommand("move");
            players.put(me.getUsername(), me);

            repaint();

            System.out.println(me.getUsername() + " was added. player: " + me);
            os.println(me.getUsername() + "|" + me.getX() + "|" + me.getY() + "|"
                        + me.getMap() + "|" + me.getCommand());

            attack = 4;
            defense = 5;
            gold = 542;
            level = 1;
            exp = 53;

      }
4
  • 1
    You should also probably post your code that shows how/when you add the player to your players object when they log in. Commented Aug 18, 2010 at 20:24
  • 2
    And the code for where the destroy method actually gets called. Commented Aug 18, 2010 at 20:25
  • The destory method is called when a user exits the webpage that the applet is on. I edited my post to include what happens when a player logins Commented Aug 18, 2010 at 20:26
  • There is no need to "delete" your question just because it is solved. It can be of help for someone else having a similar problem. Commented Aug 18, 2010 at 22:35

2 Answers 2

2

In other words, your Applet#destroy() method is not called at the moment you expect it is called? You should use Applet#stop(). The destroy() is only called when the object in question is eligible for GC and/or when the whole browser instance is been closed (and thus not only the current page/tab). JVM may namely keep running as long as the browser instance runs.

Sign up to request clarification or add additional context in comments.

2 Comments

its getting called. but the reason it doesnt work now is because im gettigng another error. tansk
Still, you should prefer stop() for that. What exactly is/was the problem? Is it now solved? By the way, now I read your code once more, maybe it's the vague code, but do you realize that the applet instance isn't shared among different visitors at all?
1

When you hit Q... you are checking the contents of players but where is your call to destroy()? Do you explicitly call destroy() anywhere in your code?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.