2013-12-24 63 views
0

我有一個菜單按鈕,一旦點擊它就會轉換到遊戲狀態,但它不起作用。這是我的MouseInput類。忽略除MousePressed方法外的所有方法。菜單按鈕不起作用?

package com.game.src.main; 

import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

public class MouseInput implements MouseListener 
{ 
    @Override 
public void mouseClicked(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseEntered(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void mouseExited(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

public void mousePressed(MouseEvent e) 
{ 
    int mx = e.getX(); 
    int my = e.getY(); 

    /** 
    public Rectangle playButton = new Rectangle(Game.WIDTH/2 + 120, 150, 100, 50); 
    public Rectangle helpButton = new Rectangle(Game.WIDTH/2 + 120, 250, 100, 50); 
    public Rectangle quitButton = new Rectangle(Game.WIDTH/2 + 120, 350, 100, 50); 
    */ 

    if (mx >= Game.WIDTH/2 + 120 && mx <= Game.WIDTH/2 + 220) 
    { 
     if (my >= 150 && mx <= 200) 
      Game.State = Game.STATE.GAME; 
    } 

} 

@Override 
public void mouseReleased(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 

} 

這裏是init方法,它是Game類的一部分。我加粗和斜體的代碼行,我認爲使用addMouseListener方法來啓用鼠標功能(但顯然它不起作用)。

public static enum STATE 
{ 
    MENU, 
    GAME 
}; 

public static STATE State = STATE.MENU; 

public void init() 
{ 
    requestFocus(); 
    BufferedImageLoader loader = new BufferedImageLoader(); 
    try 
    { 
     spriteSheet = loader.loadImage("/spritesheettemplate.png"); 
     background = loader.loadImage("/bkg.png"); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

    addKeyListener(new KeyInput(this)); 
    ***addMouseListener(new MouseInput());*** 

    tex = new Textures(this); 

    p = new Player(WIDTH/2*SCALE - 16, HEIGHT/2*SCALE - 16, tex); 
    c = new Controller(this, tex); 

    menu = new Menu(); 

    eA = c.getEntityA(); 
    eB = c.getEntityB(); 

    c.addEnemy(enemyCount); 
} 

任何幫助將不勝感激。這是我比賽中的最後一步。我正在關注Youtube上的一個教程系列,但上傳者從不回答問題,所以你們是我唯一的希望(我想我有點戲劇性)。如果你需要我發佈我的代碼的更多部分,我可以做到這一點。

+0

當你發現你的'MouseListener'方法有一半是空的,切換到'MouseAdapter' – MultiplyByZer0

+0

_「我有一個菜單按鈕」 _ ..爲什麼不使用'ActionListener'? –

回答

2

只需將ActionListener添加到該菜單按鈕而不處理MouseListener。

button.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) 
      { 
       Game.State = Game.STATE.GAME; 
      } 
     });