簡體   English   中英

Java鍵綁定和箭頭鍵

[英]Java Key Bindings and Arrow Keys

我正在與一個由箭頭鍵控制的二維數組中移動的玩家編寫一個小型的自頂向下游戲。
我一定已經看過有關鍵綁定的所有教程,但是我無法獲取用於更新主要“地板”對象的鍵綁定。 救命! 很抱歉,我不能相信這三個文件的上下文。 謝謝!

編輯:SSCCE

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package palace.hero;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class SSCCE 
{
    public static void main(String[] args) 
    {    
        JPanel gridPanel = new JPanel();

        int xCoord = 0;
        int yCoord = 0;

        //Key Bindings        
        gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "up");
        gridPanel.getActionMap().put("up", new SSCCEKA(xCoord, yCoord, "up"));
        gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "down");
        gridPanel.getActionMap().put("down", new SSCCEKA(xCoord, yCoord, "down"));
        gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
        gridPanel.getActionMap().put("left", new SSCCEKA(xCoord, yCoord, "left"));
        gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");
        gridPanel.getActionMap().put("right", new SSCCEKA(xCoord, yCoord, "right"));

        //Window
        JFrame window = new JFrame("Window");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int windowHeight = 1125;
        int windowWidth = 900;
        window.setPreferredSize(new Dimension(windowHeight, windowWidth));

        window.add(gridPanel);
        window.pack();
        window.setVisible(true);

        gridPanel.requestFocusInWindow();
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package palace.hero;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class SSCCEKA extends AbstractAction
{
    String direction;
    int x = 0;
    int y = 0;

    public SSCCEKA(int x, int y, String direction) 
    {
        this.x = x;
        this.y = y;
    }

    @Override
    public void actionPerformed(ActionEvent ae) 
    {
        if(direction.toLowerCase().equals("up"))
        {
            x++;
        }
        if(direction.toLowerCase().equals("down"))
        {
            x--;
        }
        if(direction.toLowerCase().equals("left"))
        {
            y--;
        }
        if(direction.toLowerCase().equals("right"))
        {
            y++;
        }
    }
}

當您創建像游戲這樣復雜的內容時,應使用模型/視圖/控制器模式

因此,讓我們創建一個模型類。

public class GameModel {

    private int xCoordinate;
    private int yCoordinate;

    public int getxCoordinate() {
        return xCoordinate;
    }

    public void setxCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    public int getyCoordinate() {
        return yCoordinate;
    }

    public void setyCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

}

您可以通過構造函數將GameModel類傳遞給SSCCEKA類。

請記住,只有控制器類可以修改游戲模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM