FinalFrager
FinalFrager

Reputation: 23

Java applet keylistener

I have the following code in Game.java:

public void start ()
{
    Thread thread = new Thread(this);
    thread.start();

    this.world = new World();
}

In the world class, I have the actual items of my game, a set of Walls, food and a player.

public class World {
    private Food food;
    private HashSet<Wall> walls = new HashSet<Wall>();
    private Player player; 
    ...
}

The problem is as follows: I want to have my keylisteners in the player class but I can't seem to figure out how to get this...

I've tried by implementing the KeyListener class in Player.java and implementing the 3 functions that come with that. However, player.java does not allow me to use this.addKeyListener(this) So my keyevents are never triggered...

How can I make this work?

Upvotes: 1

Views: 415

Answers (1)

SLaks
SLaks

Reputation: 887315

You need to call addKeyListener on your Applet / JApplet and pass the (same!) player instance as the listener.

Upvotes: 1

Related Questions