在本文中,您将会了解到关于在屏幕之间切换Libgdx的新资讯,同时我们还将为您解释怎么在屏幕之间切换的相关在本文中,我们将带你探索在屏幕之间切换Libgdx的奥秘,分析怎么在屏幕之间切换的特点,并给出
在本文中,您将会了解到关于在屏幕之间切换Libgdx的新资讯,同时我们还将为您解释怎么在屏幕之间切换的相关在本文中,我们将带你探索在屏幕之间切换Libgdx的奥秘,分析怎么在屏幕之间切换的特点,并给出一些关于1、Libgdx简介、4、libgdx 应用框架、android – LibGDX gdx-freetype BlueStacks、android – LIBGDX输入 – 触摸屏幕的手指数的实用技巧。
本文目录一览:- 在屏幕之间切换Libgdx(怎么在屏幕之间切换)
- 1、Libgdx简介
- 4、libgdx 应用框架
- android – LibGDX gdx-freetype BlueStacks
- android – LIBGDX输入 – 触摸屏幕的手指数
在屏幕之间切换Libgdx(怎么在屏幕之间切换)
大家好,我仍在从事这个libgdx项目的工作,我试图找出将屏幕切换到游戏屏幕的最佳方法。现在,当单击一个按钮时,我需要它来切换到游戏屏幕。我已经看到了一些扩展游戏类的实现方式,但是我不确定这里最好的方法是什么。如果您看到一些可以改进的代码,请告诉我。
这是主要的应用程序类:
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 }}
答案1
小编典典这就是我始终执行屏幕切换的方式:
首先,主类需要扩展Game(From com.badlogic.gdx.Game
),然后您需要具有一个新的type字段Game
:
public class ConnectFourApplication extends Game{ private Game game;
现在game
在构造函数中初始化:
public ConnectFourApplication(){ game = this; // Since this class extends Game
要将screen设置为MainScreen
,现在,您所需要做的就是使用setScreen(newMainScreen(game));
method(通过传递,game
以便我们可以从MainScreen
类中设置屏幕)。现在,您需要一个新的MainScreen
class
构造函数和一个新字段:
private Game game;public MainScreen(Game game){ this.game = game;
现在,您可以game.setScreen(new Screen(game));
用来将屏幕设置为另一个实现的类Screen
。
但是现在,在主类中,render()
必须使用该方法super.render();
来利用其他屏幕渲染中的所有内容!
public void render() { clearWhite(); super.render();}
PS:请确保您要在屏幕上实际上课implements Screen
。
1、Libgdx简介
(原文:http://www.libgdx.cn/topic/11/1-libgdx%E7%AE%80%E4%BB%8B)
Libgdx 是一个跨平台和可视化的的开发框架。它当前支持Windows,Linux,Mac OS X,Android,IOS和HTML5作为目标平台。
Libgdx允许你一次编写代码不经修改部署到多个平台。你会可以从能够快速迭代的桌面环境中编码而获益,而不是将你的最新的修改部署到你的设备,或者编译成HTML5。你可以使用所有java生态系统的工具来让你的产品变得更好。
Libgdx 可以让你深入底层,给予你直接访问文件系统,输入设备,声音设备和通过一个统一的OpenGL ES 2.0和3.0接口使用OpenGL的权限。
在这些底层工具之上我们构建了强大API帮助你完成常见的游戏开发任务。像渲染精灵和文本,构建用户接口,播放音效和音乐,线性代数和三角计算,解析JSON和XML等等。
如果需要的话,libgdx可以跨国java的束缚使用本地代码更好的进行效果展现。所有这些功能都隐藏在Java API中,所以你无需考虑将本地代码(native code)交叉编译到所有平台。所有的这些都有Libgdx来处理。
Libgdx的目标是成为一个游戏开发框架而不是游戏引擎,是因为我们意识到在实际开发中没有一劳永逸的事情。因此,我们将强大的功能进行抽象,开发者可以自我选择采用什么样的功能。
(www.libgdx.cn版权所有,如需转载,注明出处)
4、libgdx 应用框架
(原文:http://www.libgdx.cn/topic/29/4-libgdx%E5%BA%94%E7%94%A8%E6%A1%86%E6%9E%B6)
模块
作为核心,libgdx 提供了六个接口与操作系统进行交互,针对每个操作系统的 backend 实现这些接口。
Application:运行应用和通知一个 API 客户端应用程序层面的事件,比如窗口更改大小。提供日志系统和查询方法,比如内存使用。
Files:暴露系统底层的文件系统。提供了一系列的针对文件操作的抽象。
Input:用户输入。如鼠标,键盘,触摸和传感器。同时支持轮询和事件驱动。
Net:提供了通过 HTTP/HTTPS 跨平台访问资源的方法,可以创建 TCP 服务器和客户端 sockets。
Audio:提供了回放音效和音乐的方法,同时提供了直接访问设备播放 PCM。
Graphics:暴露了 OpenGL ES 2.0。
启动类
对于每个平台来说,必须有的就是启动类。每个平台的启动类实现了每个平台的 backend 的 Application 接口。对桌面应用来说,使用 Lwjgl backend,如下:
public class DesktopStarter {
public static void main(String[] argv) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
new LwjglApplication(new MyGame(), config);
}
}
对 Android 来说,启动类如下:
public class AndroidStarter extends AndroidApplication {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGame(), config);
}
}
访问模块
模块可以通过 Gdx class 提供的静态方法进行访问。
比如,audio 模块可以通过以下方式进行访问:
AudioDevice audioDevice = Gdx.audio.newAudioDevice(44100, false);
(www.libgdx.cn 版权所有,如需转载,注明出处)
android – LibGDX gdx-freetype BlueStacks
但仍然:
11-08 21:14:10.280: E/AndroidRuntime(1378): FATAL EXCEPTION: GLThread 9 11-08 21:14:10.280: E/AndroidRuntime(1378): com.badlogic.gdx.utils.GdxRuntimeException: Couldn''t load shared library ''gdx-freetype'' for target: Linux,32-bit 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:110) 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.badlogic.gdx.graphics.g2d.freetype.FreeType.initFreeType(FreeType.java:541) 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.<init>(FreeTypeFontGenerator.java:64) 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.axl.where.test.create(test.java:153) 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:334) 11-08 21:14:10.280: E/AndroidRuntime(1378): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1356) 11-08 21:14:10.280: E/AndroidRuntime(1378): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1118) 11-08 21:14:10.280: E/AndroidRuntime(1378): Caused by: java.lang.UnsatisfiedLinkError: Couldn''t load gdx-freetype: findLibrary returned null 11-08 21:14:10.280: E/AndroidRuntime(1378): at java.lang.Runtime.loadLibrary(Runtime.java:429) 11-08 21:14:10.280: E/AndroidRuntime(1378): at java.lang.System.loadLibrary(System.java:554) 11-08 21:14:10.280: E/AndroidRuntime(1378): at com.badlogic.gdx.utils.SharedLibraryLoader.load(SharedLibraryLoader.java:106) 11-08 21:14:10.280: E/AndroidRuntime(1378): ... 6 more
有任何想法吗 ? LibGDX 0.9.9
解决方法
http://libgdx.badlogicgames.com/nightlies/dist/extensions/gdx-freetype/x86/
另外,你正在使用v0.9.6文件o.O使用nightlies:
http://libgdx.badlogicgames.com/nightlies/dist/extensions/gdx-freetype/
android – LIBGDX输入 – 触摸屏幕的手指数
谢谢
解决方法
InputProcessor
进行基于事件的输入处理,只需在touchDown处递增计数器并在touchUp处递减计数器.
如果您使用Gdx.input
进行基于轮询的输入处理,请使用isTouched(int)
调用来测试指针N是否已关闭. libGDX实现tracks at most 20 pointers.我不认为任何硬件支持那么多(并且你的游戏也可能有下限).但是,您必须检查所有指针ID,因为指针ID N在指针ID N离开后仍可保持活动状态.就像是:
int activetouch = 0; for (int i = 0; i < 20; i++) { if (Gdx.input.isTouched(i)) activetouch++; }
关于在屏幕之间切换Libgdx和怎么在屏幕之间切换的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于1、Libgdx简介、4、libgdx 应用框架、android – LibGDX gdx-freetype BlueStacks、android – LIBGDX输入 – 触摸屏幕的手指数的相关知识,请在本站寻找。
本文标签: