嗨,我设置了2个矩形
矩形1和矩形2
我想在当前X +第一张图片的宽度位置之间生成一张图片。
但我似乎无法做到这一点,因为它会引发错误。
错误:
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。