简体   繁体   English

Java applet密钥侦听器

[英]Java applet keylistener

I have the following code in Game.java: 我在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. 我通过在Player.java中实现KeyListener类并实现其附带的3个功能来进行尝试。 However, player.java does not allow me to use this.addKeyListener(this) So my keyevents are never triggered... 但是,player.java不允许我使用this.addKeyListener(this),所以永远不会触发我的键盘事件...

How can I make this work? 我该如何进行这项工作?

您需要在Applet / JApplet上调用addKeyListener并传递( 相同 !) player实例作为侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM