java - KeyListener无法检测到碰撞(平台游戏)

时间:2015-03-10 18:32:02

标签: java swing keylistener

我正在制作Java游戏,完全没有任何外部库或引擎。一切都很好,但我遇到了KeyListener的问题。我不明白为什么会这样。

每当我使Character Object(使用velX扩展Character(我编码的另一个自定义类)并使用自定义碰撞检测设置动画时,它的工作正常,{{1}只要Character ObjectBlock Object发生碰撞,就会停止移动;即。

ObjectController

Character c = new Character();

字符(extends Object

public void checkCollision(LinkedList<Object> objects){
    for (int i = 0; i < objects.size(); i++) {
        if(this.bounds.getRCol().intersects(objects.get(i).bounds.getLCol())){
            setVelX(0);
        }
    }
}

对象

public void tick(LinkedList<Object> objects){
    x += velX;
    y += velY;
    checkCollision(objects)
}

KeyListener(工作版,但我希望在释放密钥后动画停止)

public void keyPressed(KeyEvent e){
    c.setVelX(1);
}

public void keyReleased(KeyEvent e){
    // nothing 
}

KeyListener(此版本有效,按下按键时动画开始,释放按键时停止,但碰撞检测不起作用)

public void keyPressed(KeyEvent e){
    c.setVelX(1);
}

public void keyReleased(KeyEvent e){
    c.setVelX(0);
}

我已经正确地编写了每个班级,或者至少我感觉如此。左右碰撞矩形正确放置在所有Object s。

那为什么这不起作用?我做错了吗?或者是Swing的KeyListener的错?

非常感谢任何帮助。如果您需要更多信息,请在下面发表评论,我很乐意提供更多信息!

谢谢,ProgrammersDude。

编辑 - 更新1

有一个名为Panel.class的类,基于JPanel它是所有面板的主要容器。

然后还有一个Subpanel.class,它也会扩展JPanel。它是一个抽象类,在该类的内部调用tick()

package com.platformer.main;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public abstract class Subpanel extends JPanel implements Runnable{
    private static final long serialVersionUID = 1L;

    public boolean loaded = false;
    public Thread loop = null;

    public Subpanel(){
        this.setFocusable(true);
        this.requestFocusInWindow();

        init();

        loop = new Thread(this);
        loop.start();
    }

    public void init(){
        load();
        loaded = true;
    }

    public abstract void load();

    public void run(){
        while(loaded){
            tick();
            repaint();
            try{
                Thread.sleep(17);
            }catch(InterruptedException e){

            }
        }
    }

    public abstract void tick();

    BufferedImage buffer;
    Graphics bg;

    public void paint(Graphics g){
        buffer = (BufferedImage) this.createImage(Start.WIDTH,    Start.HEIGHT);
        bg = buffer.getGraphics();
        paintComponent(bg);
        g.drawImage(buffer, 0, 0, null);
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        if(loaded){
            render(g2);
        }
    }

    public abstract void render(Graphics2D g);
    }
}

Gamepanel.class内部看起来像这样:

package com.platformer.subpanels;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import com.platformer.input.GameKeys;
import com.platformer.main.Start;
import com.platformer.main.Subpanel;
import com.platformer.objects.ObjectController;

public class Gamepanel extends Subpanel{
    private static final long serialVersionUID = 1L;

    ObjectController objectController;

    GameKeys key;

    public void load(){
        objectController = new ObjectController();
        this.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e){
                objectController.character.setVelX(1);
            }
            public void keyReleased(KeyEvent e) {
                objectController.character.setVelX(0);
            }
        });
    }

    public void tick(){
        objectController.tick();
    }

    public void render(Graphics2D g){
        g.setColor(Color.black);
        g.fillRect(0, 0, Start.WIDTH, Start.HEIGHT);

        objectController.render(g);
    }
}

GameKeys是我以前使用的KeyListener。现在在您看到的代码中,我使用自定义KeyAdapter来测试它。它仍然无法运作。

GameKeys.class看起来像这样:

package com.platformer.input;
import java.awt.event.KeyEvent;
import com.platformer.objects.ObjectController;
import com.platformer.subpanels.Gamepanel;

public class GameKeys extends KeyInput{

    private ObjectController objectController;
    private com.platformer.objects.Character character;

    public GameKeys(Gamepanel panel, ObjectController objectController){
        super(panel);
        this.objectController = objectController;
        this.character = this.objectController.character;
    }

    public void keyPressed(KeyEvent e){
        switch(e.getKeyCode()){
        case KeyEvent.VK_ESCAPE:
            System.exit(0);
        case KeyEvent.VK_RIGHT:
            character.setVelX(1);
        }
    }

    public void keyReleased(KeyEvent e){
        switch(e.getKeyCode()){
        case KeyEvent.VK_RIGHT:
            character.setVelX(0);
        }
    }

    public void keyTyped(KeyEvent e) {

    }
}

它基于KeyInput.class

package com.platformer.input;
import java.awt.event.KeyListener;
import javax.swing.JPanel;

public abstract class KeyInput implements KeyListener{
    public KeyInput(JPanel panel){
        panel.addKeyListener(this);
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了一种解决方法。我添加了一个简单的布尔值,

public void isCollision(){
    return collision;
}

collision对象适合:

for (int i = 0; i < objects.size(); i++) {
    if(this.bounds.getRCol().intersects(objects.get(i).bounds.getLCol())){
       collision = true;
       setVelX(0);
    }else{collision = false;}
}

然后从GameKeys.class修改了这个:

public void keyPressed(KeyEvent e){
    if(!objectController.character.isCollision()){
        objectController.character.setVelX(1);
    }
}