java render()_java-使用gdx render()方法的libgdx出现NullPointer...

开始制作游戏.

这是我的一些代码.

package games.tribe.screens;

import games.tribe.model.World;

import games.tribe.view.WorldRenderer;

import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.Screen;

import com.badlogic.gdx.graphics.GL10;

public class GameScreen implements Screen {

private World world;

private WorldRenderer renderer;

/** This was the bit I'd missed --------------------------------------**/

@Override

public void show() {

world = new World();

renderer = new WorldRenderer(world);

}

/**------------------------------------------------------------------**/

@Override

public void render(float delta) {

Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

renderer.render();

}

@Override

public void resize(int width, int height) {

// 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

}

}

这是WorldRenderer类:

package games.tribe.view;

import com.badlogic.gdx.graphics.Color;

import com.badlogic.gdx.graphics.OrthographicCamera;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

import com.badlogic.gdx.math.Rectangle;

import games.tribe.model.Block;

import games.tribe.model.TribalHunter;

import games.tribe.model.World;

public class WorldRenderer {

private World world;

private OrthographicCamera cam;

/**for debug rendering**/

ShapeRenderer debugRenderer = new ShapeRenderer();

public WorldRenderer(World world) {

this.world = world;

this.cam = new OrthographicCamera(10, 7);

this.cam.position.set(5, 3.5f, 0);

this.cam.update();

}

public void render() {

//render blocks

debugRenderer.setProjectionMatrix(cam.combined);

debugRenderer.begin(ShapeType.Rectangle);

for(Block block : world.getBlocks()) {

Rectangle rect = block.getBounds();

float x1 = block.getPosition().x + rect.x;

float y1 = block.getPosition().y + rect.y;

debugRenderer.setColor(new Color(1, 0, 0, 1));

debugRenderer.rect(x1, y1, rect.width, rect.height);

}

//render hunter

TribalHunter hunter = world.getHunter();

Rectangle rect = hunter.getBounds();

float x1 = hunter.getPosition().x + rect.x;

float y1 = hunter.getPosition().y + rect.y;

debugRenderer.setColor(new Color(0, 1, 0, 1));

debugRenderer.rect(x1, y1, rect.width, rect.height);

debugRenderer.end();

}

}

当我将其作为桌面应用程序运行时,这是一个例外:

Exception in thread "LWJGL Application" java.lang.NullPointerException

at games.tribe.screens.GameScreen.render(GameScreen.java:19)

at com.badlogic.gdx.Game.render(Game.java:46)

at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)

at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)

AL lib: ReleaseALC: 1 device not closed

gdx.Game.render的第46行是这种方法:

@Override

public void render () {

if (screen != null) screen.render(Gdx.graphics.getDeltaTime());

}

任何帮助将不胜感激

提前致谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值