-1

MadProgrammer さん、申し訳ありませんが、KeyBinding を思い通りに動作させることができませんでした :(. KeyListener に接続すると動作します.しかし、今 p.move(); が実際にプレーヤーを動かさないという問題があります. p.move();を除いて、私が入れた他のすべてのコードは正常に動作します.私はおそらくそんなに多くの質問をするべきではないので、やめてほしいならそう言ってください. しかし、SO コミュニティ全体は本当に素晴らしい. 繰り返しますが、コードを投稿します.

メインクラス:

import javax.swing.*;

public class Game extends JFrame{
public static void main(String[] args){
    new Game();
}
public Game(){

    add(new Board());

    setTitle("Hi mom");
    setSize(555,330);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(3);
    setVisible(true);

}
}

ボード クラス:

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

public class Board extends JPanel implements ActionListener {

Image background;
Player p;
boolean moving;

public Board() {

    setFocusable(true);
    requestFocus();

    addKeyListener(new KeyInputEvents());

    Timer timer = new Timer(25, this);
    timer.start();

    ImageIcon img = new ImageIcon(getClass().getResource("images/map.png"));
    background = img.getImage();

    p = new Player();

}

public void paint(Graphics g) {
    g.drawImage(background, 0, 0, null);
    g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
}

public void actionPerformed(ActionEvent e) {
    repaint();
}
public JPanel getBoard(){
    return this;
}
)

Player クラス (これはおそらく問題が発生する場所です):

import javax.swing.*;
import java.awt.*;
public class Player{
int x = 30;
int y = 187;
Image player;
public Player(){
    ImageIcon img = new ImageIcon(getClass().getResource("images/player.png"));
    player = img.getImage();
}
public Image getPlayer(){
    return player;
}
public void move(int x, int y){
    this.x += x;
    this.y += y;
}
public int getX(){
    return x;
}
public int getY(){
    return y;
}
)

KeyInputEvents クラス:

import java.awt.event.*;

import javax.swing.*;

public class KeyInputEvents extends KeyAdapter implements ActionListener{
int k;
boolean moving = true;
Player p = new Player();
public KeyInputEvents(){
    Timer timer = new Timer(25,this);
    timer.start();
}
public void keyPressed(KeyEvent e){
    k = e.getKeyCode();
    moving = true;
}
public void keyReleased(KeyEvent e){
    moving = false;
}
public void actionPerformed(ActionEvent e) {
    if(k == 'D' && moving == true){p.move(5,0);}
    if(k == 'A' && moving == true){p.move(-5,0);}
}

}
4

1 に答える 1

1

Player画面に表示されているものは、クラスPlayerで動いているものとは異なりますKeyInputEvents...

あなたは...Boardのインスタンスを作成しますPlayer

public class Board extends JPanel implements ActionListener {
    Player p;
    public Board() {
        //...
        p = new Player();
    }

そして、KeyInputEventsあなたは別のものを作成します...

public class KeyInputEvents extends KeyAdapter implements ActionListener {
    //....
    Player p = new Player();

これらの 2 つのインスタンスは、まったく関連がありません...

私がここにいる間は、実際にはオーバーライドするべきではありませんがpaint、代わりにオーバーライドpaintComponentして、間違いなく呼び出す必要がありますsuper.paintXxx

キーバインディングの例で更新

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.ImageIcon;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class Game extends JFrame {

    public static void main(String[] args) {
        new Game();
    }

    public Game() {

        add(new Board());

        setTitle("Hi mom");
        setSize(555, 330);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(3);
        setVisible(true);

    }

    public class Board extends JPanel implements ActionListener {

        Image background;
        Player p;
        private int xDelta, yDelta;

        public Board() {

            setFocusable(true);
            requestFocus();

            ImageIcon img = new ImageIcon(getClass().getResource("/images/map.jpg"));
            background = img.getImage();

            p = new Player();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Left.move");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Left.stop");

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Right.move");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Right.stop");

            ActionMap am = getActionMap();
            am.put("Left.move", new MoveLeft(this));
            am.put("Left.stop", new StopAllMovement(this));
            am.put("Right.move", new MoveRight(this));
            am.put("Right.stop", new StopAllMovement(this));

            Timer timer = new Timer(25, this);
            timer.start();

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            g.drawImage(background, 0, 0, null);
            g.drawImage(p.getPlayer(), p.getX(), p.getY(), null);
        }

        protected void setMovement(int xDelta, int yDelta) {
            this.xDelta = xDelta;
            this.yDelta = yDelta;
        }

        public void actionPerformed(ActionEvent e) {
            p.move(xDelta, yDelta);
            repaint();
        }

        public JPanel getBoard() {
            return this;
        }

    }

    public class Player {

        int x = 30;
        int y = 187;
        Image player;

        public Player() {
            ImageIcon img = new ImageIcon(getClass().getResource("/images/player.png"));
            player = img.getImage();
        }

        public Image getPlayer() {
            return player;
        }

        public void move(int x, int y) {
            this.x += x;
            this.y += y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }
    }

    public abstract class AbstractMove extends AbstractAction {

        private Board board;
        private int xDelta;
        private int yDelta;

        public AbstractMove(Board board, int xDelta, int yDelta) {
            this.board = board;
            this.xDelta = xDelta;
            this.yDelta = yDelta;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            board.setMovement(xDelta, yDelta);
        }

    }

    public class MoveLeft extends AbstractMove {

        public MoveLeft(Board board) {
            super(board, -5, 0);
        }

    }

    public class MoveRight extends AbstractMove {

        public MoveRight(Board board) {
            super(board, 5, 0);
        }

    }

    public class StopAllMovement extends AbstractMove {

        public StopAllMovement(Board board) {
            super(board, 0, 0);
        }

    }

}
于 2013-10-15T06:59:56.307 に答える