gpt4 book ai didi

java - 创建多个矩形 java libgdx

转载 作者:行者123 更新时间:2023-12-02 13:14:29 26 4
gpt4 key购买 nike

您好,我是 libgdx 和 java 的新手,在屏幕上创建多个对象时遇到问题。当我单击 SPACE 时,我想在屏幕矩形上从 Pocisk 类中创建一个新对象,但当我单击它时,它就会下降。

游戏类

public class Gra extends ApplicationAdapter {
SpriteBatch batch;
private Texture Statektekstura, Pocisktekstura;
private Statek statek;
private Pocisk pocisk;

@Override
public void create() {
zaladujdane();
init();
}

private void init() {
batch = new SpriteBatch();
statek = new Statek(Statektekstura);
pocisk = new Pocisk(Pocisktekstura);
}

private void zaladujdane() {
//tekstura statku pomniejszana
Pixmap pixmap2 = new Pixmap(Gdx.files.internal("statek.jpg"));
Pixmap pixmap1 = new Pixmap(100, 100, pixmap2.getFormat());
pixmap1.drawPixmap(pixmap2,
0, 0, pixmap2.getWidth(), pixmap2.getHeight(),
0, 0, pixmap1.getWidth(), pixmap1.getHeight()
);
Statektekstura = new Texture(pixmap1);

//tekstura pocisk pomniejszona
Pixmap pixmap20 = new Pixmap(Gdx.files.internal("pocisk.jpg"));
Pixmap pixmap10 = new Pixmap(35, 35, pixmap20.getFormat());
pixmap10.drawPixmap(pixmap20,
0, 0, pixmap20.getWidth(), pixmap20.getHeight(),
0, 0, pixmap10.getWidth(), pixmap10.getHeight()
);

Pocisktekstura = new Texture(pixmap10);
}

@Override
public void render() {
udpdate();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
statek.draw(batch);
pocisk.draw(batch);

batch.end();
}

private void udpdate() {
przesuwanie();
pocisk.y = pocisk.y+pocisk.pociskpredkosc*Gdx.graphics.getDeltaTime();
}

private void przesuwanie() {
if (Gdx.input.isKeyPressed(Keys.A)) {
statek.x = statek.x - 250 * Gdx.graphics.getDeltaTime();
}
if (Gdx.input.isKeyPressed(Keys.D)) {
statek.x = statek.x + 250 * Gdx.graphics.getDeltaTime();
}
if (Gdx.input.isKeyPressed(Keys.SPACE)) {

pocisk = new Pocisk(Pocisktekstura);
pocisk.strzal();
}
}

@Override
public void dispose() {
//batch.dispose();
}
}

类 Pocisk 代码:

public class Pocisk extends Rectangle {

private Texture texture;
public float pociskpredkosc = 0;

Pocisk(Texture texture) {
this.texture = texture;
this.height = texture.getHeight();
this.width = texture.getWidth();
}

public void draw(SpriteBatch batch) {
batch.draw(texture, x, y);
}

public void strzal() {
pociskpredkosc = pociskpredkosc + 80;
}
}

我认为我在 gra 类中失败,而不是在 Pocisk 类中失败,当我单击空格时,我应该更改什么才能始终创建一个新的矩形?

最佳答案

每次创建新对象并引用 pocisk 变量时,您都在处理单个 pocisk 引用。由该变量执行的 Pocisk 更新,以便每次 pocisk 从初始位置开始。对于多个 pocisk,您需要保留所有对象引用并对该引用执行更新,因此使用任何容器来存储所有引用。您可以使用 Array 来实现相同的目的。

public class MainGame extends ApplicationAdapter {
SpriteBatch batch;
private Texture Statektekstura,Pocisktekstura;
private Statek statek;
Array<Pocisk> pociskList;
boolean oneAtOnce;

@Override
public void create () {
zaladujdane();
init();
}

private void init() {
batch=new SpriteBatch();
statek=new Statek(Statektekstura);
pociskList=new Array<Pocisk>();
}

private void zaladujdane() {
//tekstura statku pomniejszana
Pixmap pixmap2 = new Pixmap(Gdx.files.internal("badlogic.jpg"));
Pixmap pixmap1 = new Pixmap(100, 100, pixmap2.getFormat());
pixmap1.drawPixmap(pixmap2,
0, 0, pixmap2.getWidth(), pixmap2.getHeight(),
0, 0, pixmap1.getWidth(), pixmap1.getHeight()
);
Statektekstura=new Texture(pixmap1);

//tekstura pocisk pomniejszona
Pixmap pixmap20 = new Pixmap(Gdx.files.internal("badlogic.jpg"));
Pixmap pixmap10 = new Pixmap(35, 35, pixmap20.getFormat());
pixmap10.drawPixmap(pixmap20,
0, 0, pixmap20.getWidth(), pixmap20.getHeight(),
0, 0, pixmap10.getWidth(), pixmap10.getHeight()
);

Pocisktekstura=new Texture(pixmap10);
}

@Override
public void render () {
udpdate();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
statek.draw(batch);
for(Pocisk pocisk:pociskList)
pocisk.draw(batch);

batch.end();
}

private void udpdate() {
przesuwanie();
for (Pocisk pocisk:pociskList)
pocisk.y=pocisk.y+pocisk.pociskpredkosc*Gdx.graphics.getDeltaTime();
}

private void przesuwanie() {
if(Gdx.input.isKeyPressed(Input.Keys.A))
{
statek.x=statek.x-250*Gdx.graphics.getDeltaTime();
}
if(Gdx.input.isKeyPressed(Keys.D))
{
statek.x=statek.x+250*Gdx.graphics.getDeltaTime();
}
if(Gdx.input.isKeyPressed(Input.Keys.SPACE))
{
Pocisk pocisk=new Pocisk(Pocisktekstura); //Called in game render method so create number of Object
pocisk.strzal();
pociskList.add(pocisk);

}
}

@Override
public void dispose () {
//batch.dispose();
}
}

我会提醒您,您正在游戏 render 方法中创建 Pocisk 对象,因此当您按下 Keys.SPACE 时它会执行多次> 创建的对象数量。最好在InputProcessor接口(interface)的keyDown方法中创建对象。

关于java - 创建多个矩形 java libgdx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43820848/

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