私のプロジェクトの学校では、ゲームを作る必要があります。Java(KeyPressed)でオブジェクトを移動する
私は矢印キーでオブジェクトを移動しようとしていますが、実行してキーを押すと何も起こりません。
addKeyListener(this);
を間違った場所に実装したことがありますか?
package VoorbeeldSpel;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.*;
public class PlayPanel extends JPanel implements ActionListener, KeyListener {
private static final long serialVersionUID = 584263623777101573 L;
final static String IMAGE_FOLDER = "images/";
final static String FILE_FOLDER = "files/";
private Timer spelTimer = new Timer(50, this); // elke 1000 milliseconden zal een event gelanceerd worden
private JButton menuKnop;
private MijnProject mainVenster;
MusicPlayer music;
private Image ninjaImage;
private Image BackgroundImage;
public int pos_x, pos_y;
public int v_x, v_y;
private String inputText = "";
public PlayPanel(MijnProject mainVenster) {
this.mainVenster = mainVenster;
menuKnop = new JButton("Menu");
menuKnop.addActionListener(this);
this.add(new JLabel("We are playing ... "));
this.add(menuKnop);
addKeyListener(this);
// Music player laden
music = new MusicPlayer("nyancat-loop.wav", true);
// Prentjes laden
ninjaImage = new ImageIcon(getClass().getResource(IMAGE_FOLDER + "ninja.png")).getImage();
BackgroundImage = new ImageIcon(getClass().getResource(IMAGE_FOLDER + "Backgroundmenu.jpg")).getImage();
spelTimer.start(); // hier starten we de gameloop
}
// we gaan init aanroepen nadat we dit JPanel aan het frame hebben toegevoegd
// op dit moment zijn de hoogte en breedte van het panel bepaald en kunnen we de pos_x en pos_y variabelen initialiseren als het midden van het scherm
public void init() {
addKeyListener(this);
v_x = ;
v_y = 0;
pos_x = getWidth()/2;
pos_y = getHeight()/2;
addKeyListener(this);
}
public void gameloop() {
pos_x += v_x;
pos_y += v_y;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(BackgroundImage, this.getWidth()/50, this.getHeight()/50, mainVenster.getWidth(), mainVenster.getHeight(), this);
g.drawImage(ninjaImage, pos_x, pos_y, 60, 60, this);
g.drawChars(inputText.toCharArray(), 0, inputText.length(), 25, getHeight()/2);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == menuKnop) {
mainVenster.switchPanel();
music.stop();
spelTimer.stop(); // stoppen van het spel
} else if (ae.getSource() == spelTimer) {
gameloop();
addKeyListener(this);
System.out.println("De volgende stap van het spel wordt uitgevoerd...");
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
v_x = 0;
v_y = -5;
case KeyEvent.VK_DOWN:
v_x = 0;
v_y = 5;
case KeyEvent.VK_RIGHT:
v_x = 5;
v_y = 0;
case KeyEvent.VK_LEFT:
v_x = -5;
v_y = 0;
}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
int Keycode = e.getKeyCode();
switch (Keycode) {
case KeyEvent.VK_UP:
v_x = 0;
v_y = 0;
case KeyEvent.VK_DOWN:
v_x = 0;
v_y = 0;
case KeyEvent.VK_RIGHT:
v_x = 0;
v_y = 0;
case KeyEvent.VK_LEFT:
v_x = 0;
v_y = 0;
}
repaint();
}
}
各ループの繰り返しでキーリスナーを追加する必要があるのではないかと思います。リスナーが多すぎます(内部コードがうまくいけば、あなたの嘆願を無視し、リスナーは1つしかありません)。 –