1

嗨,我已经设置了 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

}

}

非常感谢任何帮助。

谢谢

丹尼尔

4

1 回答 1

1

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

于 2012-06-25T13:10:54.673 回答