大家好,我仍然在做这个libgdx项目,我正在努力找出最好的方法来改变我的游戏屏幕现在,当一个按钮被点击时,我需要它转换到游戏屏幕。我见过一些扩展game类的实现,但我不确定从这里开始最好的方法是什么。如果你看到一些可以改进的代码,请让我知道。
以下是主要的应用程序类:
public class ConnectFourApplication implements ApplicationListener {
private Screen screen;
public static void main(String[] args) {
new LwjglApplication(new ConnectFourApplication(), "PennyPop", 1280, 720,
true);
}
@Override
public void create() {
screen = new MainScreen();
screen.show();
}
@Override
public void dispose() {
screen.hide();
screen.dispose();
}
/** Clears the screen with a white color */
private void clearWhite() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
@Override
public void pause() {
screen.pause();
}
@Override
public void render() {
clearWhite();
screen.render(Gdx.graphics.getDeltaTime());
}
@Override
public void resize(int width, int height) {
screen.resize(width, height);
}
@Override
public void resume() {
screen.resume();
}
}
public class MainScreen implements Screen {
private final Stage stage;
private final SpriteBatch spriteBatch;
//Parameter for drawing the buttons
private final BitmapFont font;
private final TextureAtlas buttons;
private final Button SFXButton;
private final Button APIButton;
private final Button GameButton;
private final Skin images;
//Parameter for Sound
private final com.badlogic.gdx.audio.Sound SFXClick;
//Parameter for the api call
private final String WeatherUrl;
private final HttpRequest request;
private final City SanFrancisco;
//The screen to load after the game button is hit
private Screen gamescreen;
public MainScreen() {
//Set up our assets
spriteBatch = new SpriteBatch();
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false, spriteBatch);
font = new BitmapFont(Gdx.files.internal("assets/font.fnt"),
Gdx.files.internal("assets/font.png"), false);
buttons = new TextureAtlas("assets/GameButtons.pack");
images = new Skin(buttons);
images.addRegions(buttons);
SFXButton = new Button(images.getDrawable("sfxButton"));
SFXButton.setPosition(295, 310);
APIButton = new Button(images.getDrawable("apiButton"));
APIButton.setPosition(405, 310);
GameButton = new Button(images.getDrawable("gameButton"));
GameButton.setPosition(515, 310);
SFXClick = Gdx.audio.newSound(Gdx.files.internal("assets/button_click.wav"));
//Add our actors to the stage
stage.addActor(SFXButton);
stage.addActor(APIButton);
stage.addActor(GameButton);
//Set up our Url request to be used when clicking the button
WeatherUrl = "http://api.openweathermap.org/data/2.5/weather?q=San%20Francisco,US";
request = new HttpRequest(HttpMethods.GET);
request.setUrl(WeatherUrl);
SanFrancisco = new City("Unavailable","Unavailable","0","0"); //init san fran to be displayed before they have clicked the button
//initialize the game screen that we will switch to when the button is pressed
gamescreen = new GameScreen();
}
@Override
public void dispose() {
spriteBatch.dispose();
stage.dispose();
}
@Override
public void render(float delta) {
stage.act(delta);
stage.draw();
//Begin sprite batch
spriteBatch.begin();
//Set our on click listeners for our buttons
if (SFXButton.isPressed())
SFXClick.play();
if(APIButton.isPressed())
{
CallApi();
}
if(GameButton.isPressed())
LoadGame();
//Set font color and render the screen
font.setColor(Color.RED);
font.draw(spriteBatch, "PennyPop", 455 - font.getBounds("PennpyPop").width/2,
460 + font.getBounds("PennyPop").height/2);
font.setColor(Color.BLACK);
font.draw(spriteBatch, "Current Weather", 900 - font.getBounds("PennpyPop").width/2,
460 + font.getBounds("PennyPop").height/2);
font.setColor(Color.LIGHT_GRAY);
font.draw(spriteBatch, SanFrancisco.Name, 940 - font.getBounds("PennpyPop").width/2,
420 + font.getBounds("PennyPop").height/2);
font.setColor(Color.RED);
font.draw(spriteBatch, SanFrancisco.CurrentCondition, 950 - font.getBounds("PennpyPop").width/2,
300 + font.getBounds("PennyPop").height/2);
font.draw(spriteBatch, SanFrancisco.Temperature + " Degrees,", 920 - font.getBounds("PennpyPop").width/2,
270 + font.getBounds("PennyPop").height/2);
font.draw(spriteBatch, SanFrancisco.WindSpeed, 1200 - font.getBounds("PennpyPop").width/2,
270 + font.getBounds("PennyPop").height/2);
//End or sprite batch
spriteBatch.end();
}
//Handles calling our API
public void CallApi(){
//Sends our stored HTTPRequest object
Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
@Override
public void handleHttpResponse(HttpResponse httpResponse) {
//Uses our private response reader object to give us a the JSON from the api call
JSONObject json = HttpResponseReader(httpResponse);
//Gets the name of the city
SanFrancisco.Name = (String) json.get("name");
//Parsed through our returned JSON for the weather key
JSONArray WeatherArray = (JSONArray) json.get("weather");
//Gets the actual weather dictionary
JSONObject Weather = (JSONObject) WeatherArray.get(0);
//Finally get the value with the key of description and assign it
//To the San Fran current conditions field
SanFrancisco.CurrentCondition = (String) Weather.get("description");
//Gets the actual main dictionary
JSONObject main = (JSONObject) json.get("main");
//Finally get the values based on the keys
SanFrancisco.Temperature = (String) Double.toString((double) main.get("temp"));
//Finally get the wind speed
JSONObject wind = (JSONObject) json.get("wind");
SanFrancisco.WindSpeed = (String) Double.toString((double) wind.get("speed"));
}
@Override
public void failed(Throwable t) {
Gdx.app.log("Failed ", t.getMessage());
}
});
}
//When the button game button is clicked should load the connect four game
public void LoadGame(){
hide();
gamescreen.show();
}
//Converts our HttpResponse into a JSON OBject
private static JSONObject HttpResponseReader(HttpResponse httpResponse){
BufferedReader read = new BufferedReader(new InputStreamReader(httpResponse.getResultAsStream()));
StringBuffer result = new StringBuffer();
String line = "";
try {
while ((line = read.readLine()) != null) {
result.append(line);
}
JSONObject json;
try {
json = (JSONObject)new JSONParser().parse(result.toString());
return json;
} catch (ParseException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void resize(int width, int height) {
stage.setViewport(width, height, false);
}
@Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
render(0);
}
@Override
public void pause() {
// Irrelevant on desktop, ignore this
}
@Override
public void resume() {
// Irrelevant on desktop, ignore this
}
}
在LibGDX上有一个例子来说明这一点和许多其他概念。信息
这就是我总是如何实现屏幕切换:
首先,主类需要扩展Game(从com.badlogic.gdx.Game
),您需要有一个类型为Game
的新字段:
public class ConnectFourApplication extends Game{
private Game game;
现在在构造函数中初始化游戏
:
public ConnectFourApplication(){
game = this; // Since this class extends Game
要将屏幕设置为MainScreen
,现在只需使用setScreen(新的主屏幕(游戏))
method(传递game
以便我们可以从MainScreen
class设置屏幕)您现在需要MainScreen
类的新构造函数和新字段:
private Game game;
public MainScreen(Game game){
this.game = game;
现在你可以使用游戏了。设置屏幕(新屏幕(游戏))
将屏幕设置为另一个实现screen
的类。
但是现在,在main类中,在render()
方法中,必须使用super。render()
使用来自其他屏幕的所有内容渲染!
public void render() {
clearWhite();
super.render();
}
PS:确保你正在制作的类实际上是一个屏幕,
实现了屏幕
。
问题内容: 大家好,我仍在从事这个libgdx项目的工作,我试图找出将屏幕切换到游戏屏幕的最佳方法。现在,当单击一个按钮时,我需要它来切换到游戏屏幕。我已经看到了一些扩展游戏类的实现方式,但是我不确定这里最好的方法是什么。如果您看到一些可以改进的代码,请告诉我。 这是主要的应用程序类: 问题答案: 这就是我始终执行屏幕切换的方式: 首先,主类需要扩展Game(From ),然后您需要具有一个新的t
在libGDX中切换屏幕似乎有问题。它会切换到游戏屏幕,但不会切换回主屏幕,也不会切换到屏幕上的游戏。我的游戏课: 我的GameScreen类(实现屏幕): 这就是我如何更改屏幕(不工作): 你可以在这里找到全部来源。
我试图改变Libgdx屏幕之间的动画。我想写我的自定义动画(淡入淡出等)。有人能给我个线索吗?我似乎在Libgdx代码中找不到转换的实现。
嗨,我在libgdx切换屏幕时遇到了问题。我正在建造一个小行星游戏克隆。因此,首先呈现我的Main MenuScreen类(使用Fitviewport),然后我调用setScreen()到GameScreen(GameScreen不使用Fitviewport),除了第二个屏幕呈现为使用Fitviewport。如果我调整第二个屏幕的大小,那么整个窗口用于渲染。为什么会发生这种情况?这里有一些图片..
当我的游戏结束时,我试图改变屏幕到游戏结束屏幕,但它没有这样做,只是闪烁当前的游戏屏幕。 游戏结束画面
多亏了我在这个论坛上解决其他问题的帮助,我成功地推进了我的项目,但另一个障碍出现在我的道路上。 我有麻烦在libgdx java实现多个屏幕。我想知道如何实现多个屏幕(一个用于主菜单,一个用于播放,一个用于加载屏幕,...)。 关于我应该如何构造我的屏幕类的一个例子或一些解释将非常有帮助。我试着实现我自己的屏幕管理器,但不太顺利...还有一些关于我应该如何处理屏幕的提示,因为每次从主菜单到播放或选