麻烦问一下,为什么我的这个代码,keypressed运行的时候,按键上下左右没反应呢。
package gremlins;
import processing.core.PApplet;
import processing.core.PImage;
import processing.data.JSONObject;
import processing.data.JSONArray;
import java.util.Random;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.event.KeyListener;
import java.io.*;
public class App extends PApplet implements KeyListener {
public static final int WIDTH = 720;
public static final int HEIGHT = 720;
public static final int SPRITESIZE = 20;
public static final int BOTTOMBAR = 60;
public static final int FPS = 60;
public static final Random randomGenerator = new Random();
public String configPath;
public PImage brickwall;
public PImage stonewall;
private Wizard wizard;
public App() {
this.configPath = "config.json";
}
/**
* Initialise the setting of the window size.
*/
public void settings() {
size(WIDTH, HEIGHT);
}
/**
* Load all resources such as images. Initialise the elements such as the player, enemies and map elements.
*/
public void setup() {
frameRate(FPS);
// Load images during setup
this.stonewall = loadImage(this.getClass().getResource("stonewall.png").getPath().replace("%20", " "));
this.brickwall = loadImage(this.getClass().getResource("brickwall.png").getPath().replace("%20", " "));
//this.gremlin = loadImage(this.getClass().getResource("gremlin.png").getPath().replace("%20", " "));
//this.slime = loadImage(this.getClass().getResource("slime.png").getPath().replace("%20", " "));
//this.fireball = loadImage(this.getClass().getResource("fireball.png").getPath().replace("%20", " "));
JSONObject conf = loadJSONObject(new File(this.configPath));
this.wizard = new Wizard(30, 200, this.loadImage("src/main/resources/gremlins/wizard2.png"));
}
/**
* Receive key pressed signal from the keyboard.
*/
@Override
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if (key == KeyEvent.VK_UP){
System.out.println("this is up");
}else if (key == KeyEvent.VK_DOWN){
this.wizard.moveDOWN();
}else if (key == KeyEvent.VK_LEFT){
this.wizard.moveLEFT();
}else if (key == KeyEvent.VK_RIGHT){
this.wizard.moveRIFHT();
}
}
public void keyTyped(KeyEvent e){
}
/**
* Receive key released signal from the keyboard.
*/
@Override
public void keyReleased(KeyEvent e){
}
/**
* Draw all elements in the game by current frame.
*/
public void draw() {
this.wizard.tick();
this.rect(-1, -1, WIDTH + 2, HEIGHT + 2);
this.wizard.draw(this);
}
public static void main(String[] args) {
PApplet.main("gremlins.App");
}
}
下面是我的wizard里面的代码
package gremlins;
import processing.core.PImage;
import processing.core.PApplet;
public class Wizard {
private int x;
private int y;
private int yVel;
private int xVel;
private PImage wizard;
public Wizard(int x, int y, PImage wizard){
this.x = x;
this.y = y;
this.xVel = 0;
this.yVel = 0;
this.wizard = wizard;
}
public void tick(){
//Handles logic
this.y += this.yVel;
this.x += this.xVel;
this.yVel = 0;
this.xVel = 0;
}
public void draw(PApplet app){
// Handling graphics
app.image(this.wizard, this.x, this.y);
}
public void moveUP(){
this.yVel = -10;
}
public void moveDOWN(){
this.yVel = 10;
}
public void moveLEFT(){
this.xVel = -10;
}
public void moveRIFHT(){
this.xVel = 10;
}
}