gpt4 book ai didi

java - 无法让我的代码在另一个图像的尺寸内生成图像。使用 LibGDX

转载 作者:太空宇宙 更新时间:2023-11-04 08:11:18 25 4
gpt4 key购买 nike

嗨,我设置了 2 个矩形矩形1和矩形2

我想在当前 X + 第一个图像的宽度位置之间生成一个图像。

但我似乎无法理解它的值(value),因为它会引发错误。错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.inspirednotion.thing.Thing.<init>(Thing.java:65)
at com.inspirednotion.thing.MainThing.create(MainThing.java:18)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:144)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
AL lib: ReleaseALC: 1 device not closed

这是我正在使用的代码:

package com.inspirednotion.thing;

import java.util.Iterator;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class Thing implements Screen {

MainThing g;
public OrthographicCamera cam;
public SpriteBatch batch;
public Texture t;
public TextureRegion tR;

public Texture tt;
public TextureRegion ttR;

public Texture cloud;
public TextureRegion cloudR;

Array<Rectangle> raindrops;
long lastDropTime;

Rectangle cloudRec;

Texture rainDrop;
TextureRegion rainDropR;

Rectangle water;

Random rand;

float moveY;

public Thing(MainThing g){
this.g=g;
cam = new OrthographicCamera(480,800);
cam.position.set(480/2, 800/2, 0);
batch = new SpriteBatch();
t = new Texture(Gdx.files.internal("bg.png"));
tR = new TextureRegion(t,0,0,480,700);

tt = new Texture(Gdx.files.internal("bgCloud.png"));
ttR = new TextureRegion(tt,0,0,480,350);

cloud = new Texture(Gdx.files.internal("cloud.png"));
cloudR = new TextureRegion(cloud,0,0,256,140);

rainDrop = new Texture(Gdx.files.internal("RainDrop.png"));

rand = new Random();

moveY = 0;


raindrops = new Array<Rectangle>();
spawnRaindrop();

cloudRec = new Rectangle();
cloudRec.x = cam.viewportWidth /2 - 256 / 2;
cloudRec.y = 600;
cloudRec.height = 140;
cloudRec.width = 256;

water = new Rectangle();
water.x = 0;
water.y = -700;
water.width = 480;
water.height = 700;


}


private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(cloudRec.x, cloudRec.x + cloudRec.width);
raindrop.y = 510;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}






@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, .05f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(ttR,0,450,480,350);
batch.end();

batch.begin();
if(moveY < cam.viewportHeight -200){
water.y += 10 * Gdx.graphics.getDeltaTime();
}else
{
moveY = 0;
}
batch.draw(tR, water.x, moveY + water.y , water.width, water.height);
/*batch.draw(tR, 0, moveY -700,cam.viewportWidth, cam.viewportHeight);*/
batch.end();

batch.begin();
batch.draw(cloudR, cloudRec.x, cloudRec.y, cloudRec.width, cloudRec.height);
batch.end();

batch.begin();
for(Rectangle raindrop: raindrops) {
batch.draw(rainDrop, raindrop.x , raindrop.y);
}
batch.end();

if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();

Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
//this effects the speed of downward movement
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y < water.y+680) iter.remove();
// if(raindrop.overlaps(water)) {
// dropSound.play();
// iter.remove();
}}








@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub

}

@Override
public void show() {
// TODO Auto-generated method stub

}

@Override
public void hide() {
// TODO Auto-generated method stub

}

@Override
public void pause() {
// TODO Auto-generated method stub

}

@Override
public void resume() {
// TODO Auto-generated method stub

}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

}

非常感谢任何帮助。

谢谢

丹尼尔

最佳答案

您需要在调用spawnRaindrop()之前实例化您的cloudRec。这就是 NullPointerException 的原因,因为您尝试在 spawnRaindrop() 方法中使用 cloudRec..

关于java - 无法让我的代码在另一个图像的尺寸内生成图像。使用 LibGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188647/

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