gpt4 book ai didi

Java - 不存储值的对象数组(范围?)

转载 作者:行者123 更新时间:2023-12-01 22:32:00 25 4
gpt4 key购买 nike

我正在尝试获取要渲染到游戏屏幕中的对象数组。我环顾四周,发现其他人提出了一些类似的问题,但我似乎无法将他们得到的答案应用到我的程序中。

问题似乎出现在嵌套的 for 循环中。在尝试解决此问题时,我在这些 for 循环中的第一行收到了 Java NullPointerExceptions。

package com.frfanizz.agility;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GameScreen implements Screen {

AgilityGame game;
OrthographicCamera camera;

SpriteBatch batch;
Hero hero;
Spark[] sparkArray;
int totalSparks;

public GameScreen(AgilityGame game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(true, 1920, 1080);

batch = new SpriteBatch();

hero = new Hero(60,540);

//Variables to set spark array
int screenX = 1400;
int screenY = 1200;
int numOfRow = 11;
int numOfCol = 8;
float spacingRow = screenY/(numOfRow + 1);
float spacingCol = screenX/(numOfCol + 1);
int totalSparks = numOfRow*numOfCol;
//Spark array
Spark[] sparkArray = new Spark[totalSparks];
//setting bounds for sparks
int index = 0;
for (int i=0;i<numOfCol;i++) {
for (int j=0; j<numOfRow;j++) {
//sparkArray[index] = new Spark();
sparkArray[index].bounds.x = (float) (60 + spacingCol + ((i)*spacingCol));
sparkArray[index].bounds.y = (float) (spacingRow + ((j-0.5)*spacingRow));
index++;
}
}

}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
camera.update();
generalUpdate();

batch.setProjectionMatrix(camera.combined);

batch.begin();

//Rendering code
batch.draw(Assets.spriteBackground, 0, 0);
batch.draw(hero.image,hero.bounds.x,hero.bounds.y);

for (int i=0; i<totalSparks; i++) {
batch.draw(sparkArray[i].image,sparkArray[i].bounds.x,sparkArray[i].bounds.y);
}

batch.end();

}

//Other gamescreen methods

}

Hero 和 Spark 类如下:

package com.frfanizz.agility;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;

public class Hero {

public Sprite image;
public Rectangle bounds;

public Hero(int spawnX, int spawnY) {
image = Assets.spriteHero;
image.flip(false, true);
bounds = new Rectangle(spawnX - 16, spawnY - 16, 32, 32);
}
}

和:

package com.frfanizz.agility;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;

public class Spark {

public Sprite image;
public Rectangle bounds;

public Spark () {
image = Assets.spriteSpark;
image.flip(false, true);
bounds = new Rectangle();
bounds.height = 32;
bounds.width = 32;
}
}

在for循环中,我打印了sparkArray的值,所以我认为我对值有问题,而不是我读过答案的其他问题的引用。(以下是我(未成功)引用的问题: Java. Array of objects , Java NullPointerException with objects in array )

提前致谢!

最佳答案

取消注释该行

//sparkArray[index] = new Spark();

您的 Spark 对象数组包含 null 值,直到您将 Spark 实例放入其中为止。

如果适合您正在做的事情,请考虑使用 Spark 的二维数组。

int numOfRow = 11;
int numOfCol = 8;
Spark[][] sparkArray = new Spark[numOfRow][numOfCol];
//setting bounds for sparks
for (int row=0; row<numOfRow; row++) {
for (int col=0; col<numOfCol; col++) {
sparkArray[row][col] = new Spark();
sparkArray[row][col].bounds.x = 1.23; // example of usage
}
}

关于Java - 不存储值的对象数组(范围?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558890/

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