我正在使用libgdx在Java上进行游戏,现在缩放存在问题。这是我的代码:
1:
public class TileMap implements Screen {
private TiledMap tiledMap;
private OrthographicCamera camera;
private OrthogonalTiledMapRenderer mapRenderer;
private TiledMapStage tiledMapStage;
public TileMap(){
tiledMapStage = new TiledMapStage(camera,this);
camera = new OrthographicCamera();
camera.setToOrtho(false);
camera.translate(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
camera.update();
tiledMap = new TmxMapLoader().load("gameMap.tmx");
mapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
}
@Override
public void render (float delta) {
float x = Gdx.input.getDeltaX();
float y = Gdx.input.getDeltaY();
Gdx.gl.glClearColor(135/255f, 206/255f, 235/255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.position.add(-x, y, 0);
camera.update();
mapRenderer.render();
mapRenderer.setView(camera);
tiledMapStage.render();
}
2:
public class TiledMapStage {
private TileMap tileMap;
private Stage stage;
private OrthographicCamera Camera;
TiledMapStage(OrthographicCamera camera, TileMap tileMap){
this.tileMap = tileMap;
this.Camera = camera;
ScreenViewport viewport = new ScreenViewport();
stage = new Stage(viewport);
Gdx.input.setInputProcessor(stage);
MapActor zoomIn = new MapActor(new Texture("zoomIn.png"),"zoomIn", Camera, this);
stage.addActor(zoomIn);
}
public void render(){
stage.draw();
}
3:
public class MapActor extends Actor {
private OrthographicCamera camera;
Sprite sprite;
TiledMapStage tiledMapStage;
public MapActor(Texture texture, final String actorName, final OrthographicCamera camera, final TiledMapStage tiledMapStage){
this.camera = camera;
this.tiledMapStage = tiledMapStage;
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
sprite = new Sprite(texture);
setTouchable(Touchable.enabled);
if(actorName.equals("zoomIn")) {
sprite.setSize(width/10f,height/7f);
sprite.setPosition(width-width/10f-sprite.getWidth()/2,height/7f-sprite.getHeight()/2);
addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log("2222222222222222","tochdow");
camera.zoom = (float) (camera.zoom + 0.02);
return true;
}
});
}
spritePos(sprite.getX(), sprite.getY());
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
}
@Override
public void act(float delta) {
super.act(delta);
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.setColor(1, 1, 1, parentAlpha);
sprite.draw(batch);
}
public void spritePos(float x, float y){
sprite.setPosition(x, y);
setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
}
}
问题是
E / AndroidRuntime:严重例外:GLThread 7412 流程:com.mygdx.game,PID:8137 java.lang.RuntimeException:Actor:MapActor 在com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:202) 在com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:152) 在com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:281) 在com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:405) 在com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:492) 在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1571) 在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1270) 原因:java.lang.NullPointerException:尝试从字段“ float com.badlogic.gdx.graphics.OrthographicCamera.zoom”中读取 空对象引用 在com.mygdx.game.Actor.MapActor $ 1.touchDown(MapActor.java:33) 在com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:57) 在com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:188) 在com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:152) 在com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:281) 在com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:405) 在com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:492) 在android.opengl.GLSurfaceView $ GLThread.guardedRun(GLSurfaceView.java:1571) 在android.opengl.GLSurfaceView $ GLThread.run(GLSurfaceView.java:1270)