my Boolean does not respond , what should I do?

Asked

Viewed 58 times

-3

my code is

public class Game extends Canvas implements Runnable,KeyListener{
private static final long serialVersionUID = 1L;
public static JFrame frame;
private Thread thread;
private boolean isRunning = true;
private final int WIDTH = 240;
private final int HEIGHT = 160;
private final int SCALE = 3;

private BufferedImage image;

private Player player;

public List<Entity> entities;
public Spritesheet spritesheet;



public Game(){
    addKeyListener(this);
    setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
    initFrame();
    //inicializando Objetos
    image = new BufferedImage(WIDTH*SCALE,HEIGHT*SCALE,BufferedImage.TYPE_INT_RGB);
    entities = new ArrayList<Entity>();
    spritesheet = new Spritesheet("/spritesheet.png");
    
    Player player = new Player(0,0,16,16,spritesheet.getSprite(32, 0, 16, 16));
    entities.add(player);
}

public void initFrame(){
    frame = new JFrame("Game");
    frame.add(this);
    frame.setResizable(false);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public synchronized void start(){
    thread = new Thread(this);
    isRunning = true;
    thread.start();
}

public synchronized void stop(){
    
}

public static void main(String args[]) {
    Game game = new Game();
    game.start();
}

public void tick() {
    for(int i = 0 ; i < entities.size(); i++) {
        Entity e = entities.get(i);
        if(e instanceof Player){
            //estou dando tick no player
        }
        e.tick();
    }
}

public void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
        this.createBufferStrategy(3);
        return;
    }
    Graphics g = image.getGraphics();
    g.setColor(new Color(10,10,10));
    g.fillRect(0, 0,WIDTH*SCALE,HEIGHT*SCALE);
    
    /*Renderização do jogo*/
    for(int i = 0 ; i < entities.size(); i++) {
        Entity e = entities.get(i);
        e.render(g);
    }

    /***/
    g.dispose();
    g = bs.getDrawGraphics();
    g.drawImage(image,0,0,WIDTH*SCALE,HEIGHT*SCALE,null);
    bs.show();
}
    
public void run(){
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    int frames = 0;
    double timer = System.currentTimeMillis();
    while(isRunning){
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
      if(delta >= 1){
          tick();
          render();
          frames++;
          delta--;
      }
      if(System.currentTimeMillis() - timer >= 1000){
          System.out.println("FPS:"+frames);
          frames = 0;
          timer+=1000;
      }
    }
}

@Override
public void keyTyped(KeyEvent e) {
    
    
}

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D){
        player.right=true;    
    }else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A){
        
    }
    
    
    if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
        
    }else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
        
    }
    
}

@Override
public void keyReleased(KeyEvent e) {

}
  

}

the player does not answer/recognize the right what I should do?

errors: Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception at.Redstar_games.main.Game.keyPressed(Game.java:143) at java.desktop/java.awt.Component.processKeyEvent(Component.java:6590) at java.desktop/java.awt.Component.processEvent(Component.java:6409) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840) at java.desktop/java.awt.Keyboardfocusmanager.redispatchEvent(Keyboardfocusmanager.java:1950) at java.desktop/java.awt.Defaultkeyboardfocusmanager.dispatchKeyEvent(Defaultkeyboardfocusmanager.java:871) at java.desktop/java.awt.Defaultkeyboardfocusmanager.preDispatchKeyEvent(Defaultkeyboardfocusmanager.java:1140) at java.desktop/java.awt.Defaultkeyboardfocusmanager.typeAheadAssertions(Defaultkeyboardfocusmanager.java:1010) at java.desktop/java.awt.Defaultkeyboardfocusmanager.dispatchEvent(Defaultkeyboardfocusmanager.java:836) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4889) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840) at java.desktop/java.awt.Eventqueue.dispatchEventImpl(Eventqueue.java:772) at java.desktop/java.awt.Eventqueue$4.run(Eventqueue.java:721) at java.desktop/java.awt.Eventqueue$4.run(Eventqueue.java:715) at java.base/java.security.Accesscontroller.doPrivileged(Native Method) java.base/java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:85) java.base/java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:95) at java.desktop/java.awt.Eventqueue$5.run(Eventqueue.java:745) at java.desktop/java.awt.Eventqueue$5.run(Eventqueue.java:743) at java.base/java.security.Accesscontroller.doPrivileged(Native Method) java.base/java.security.Protectiondomain$Javasecurityaccessimpl.doIntersectionPrivilege(Protectiondomain.java:85) at java.desktop/java.awt.Eventqueue.dispatchEvent(Eventqueue.java:742) at java.desktop/java.awt.Eventdispatchthread.pumpOneEventForFilters(Eventdispatchthread.java:203) at java.desktop/java.awt.Eventdispatchthread.pumpEventsForFilter(Eventdispatchthread.java:124) at java.desktop/java.awt.Eventdispatchthread.pumpEventsForHierarchy(Eventdispatchthread.java:113) at java.desktop/java.awt.Eventdispatchthread.pumpEvents(Eventdispatchthread.java:109) at java.desktop/java.awt.Eventdispatchthread.pumpEvents(Eventdispatchthread.java:101) at java.desktop/java.awt.Eventdispatchthread.run(Eventdispatchthread.java:90)

1 answer

0


Vc has a global variable called player which is null and is the variable being used in the method keyPressed()

public Game(){
    ...
    
    Player player = new Player(0,0,16,16,spritesheet.getSprite(32, 0, 16, 16));
    entities.add(player);
}

In your class Game You’re not using the global variable, you’re creating a new one, you know? To correct this, an alternative would simply be to use the global variable:

 public Game(){
    ...
    
    player = new Player(0,0,16,16,spritesheet.getSprite(32, 0, 16, 16));
    entities.add(player);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.