2017-04-09 35 views
0

我正在編寫一個簡單的遊戲,但我無法讓我的mouseListener工作。我的keyListener工作正常,但不是mouseListner。 下面是一些代碼:mouseListener不工作

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

當你說它不起作用,它表現得像它沒有註冊你的mouseEvents,還是不編譯? – noobcoder

+0

未註冊我的mouseEvents –

+0

程序有多長?如果它不是太長,也許你可以發佈整個代碼給我們來幫助你調試:) – noobcoder

回答

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(); 
    } 
} 
} 
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

+0

但它似乎沒有工作。我將按照教程。謝謝 –