添加“if”语句后设置变量停止

时间:2013-07-05 21:24:57

标签: java variables methods

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

InputHandler.java

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

import adam.miszczak.darkrun.entity.Player;

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

Player.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;

import adam.miszczak.darkrun.InputHandler;

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

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

1 个答案:

答案 0 :(得分:1)

Game.java内将对象player作为InputHandler构造函数中的参数传递,如下所示:

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

}