-1

我有一个输入类。当按下它设置 player.hasJumped = true; 当 hasJumped 为真时,它在播放器类中将 dy 更改为 -3。问题是,hasJumped 被设置为 true,但变量没有改变。我真的不明白为什么会这样。这是我的代码的简短示例,如果需要,我可以提供更多。

输入处理程序.java

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class InputHandler implements KeyListener{

Player player = new Player();

private boolean[] keys = new boolean[120];
public boolean up;

public void tick(){
    up = keys[KeyEvent.VK_UP] || keys[KeyEvent.VK_W];
    
    if(player.jumpReady){
        if(up){
            player.inAir = true;
            System.out.println("JUMPED " + player.inAir);
        }
    }
}

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;
}

播放器.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Player {

int x;
int y;
int dx;
public long xElapsed = 0;
public int dy;
int width = 50;
int height = 75;
public int hangTime = 0;
int maxHangTime = 100;
int nextX = 1000;
public int playerSpeed = 7;
public int coinsCollected = 0;
public boolean isMoving = true;
public boolean hasJumped = false;
public boolean jumpReady = true;
public boolean hasCollided = false;
public boolean inAir = false;
private BufferedImage moving1 = null;
private String path1 = "res/player_moving_1.png";

public Player(){
    try {
        moving1 = ImageIO.read(new File(path1));
    }catch (IOException e) {
        e.printStackTrace();
        System.err.println("Could not load image " + moving1 + " at path" + path1);
    }

    x = 150;
    y = 250;
}

public void render(Graphics g){
     Graphics2D g2d = (Graphics2D) g;
     g2d.drawImage(moving1, x, y, null);
}

public void move(){
    x = x + dx;
    y = y + dy;

    xElapsed++;
    
    if(xElapsed == nextX){
        upSpeed();
        nextX += 1000;
    }
    
    if(inAir){
        System.out.println("test");
        dy = -3;
    }
}   
    
public void paint(Graphics g){
    g.setColor(Color.RED);
    g.fillRect(x, y, width, height);
}

public void upSpeed(){
    playerSpeed++;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}   

public long getXElapsed(){
    return xElapsed;
}

public double getSpeed(){
    return playerSpeed;
}

public int getNextX(){
    return nextX;
}

public int getCoinsCollected(){
    return coinsCollected;
}

public Rectangle bounds(){
    return (new Rectangle(x, y, width, height));
    }
}

游戏.java

    private static final long serialVersionUID = 1L;
public static final int WIDTH = 350;
public static final int HEIGHT = WIDTH / 16 * 9;
public static final int SCALE = 2;

private Thread thread;
private JFrame frame;
private InputHandler input;

private boolean running = false;

private Screen screen;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

Player player = new Player();    


public Game() {
    Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
    setPreferredSize(size);
    
    screen = new Screen(WIDTH, HEIGHT);
    frame = new JFrame();
    input = new InputHandler();
    
    addKeyListener(input);
    setFocusable(true);
}


public void run() {
    long lastTime = System.nanoTime();
    long timer = System.currentTimeMillis();
    final double ns = 1000000000.0 / 60.0;
    double delta = 0;
    int frames = 0;
    int ticks = 0;

    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        
        while(delta >= 1){
            tick();
            ticks++;
            delta--;
        }
        render();
        frames++;
        
        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println(ticks + " tps, " + frames + " fps");
            ticks = 0;
            frames = 0;
        }
    }
    stop();
}

public void tick(){
    input.tick();
    player.move();
    collision();
    obstacleHole.move();
    obstacleWolf.move();
    coin.move();
}
4

1 回答 1

1

在构造函数中将对象作为参数Game.java传递,如下所示:playerInputHandler

public Game() {
    Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
    setPreferredSize(size);

    screen = new Screen(WIDTH, HEIGHT);
    frame = new JFrame();
    input = new InputHandler(player);//pass player object as argument

    addKeyListener(input);
    setFocusable(true);
}

并将InputHandler类更改如下:

public class InputHandler implements KeyListener{

Player player ; //Don't initialize it.

//Create a parametric constructor
public InputHandler(Player player)
{
  this.player = player;
}
//...And then proceed with other stuffs

}
于 2013-07-05T21:54:52.977 回答