我正在编写一个简单的游戏,但我无法让我的mouseListener工作。我的keyListener工作正常但不是mouseListner。 这是一些代码:
public Game(String title, int width, int height){
this.falling = true;
this.width = width;
this.height = height;
this.title = title;
keyManager = new KeyManager();
mouseInput = new MouseInput();
}
private void init(){
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
display.getFrame().addMouseListener(mouseInput);
display.getFrame().addMouseMotionListener(mouseInput);
}
在mouseInput类中:
public class MouseInput extends MouseAdapter{
private int x;
private int y;
@Override
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
}
public int getX(){
return x;
}
public int getY(){
return y;
}}
答案 0 :(得分:0)
package infiniterunner;
import infiniterunner.gfx.ImageLoader;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Game implements Runnable{
private Display display;
public int width, height;
public String title;
private boolean running;
private Thread thread;
public BufferStrategy bs;
public Graphics g;
Font header = new Font("default", Font.BOLD, 50);
Font button = new Font("default", Font.BOLD, 40);
private final int margin=200;
private final int dyPlate=1;
private int velY=1;
private int dxBall=6;
private int wentBy=0;
private int xMargin;
private int gameOverY=250;
private int background=1;
private float alpha1 = 0.0f;
private float alpha2 = 0.0f;
private float alpha3 = 0.0f;
private boolean falling;
private boolean start;
private boolean everyOther;
private boolean gameOver;
private boolean fadeIn=true;
//Input
private final KeyManager keyManager;
private final MouseInput mouseInput;
Plate[] plate = new Plate[1000];
Ball ball;
//Bilder
private BufferedImage briffaBackground1;
private BufferedImage briffaBackground2;
public Game(String title, int width, int height){
this.falling = true;
this.width = width;
this.height = height;
this.title = title;
keyManager = new KeyManager();
mouseInput = new MouseInput();
}
private void init(){
display = new Display(title, width, height);
display.getFrame().addKeyListener(keyManager);
display.getFrame().addMouseListener(mouseInput);
display.getFrame().addMouseMotionListener(mouseInput);
briffaBackground1 =
ImageLoader.loadImage("/textures/briffaBackground1.png");
briffaBackground2 =
ImageLoader.loadImage("/textures/briffaBackground2.png");
//DrawPlate
for(int f=0; f<plate.length; f++){
plate[f] = new Plate((int) (Math.random() * 420),600-margin*f, 80,
10);
}
ball = new Ball(30, 30, 250, 300);
}
private void tick(){
keyManager.tick();
System.out.println(mouseInput.getX());
if(keyManager.space){
start=true;
}
if(start){
if(keyManager.left&&ball.getxPos()>0){
int x = ball.getxPos();
x-=dxBall;
ball.setxPos(x);
}else if(keyManager.right&&ball.getxPos()<500-30){
int x = ball.getxPos();
x+=dxBall;
ball.setxPos(x);
}
//Rörelse
for (Plate plate1 : plate) {
int newX = plate1.getyPos() + velY;
plate1.setyPos(newX);
}
//Collision Detection
if(falling){
for(int i=wentBy; i<plate.length; i++){
if(ball.getBounds().intersects(plate[i].getBounds())&&velY<=0){
falling=false;
}
}
}
//Launch
if(falling){
everyOther=!everyOther;
if(everyOther){
velY-=dyPlate;
}
}else{
velY=10;
xMargin++;
if(xMargin>20){
falling=true;
xMargin=0;
}
}
if(plate[wentBy].getyPos()<0){
gameOver=true;
}
}
}
private void render(){
bs = display.getCanvas().getBufferStrategy();
if(bs == null){
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//ClearScreen
g.clearRect(0, 0, width, height);
//Draw
switch(background){
case 1:
g.drawImage(briffaBackground1, 0, 0, null);
break;
case 2:
g.drawImage(briffaBackground2, 0, 0, null);
break;
}
if(wentBy>5){
background=2;
}
if(!gameOver){
for(int i=wentBy; i<10+wentBy; i++){
if(plate[i].getyPos()>800){
wentBy++;
}
plate[i].paint(g);
ball.paint(g);
}
}else{
gameOver(g);
btnRestart(g);
btnMainmenu(g);
}
//End Draw
bs.show();
g.dispose();
}
public void gameOver(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha1));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.
VALUE_ANTIAL
IAS_ON);
if(alpha1<0.9f){
alpha1+=0.02f;
}
if(gameOverY>200){
gameOverY--;
}
g.setFont(header);
g.drawString("GAME OVER", 100, gameOverY);
}
public void btnRestart(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha2));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints
.VALUE_ANTIALIAS_ON);
if(gameOverY==200){
if(alpha2<0.999f){
alpha2+=0.01;
}
g.setColor(Color.DARK_GRAY);
g.fillRect(125, 250, 250, 70);
g.setColor(Color.WHITE);
g.setFont(button);
g.drawString("Restart", 180, 298);
}
}
public void btnMainmenu(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha3));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints
.VALUE_ANTIALIAS_ON);
if(alpha2>0.15f){
if(alpha3<0.999f){
alpha3+=0.01;
}
g.setColor(Color.DARK_GRAY);
g.fillRect(125, 370, 250, 70);
g.setColor(Color.WHITE);
g.setFont(button);
g.drawString("Main Menu", 150, 418);
}
}
@Override
public void run(){
init();
int fps=60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
while(running){
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
lastTime = now;
if(delta >= 1){
tick();
render();
delta--;
}
}
stop();
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
虽然我要求您发布代码,但我可能已经找到了您的问题。我从未使用过mouseMotionListener但它应该是这样的:
private class MyListener extends MouseInputAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
currentRect = new Rectangle(x, y, 0, 0);
updateDrawableRect(getWidth(), getHeight());
repaint();
}
public void mouseDragged(MouseEvent e) {
updateSize(e);
}
public void mouseReleased(MouseEvent e) {
updateSize(e);
}
void updateSize(MouseEvent e) {
int x = e.getX();
int y = e.getY();
currentRect.setSize(x - currentRect.x,
y - currentRect.y);
updateDrawableRect(getWidth(), getHeight());
Rectangle totalRepaint = rectToDraw.union(previouseRectDrawn);
repaint(totalRepaint.x, totalRepaint.y,
totalRepaint.width, totalRepaint.height);
}
}
似乎mouseDragged()是一个函数,同时调用了mousePressed()。所以mousePress()存储了拖动开始的坐标,mouseDragged()更新了鼠标的坐标,最后是mouseRelease();
找到完整的教程here。