gpt4 book ai didi

java - 勾选和渲染不适用于其他类,我该怎么办

转载 作者:行者123 更新时间:2023-12-01 09:10:33 25 4
gpt4 key购买 nike

当我尝试 tick()render() 时,我的游戏类中的其他类不起作用。

Exception in thread "Thread-2" java.lang.NullPointerException 
at ca.patrick.main.window.Game.render(Game.java:87)
at ca.patrick.main.window.Game.run(Game.java:52)
at java.lang.Thread.run(Unknown Source)

有时:

Exception in thread "Thread-2" java.lang.NullPointerException 
at ca.patrick.main.window.Game.tick(Game.java:71)
at ca.patrick.main.window.Game.run(Game.java:54)
at java.lang.Thread.run(Unknown Source)

这是我的游戏代码(我用“name”替换了我的名字)和正如你所看到的,我是 Java 的初学者,所以我的代码可能不符合标准,但这是我的代码:

package ca.name.main.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.LinkedList;

import ca.name.main.framework.GameObject;
import ca.name.main.framework.Id;
import ca.name.main.objects.Enemy1;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = -4319386659730073928L;

public int WIDTH, HEIGHT;
public boolean running = false;
private Thread thread;

private LinkedList<GameObject> object;
private Enemy1 enemy1;

public synchronized void start(){
if(running)
return;

running = true;
thread = new Thread(this);
thread.start();
}

游戏循环:

    public void run() {
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;

if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}

private void init() {
HEIGHT = getHeight();
WIDTH = getWidth();

Enemy1 enemy1 = new Enemy1(0, 0, Id.Enemy);
}

打勾:

    private void tick() {
enemy1.tick(object);
}

渲染:

    private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();

g.setColor(Color.cyan);
g.fillRect(0, 0, getHeight() * 2, getWidth() * 2);

enemy1.render(g);

bs.show();
g.dispose();
}

public Game(){

}

public static void main(String args[]){
new Window(600, 800, "Log Platformer prototype", new Game());
}
}

最佳答案

我刚刚发现这是我的代码:)

package ca.patrick.main.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.image.BufferStrategy;
import java.util.LinkedList;

import ca.patrick.main.framework.GameObject;
import ca.patrick.main.framework.Id;
import ca.patrick.main.objects.Enemy1;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = -4319386659730073928L;

public int WIDTH, HEIGHT;
public boolean running = false;
private Thread thread;

private LinkedList<GameObject> object;
private Enemy1 e;

private void init() {
HEIGHT = getHeight();
WIDTH = getWidth();

e = new Enemy1(200, 200, Id.Enemy);
}
public synchronized void start(){
if(running)
return;

running = true;
thread = new Thread(this);
thread.start();
}

public void run() {
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;

if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
}



private void tick() {
e.tick(object);
}

private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();

g.setColor(Color.cyan);
g.fillRect(0, 0, getHeight() * 2, getWidth() * 2);

e.render(g);

bs.show();
g.dispose();
}

public Game(){

}

public static void main(String args[]){
new Window(600, 800, "Log Platformer prototype", new Game());
}

}

关于java - 勾选和渲染不适用于其他类,我该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40943617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com