GUJ Discussões   :   últimos tópicos   |   categorias   |   GUJ Respostas

Iniciante em Programação Java de games

Tags: #<Tag:0x00007f289b5a9478> #<Tag:0x00007f289b5a8dc0> #<Tag:0x00007f289b5a8b40>

Sou novato e estou começando a criar meu primeiro jogo,mas quando adiciono os botões para controlar a personagem da erro, alguém pode ajudar?? o erro é o que vem logo abaixo, vou postar o código da classe principal, se precisar das outras classes mando também, tem a classe player e entidade

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException: Cannot assign field “right” because “this.player” is null
at com.TenebrisGames.main.Game.keyPressed(Game.java:148)
at java.desktop/java.awt.Component.processKeyEvent(Component.java:6572)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:870)
at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1139)
at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1009)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:835)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4871)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
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(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at 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(AccessController.java:391)
at 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)

package com.TenebrisGames.main;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import com.TenebrisGames.entities.Entity;
import com.TenebrisGames.entities.Player;
import com.TenebrisGames.graphics.Spritesheet;

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 = 700;
private final int HEIGHT = 400;
private final int SCALE = 2;

private BufferedImage image;	

public List<Entity> entities;
public Spritesheet character;

private Player player;

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

public void initFrame() {
	frame = new JFrame("Stolen my barbecue");
	frame.add(this);
	frame.setResizable(false);
	frame.pack();
	frame.setLocationRelativeTo(null);
	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() {
	isRunning = false;
	try {
		thread.join();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	
}

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

public void render() {
	BufferStrategy bs = this.getBufferStrategy();
	if(bs == null) {
		this.createBufferStrategy(3);
		return;
	}
	Graphics g = image.getGraphics();
	g.setColor(new Color(19,19,19));
	g.fillRect(0, 0, WIDTH, HEIGHT);
	
	/*rendereização do jogo*/
	//Graphics2D g2 = (Graphics2D) g;
	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;
		}
	}
	
	stop();
}

@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;
		System.out.println("Direita");
		
	}else if(e.getKeyCode() ==  KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
		player.left = true;
		System.out.println("Esquerda");
	}
	
	if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
		player.up = true;
		System.out.println("Cima");
		
	}else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
		player.down = true;
		System.out.println("Baixo");
	}
	
}

@Override
public void keyReleased(KeyEvent e) {
	/*
	if(e.getKeyCode() == KeyEvent.VK_RIGHT ||
			e.getKeyCode() == KeyEvent.VK_D) {
		//player.right = false;
	}else if(e.getKeyCode() ==  KeyEvent.VK_LEFT ||
			e.getKeyCode() == KeyEvent.VK_A) {
		//player.left = false;
	}
	
	if(e.getKeyCode() == KeyEvent.VK_UP ||
			e.getKeyCode() == KeyEvent.VK_W) {
		//player.up = false;
	}else if(e.getKeyCode() == KeyEvent.VK_DOWN ||
			e.getKeyCode() == KeyEvent.VK_S) {
		//player.down = false;
	}
	*/
}


}

Procura essa linha:

Player player = new Player(0, 0, 57, 57, character.getSprite(0, 0, 57, 57));

E troca por essa:

player = new Player(0, 0, 57, 57, character.getSprite(0, 0, 57, 57));

Eu só removi o Player porque se vc deixar, vc vai estar usando uma variável local.

2 Curtidas

muito obrigado, ajudou muito

1 Curtida
//