Android game development for 10 days (2)-build the libgdx Game Engine

Source: Internet
Author: User
Tags libgdx
Libgdx Overview

Libgdx is a cross-platform 2D/3D Game Development Framework written in Java/C ++. It is based on the Apache license and Version 2.0 protocol and is free for commercial and non-commercial use.

Libgdx is compatible with windows, Linux, Max OS X, Java Applet, JavaScript/webgl, and Android [4] (version 1.5 +) platforms. The support for the Android platform is growing in the android camp and related markets, with more and more libgdx users. In addition, monotouch enables IOS compatibility.

Http://libgdx.badlogicgames.com/

Build a Development Environment

Operating System: opensuse12.2

Make sure that the android development environment is configured. JDK 1.6 is recommended. The official website says that 1.7 has a bug.

1) download libgdxHttp://libgdx.badlogicgames.com/download.html

Select release builds.

Decompress the downloaded file, as shown in the following figure:

2) Automatic Production and Development Project

The self-contained GDX-setup-UI in the package can be used to automatically generate the project, eliminating the tedious Initialization Configuration process.

Terminal to the decompressed folder, run

java -jar gdx-setup-ui.jar 

Use tools to create a project. The parameters are as follows:

Select generate the desktop project in the lower left corner to facilitate development and debugging.

If the libgdx in the middle is green, the project can be generated. If it is red, click the folder icon to locate the downloaded ZIP file.

Finally, click open the generation screen-> launch of the queue.

Three corresponding directories are generated under the current folder,

3) import the project

In eclipse, first import the game project and desktop project.

File-> Import

Select the created test-GDX-game folder-> finish.

Import test-GDX-game-desktop in the same way.

Next, import the android project.

In eclipse, CTRL + N-> Android project from exiting project-> Select Test-GDX-game-Android-> finish.

After the three projects are imported, You need to modify them.

Since we created both Android and desktop versions when creating a project, gamecode is actually in test-GDX-game, resource files are actually stored in the asset folder of the android project.

If you run the desktop project directly, an error is reported, indicating that the resource file cannot be found. Add a link to the desktop project.

Right-click test-GDX-game-desktop-> Project-> properties-> JAVA build path-> source tab-> link source... -> Browse... -> Go To The assert folder of the test-GDX-game-android project and click Finish.

After the project is established.

Both desktop and android can run directly, and the code in test-GDX-game is called.

The running results are as follows:


Project resolution

In order to initialize the application, you must implement the applicationlistener interface, which is used to update the game status (that is, logic), render the items, pause the game, save the status, and release resources.
It also has a place to handle application declaration periodic events. Each application, regardless of the background platform version, must implement the applicationlistener interface. All platforms will have and implement this interface.

Take a look at the project code:

package com.me.testgdxgame;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.graphics.GL10;import com.badlogic.gdx.graphics.OrthographicCamera;import com.badlogic.gdx.graphics.Texture;import com.badlogic.gdx.graphics.Texture.TextureFilter;import com.badlogic.gdx.graphics.g2d.Sprite;import com.badlogic.gdx.graphics.g2d.SpriteBatch;import com.badlogic.gdx.graphics.g2d.TextureRegion;public class TestGdxGame implements ApplicationListener {private OrthographicCamera camera;private SpriteBatch batch;private Texture texture;private Sprite sprite;@Overridepublic void create() {float w = Gdx.graphics.getWidth();float h = Gdx.graphics.getHeight();camera = new OrthographicCamera(1, h/w);batch = new SpriteBatch();texture = new Texture(Gdx.files.internal("data/libgdx.png"));texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);sprite = new Sprite(region);sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);}@Overridepublic void dispose() {batch.dispose();texture.dispose();}@Overridepublic void render() {Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);batch.setProjectionMatrix(camera.combined);batch.begin();sprite.draw(batch);batch.end();}@Overridepublic void resize(int width, int height) {}@Overridepublic void pause() {}@Overridepublic void resume() {}}

The lifecycle is as follows:

Several important methods:

Create (): This method is called once when an application is created.

Resize (INT width, Int, height): This method is called every time the game screen is reset and not paused. It is called once after create. The parameter is the new crazy read and height after the screen is reset.

Render (): This method is called cyclically every time rendering occurs. Game updates occur before rendering. That is to say, processing the logic first and then processing the drawing.

Pause (): Call this method before applying destroyed. It is called when the home key is pressed or re-enters the program in Android. This is a good place to save the game status. In Android, resume may not be called.

Resuem (): This method is always called in Android. When the app gets the focus, the desktop app will not call this method.

Dispose (): This method is called when destroyed is applied, after pause.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.