只是想知道如何在libgdx中切换屏幕(屏幕实现屏幕)。我尝试将其设置为在电话上的screentap上更改屏幕,或者在电脑中按下空格时,但游戏根本没有任何响应。有什么想法吗?
游戏课程:
package com.kapres.bounce;
import com.badlogic.gdx.Game;
/**
* Created by KnutAndre on 12/20/2016.
*/
public class BounceGame extends Game {
@Override
public void create() {
showMenuScreen();
}
public void showMenuScreen(){
setScreen(new MenuScreen(this));
}
public void showGameScreen(){
setScreen(new GameScreen());
}
}
菜单屏幕:
package com.kapres.bounce;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
/**
* Created by KnutAndre on 12/26/2016.
*/
public class MenuScreen extends InputAdapter implements Screen {
BounceGame game;
ShapeRenderer renderer;
FitViewport menuViewport;
public MenuScreen(BounceGame game) {this.game = game;}
@Override
public void show() {
renderer = new ShapeRenderer();
menuViewport = new FitViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGTH);
renderer.setAutoShapeType(true);
}
@Override
public void render(float delta) {
menuViewport.apply(true);
Gdx.gl.glClearColor(Constants.BACKGROUND_COLOR.r, Constants.BACKGROUND_COLOR.g, Constants.BACKGROUND_COLOR.b, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setProjectionMatrix(menuViewport.getCamera().combined);
renderer.begin();
renderer.set(ShapeRenderer.ShapeType.Filled);
renderer.setColor(Color.GREEN);
renderer.circle(Constants.WORLD_WIDTH/2, Constants.WORLD_HEIGTH/2, Constants.START_BUTTON_RADIUS, 50);
renderer.end();
}
@Override
public void resize(int width, int height) {
menuViewport.update(width, height, true);
// menuViewport.getCamera().position.set(Constants.WORLD_WIDTH/2, Constants.WORLD_HEIGTH/2);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
renderer.dispose();
}
@Override
public void dispose() {
renderer.dispose();
}
@Override
public boolean keyDown(int keyCode) {
if (keyCode == Input.Keys.SPACE){
game.showGameScreen();
}
return true;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button){
game.showGameScreen();
return true;
}
}
游戏画面:
package com.kapres.bounce;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
/**
* Created by KnutAndre on 12/20/2016.
*/
public class GameScreen implements Screen{
ShapeRenderer renderer;
FitViewport gameViewport;
Ball ball;
@Override
public void show() {
renderer = new ShapeRenderer();
renderer.setAutoShapeType(true);
gameViewport = new FitViewport(Constants.WORLD_WIDTH, Constants.WORLD_HEIGTH);
ball = new Ball(gameViewport);
Gdx.input.setInputProcessor(ball);
}
@Override
public void render(float delta) {
gameViewport.apply(true);
Gdx.gl.glClearColor(Constants.BACKGROUND_COLOR.r, Constants.BACKGROUND_COLOR.g, Constants.BACKGROUND_COLOR.b, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setProjectionMatrix(gameViewport.getCamera().combined);
renderer.begin();
ball.render(renderer, delta);
renderer.end();
}
@Override
public void resize(int width, int height) {
gameViewport.update(width, height, true);
ball.init();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
renderer.dispose();
}
}
答案 0 :(得分:0)
不要忘记在super.render()
类的render()
方法中调用BounceGame
(它不在您的代码段中显示),例如:
@Override
public void render() {
super.render();
if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) {
Gdx.app.exit();
}
}
如果屏幕不为空,则呈现屏幕(例如之前调用了setScreen(new MainScreen())
方法)。来自超类render()
的{{1}}方法:
Game
答案 1 :(得分:0)
您似乎忘了设置输入处理器
Gdx.input.setInputProcessor(var);
您应该使用MenuScreen
show()
方法调用此方法。这样引擎就会知道你的输入处理器*。
代码如下所示:
public class MenuScreen extends InputAdapter implements Screen {
...
@Override
public void show() {
Gdx.input.setInputProcessor(this);
...
}
...
}
* InputAdapter
只是一个实现InputProcessor
接口的类。
如果您需要同时使用多个输入处理器,还有一个名为InputMultiplexer
的类。
<强>文档:强>